public static void intersect(Ball a, Ball b) {
//ref http://gamedev.stackexchange.com/questions/20516/ball-collisions-sticking-together
double xDist, yDist;
xDist = a.x - b.x;
yDist = a.y - b.y;
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 speedXocity = b.speedX - a.speedX;
double speedYocity = b.speedY - a.speedY;
double dotProduct = xDist * speedXocity + yDist * speedYocity;
// 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.getMass() + b.getMass();
double collisionWeightA = 2 * b.getMass() / combinedMass;
double collisionWeightB = 2 * a.getMass() / combinedMass;
a.speedX += (collisionWeightA * xCollision);
a.speedY += (collisionWeightA * yCollision);
b.speedX -= (collisionWeightB * xCollision);
b.speedY -= (collisionWeightB * yCollision);
}
}
}
Java Collision Detection and bounce- two circles collision and response
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment
Your Comment and Question will help to make this blog better...