Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

#Actualtom_mai78101

Posted 01 August 2012 - 09:52 AM

Not necessarily a bump, but it's worth this post.

I have found another solution on Google by chance, and I decided to help others who are also looking for collision responses by linking directly and save time Googling for one.

The link is here.

The alternate solution (Author: eBusiness):

public void handleCollisions(){
	double xDist, yDist;
	for(int i = 0; i < balls.size(); i++){
		Ball A = balls.get(i);
		for(int j = i+1; j < balls.size(); j++){
			Ball B = balls.get(j);
			xDist = A.getCenterX() - B.getCenterX();
			yDist = A.getCenterY() - B.getCenterY();
			double distSquared = xDist*xDist + yDist*yDist;
			//Check the squared distances instead of the the distances, same result, but avoids a square root.
			if(distSquared <= (A.radius + B.radius)*(A.radius + B.radius)){
				double xVelocity = B.xVel - A.xVel;
				double yVelocity = B.yVel - A.yVel;
				double dotProduct = xDist*xVelocity + yDist*yVelocity;
				//Neat vector maths, used for checking if the objects moves towards one another.
				if(dotProduct > 0){
					double collisionScale = dotProduct / distSquared;
					double xCollision = xDist * collisionScale;
					double yCollision = yDist * collisionScale;
					//The Collision vector is the speed difference projected on the Dist vector,
					//thus it is the component of the speed difference needed for the collision.
					double combinedMass = A.mass + B.mass;
					double collisionWeightA = 2 * B.mass / combinedMass;
					double collisionWeightB = 2 * A.mass / combinedMass;
					A.xVel += collisionWeightA * xCollision;
					A.yVel += collisionWeightA * yCollision;
					B.xVel -= collisionWeightB * xCollision;
					B.yVel -= collisionWeightB * yCollision;
				}
			}
		}
	}
}

#1tom_mai78101

Posted 01 August 2012 - 09:51 AM

Not necessarily a bump, but it's worth this post.

I have found another solution on Google by chance, and I decided to help others who are also looking for collision responses by linking directly and save time Googling for one.

The link is here.

The alternate solution:

public void handleCollisions(){
    double xDist, yDist;
    for(int i = 0; i < balls.size(); i++){
	    Ball A = balls.get(i);
	    for(int j = i+1; j < balls.size(); j++){
		    Ball B = balls.get(j);
		    xDist = A.getCenterX() - B.getCenterX();
		    yDist = A.getCenterY() - B.getCenterY();
		    double distSquared = xDist*xDist + yDist*yDist;
		    //Check the squared distances instead of the the distances, same result, but avoids a square root.
		    if(distSquared <= (A.radius + B.radius)*(A.radius + B.radius)){
			    double xVelocity = B.xVel - A.xVel;
			    double yVelocity = B.yVel - A.yVel;
			    double dotProduct = xDist*xVelocity + yDist*yVelocity;
			    //Neat vector maths, used for checking if the objects moves towards one another.
			    if(dotProduct > 0){
				    double collisionScale = dotProduct / distSquared;
				    double xCollision = xDist * collisionScale;
				    double yCollision = yDist * collisionScale;
				    //The Collision vector is the speed difference projected on the Dist vector,
				    //thus it is the component of the speed difference needed for the collision.
				    double combinedMass = A.mass + B.mass;
				    double collisionWeightA = 2 * B.mass / combinedMass;
				    double collisionWeightB = 2 * A.mass / combinedMass;
				    A.xVel += collisionWeightA * xCollision;
				    A.yVel += collisionWeightA * yCollision;
				    B.xVel -= collisionWeightB * xCollision;
				    B.yVel -= collisionWeightB * yCollision;
			    }
		    }
	    }
    }
}

PARTNERS