double testBallCollision(Ball b, double t) {
if (t == 0.0) {
return -1;
}
// Calculate the movement vector a for t
Vector movea = current.velocity * t;
// Calculate the movement vector b for t
Vector moveb = b.current.velocity * t;
double deltaX = b.current.position.x - current.position.x;
double deltaXSquared = deltaX * deltaX;
double deltaY = b.current.position.y - current.position.y;
double deltaYSquared = deltaY * deltaY;
int sumRadii = radius + b.radius;
int sumRadiiSquared = sumRadii * sumRadii;
double distSquared = deltaXSquared + deltaYSquared;
// Reduce dynamic-dynamic collision to dynamic-static
Vector movec = movea - moveb;
// If the length of the movement vector is less
// then the distance between the objects,
// then there is no way they can hit
if (movec.length() < sqrt(distSquared) - sumRadii) {
return -1; // no collision
}
Vector N = movec.normalize();
// Find C, the vector from the center of the moving
// ball A to the center of ball B
Vector C = b.current.position - current.position;
// D is the point along movec that is closest to ball b
// D = N . C = ||C|| * cos(angle between N and C)
double D = N.dot(C);
// Find the lenght of the vector C
double lengthC = C.length();
// sqrt(F) is the distance between the center of ball b
// ad the closest point on movec
double F = (lengthC * lengthC) - (D * D);
// If the closest that A will get to B
// is more than the sum of their radii, there's no
// way they are going collide
if (F >= sumRadiiSquared) {
return -1; // no collision
}
// We now have F and sumRadii, two sides of a right triangle.
// Use these to find the third side, sqrt(T)
double T = sumRadiiSquared - F;
// If there is no such right triangle with sides length of
// sumRadii and sqrt(f), T will probably be less than 0.
// Better to check now than perform a square root of a negative number
if (T < 0) {
return -1; // no collision;
}
// Therefore the distance the circle has to travel along the
// movement vector is D - sqrt(T)
double distance = D - sqrt(T);
// Get the magnitude of the movement vector
double mag = movec.length();
// Finally, make sure that the distance A has to move
// to touch B is not greater than the magnitude of the
// movement vector.
if (mag < distance) {
return -1; // no collision
}
// Return the time it takes ball A to travel this distance
double collision_t = (distance / mag) * t;
if (collision_t < 0.0 || collision_t > t) {
return -1; // no collision
}
return collision_t;
}
Angular motion and CCD
I been researching how to handle angular motion in Continuous collision detection and was wondering when only dealing with circle-circle collisions if there are any other ways besides using Conservative Advancement (from thread http://www.gamedev.net/community/forums/topic.asp?topic_id=556189).
Without angular motion I have the following algorithm for finding Time of Impact:
I could be misunderstanding the question, but the angular momentum of circles won't affect the time of collision. At least as long as they have constant density. When a circle rotates around its center the space that it takes up doesn't change.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement