Jump to content
  • Advertisement
Sign in to follow this  

trying to understand sphere on sphere sweep test

This topic is 5059 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

this is the sphere on sphere implementation example i found on the web but there are a few things confusing me: what exactly are the figures that are passed into the QuadraticFormula() method? i use c# and have never heard of a scalar value.what exactly is it? what is the difference between real roots and complex roots, and what are roots in general?
inline void SWAP( T& a, T& b )
//swap the values of a and b


const T temp = a;
a = b;
b = temp;


// Return true if r1 and r2 are real
inline bool QuadraticFormula
const SCALAR a,
const SCALAR b,
const SCALAR c,
SCALAR& r1, //first
SCALAR& r2 //and second roots


const SCALAR q = b*b - 4*a*c; 
if( q >= 0 )


const SCALAR sq = sqrt(q);
const SCALAR d = 1 / (2*a);
r1 = ( -b + sq ) * d;
r2 = ( -b - sq ) * d;
return true;//real roots




return false;//complex roots



const bool SphereSphereSweep
const SCALAR ra, //radius of sphere A
const VECTOR& A0, //previous position of sphere A
const VECTOR& A1, //current position of sphere A
const SCALAR rb, //radius of sphere B
const VECTOR& B0, //previous position of sphere B
const VECTOR& B1, //current position of sphere B
SCALAR& u0, //normalized time of first collision
SCALAR& u1 //normalized time of second collision



const VECTOR va = A1 - A0;
//vector from A0 to A1

const VECTOR vb = B1 - B0;
//vector from B0 to B1

const VECTOR AB = B0 - A0;
//vector from A0 to B0

const VECTOR vab = vb - va;
//relative velocity (in normalized time)

const SCALAR rab = ra + rb;

const SCALAR a = vab.dot(vab);
//u*u coefficient

const SCALAR b = 2*vab.dot(AB);
//u coefficient

const SCALAR c = AB.dot(AB) - rab*rab;
//constant term
//check if they're currently overlapping
if( AB.dot(AB) <= rab*rab )


u0 = 0;
u1 = 0;
return true;


//check if they hit each other 
// during the frame
if( QuadraticFormula( a, b, c, u0, u1 ) )

if( u0 > u1 )
SWAP( u0, u1 );
return true;


return false;


Share this post

Link to post
Share on other sites
1: better use code i posted in your other thread. It is not bloated, and is much cleaner than this.

collision is given by solving quadratic equation
If you don't know something about quadratic equations, you'll need to refresh your math. It is absolutely irrelevant what programming language you use.
'SCALAR' is typedef'ed as double or as float. That is, just replace 'SCALAR' by 'double'.

Share this post

Link to post
Share on other sites
I reply in forum so somebody maybe will come there by search...and i need to link to other related thread for them.
From PM
hi. looking at your ray sphere intersection algorithm. i have one or two questions.

1)why do you pass in i1 and i2 when they , from what i see wont have any values until later down near the bottom of the method?

2)from what ive been studying, i can see what i do if i do a test between two circles and get a false, indicating no colision, but how would i act upon the time of collision returned by your formula? could i simply ignore it and wait for an actual colision?

3)how do you measure time using double's?

thank you for all your help good sir

If it returns false, there's no collision and i1,i2 doesn't have to contain anything meaningfull... them might not even be altered.

Actually i1 and i2 is passed by reference - them is altered in the function, and when function return, them should contain time of intersection. If it would be Ada, it would be "out parameters".

You can ignore/remove i2 completely, it is time when penetration ends. Initially it was ray-sphere intersection.

As about time, it is assumed that sphere moves with equation
where P is initial position, and t is time. That is, P might be position in current frame, and V might be velocity in current frame. In this case, it will return time that will pass since current frame when it collides.

How formula was derived:
At intersection(or collision), we have
(for sphere collision, r=r1+r2)

Now we have simple quadratic equation a*t2+b*t+c=0 , that is solved using
so by substituting
we obtain final formula that can be found in my intersection/collision algo.
Note that it works for any dimension>=2 , not only for 2D and 3D.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • 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!