Advanced Sphere-Sphere Continuous Collision Detection (CCD)
Vector between sphere centers and relative speed, and sum of radiuses



If c is negative, they already overlap.


If b is 0.0 or positive, they are not moving towards eachother.

If d is negative, no real roots, and therefore no collisions

If we’ve come so far, we can calculate when we collide

Code
bool testMovingSphereSphere(Scenenode *A, Scenenode *B, double &t)
{
Planet *pa = (Planet *) A;
Planet *pb = (Planet *) B;
Vector3D<double> s = pa->pos - pb->pos; // vector between the centers of each sphere
Vector3D<double> v = pa->vel - pb->vel; // relative velocity between spheres
double r = pa->radius + pb->radius;
double c = s.dot(s) - r*r; // if negative, they overlap
if (c < 0.0) // if true, they already overlap
{
t = .0;
return true;
}
float a = v.dot(v);
//if (a < EPSILON)
// return false; // does not move towards each other
float b = v.dot(s);
if (b >= 0.0)
return false; // does not move towards each other
float d = b*b - a*c;
if (d < 0.0)
return false; // no real roots ... no collision
t = (-b - sqrt(d)) / a;
return true;
}
Popularity: 5% [?]
vector v = s1.vel-s2-vel?
Ja, jeg “bommet” litt