Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Coluna

Sphere to sphere collision

This topic is 5487 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all; I''m trying to get the first time ( not the closest point) that two spheres touch each other, where dist = 2*r. I tried to do that modifying the code from Oleg Dopertchouk, at gamedev, but i failed. Does anybody knows how to do that? Here is the code i was trying to change (Oleg Dopertchouk) BOOL bSphereTest(CObject3D* obj1, CObject3D* obj2 ) { // Relative velocity D3DVECTOR dv = obj2->prVelocity - obj1->prVelocity; // Relative position D3DVECTOR dp = obj2->prPosition - obj1->prPosition; //Minimal distance squared float r = obj1->fRadius + obj2->fRadius; //dP^2-r^2 float pp = dp.x * dp.x + dp.y * dp.y + dp.z * dp.z - r*r; //(1)Check if the spheres are already intersecting if ( pp < 0 ) return true; //dP*dV float pv = dp.x * dv.x + dp.y * dv.y + dp.z * dv.z; //(2)Check if the spheres are moving away from each other if ( pv >= 0 ) return false; //dV^2 float vv = dv.x * dv.x + dv.y * dv.y + dv.z * dv.z; //(3)Check if the spheres can intersect within 1 frame if ( (pv + vv) <= 0 && (vv + 2 * pv + pp) >= 0 ) return false; //Discriminant/4 float D = pv * pv - pp * vv; return ( D > 0 ); }

Share this post


Link to post
Share on other sites
Advertisement
Try something like that; it works for me


...
{
float dt = 1.0f / UPDATE_FPS; // UPDATE_FPS == 60


Vector Vp = obj1->GetLinVelocity();
Vector Vq = obj2->GetLinVelocity();

#define sign(n) (n >= 0.0f ? 1.0f : -1.0f)

Vector Vab = Vp - Vq;
Vector l = m_Position - Sphere.m_Position;
float aa = Vab * Vab;
float bb = 2.0f * (l * Vab*dt) * dt;
float cc = l * l - (m_Radius + Sphere.m_Radius) *
(m_Radius + Sphere.m_Radius);
float q = -0.5f *
bb + sign(bb) * sqrt(bb * bb - 4 * aa * cc));
float t0 = q / aa;

if (t0 < 0.0f || t0 > 1.0f)
return 0;

if (t0 > dt)
t0 = dt;
}


good luck,
David

Share this post


Link to post
Share on other sites
ok...i use the algorithm above but i have a problem...may i normalize position before use it???
i''m working with Coluna in the game...so i''m using the same post.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!