//--------------------------------------- Intersection::CollisionInfo Intersection::Find( float t, const Spheref& A, const Vec3f& velA, const Spheref& B, const Vec3f& velB ) { // Get the relative velocity Vec3f vRel = velB - velA; float a = vRel.LengthSqr(); // Get the minkowski sum of the spheres Vec3f center = B.Center - A.Center; float c = center.LengthSqr(); float r = A.Radius + B.Radius; float r2 = r * r; CollisionInfo cInfo; if ( a > 0 ) { float b = center.Dot( vRel ); if ( b <= 0 ) { if ( -t*a <= b || t*(t*a+2*b) + c <= r2 ) { float deltaC = c - r2; float d = b*b - a*deltaC; if ( d >= 0 ) { // Sphere start in intersection if ( deltaC <= 0 ) { // Contact point is midpoint cInfo.ContactPoint = 0.5f * ( A.Center + B.Center ); cInfo.ContactTime = 0.0f; } else { cInfo.ContactTime = -( b + std::sqrtf( d ) ) / a; if ( cInfo.ContactTime < 0 ) { cInfo.ContactTime = 0.0f; } else if ( cInfo.ContactTime > t ) { cInfo.ContactTime = t; } cInfo.ContactPoint = A.Center + cInfo.ContactTime * velA + ( A.Radius / r ) * ( center + cInfo.ContactTime * vRel ); } cInfo.Collision = true; return cInfo; } } cInfo.Collision = false; return cInfo; } } // Sphere start in intersection if ( c <= r2 ) { // Contact point is midpoint cInfo.ContactPoint = 0.5f * ( A.Center + B.Center ); cInfo.ContactTime = 0.0f; cInfo.Collision = true; return cInfo; } cInfo.Collision = false; return cInfo; }
//--------------------------------------- // Sphere v Sphere //--------------------------------------- bool Intersection::Test( const Spheref& A, const Spheref& B ) { Vec3f d = B.Center - A.Center; float r = A.Radius + B.Radius; return d.LengthSqr() <= r*r; }