# trying to understand sphere on sphere sweep test

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

## 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
(
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

}

else

{

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 on other sites
1: better use code i posted in your other thread. It is not bloated, and is much cleaner than this.

2:
collision is given by solving quadratic equation
|P+V*t-C|2=r2
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 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.
Quote:
 From PMhi. 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
P+V*t,
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
|P+V*t|2=r2
(for sphere collision, r=r1+r2)

(P+V*t).(P+V*t)=r2
P.P+2*P.V*t+V.V*t2=r2
V.V*t2+2*P.V*t+P.P-r2=0
Now we have simple quadratic equation a*t2+b*t+c=0 , that is solved using
t2+(b/a)*t+(c/a)=0
t2+(b/a)*t=-(c/a)
t2+(b/a)*t+(b/2a)2-(b/2a)2=-(c/a)
(t+(b/2a))2=(b/2a)2-(c/a)
t=sqrt((b/2a)2-(c/a))-(b/2a)
so by substituting
a=V.V
b=2P.V
c=P.P-r2
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.