Jump to content
• Advertisement

# Sphere-Sphere Sweep Test

This topic is 3127 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 I'm using the code exactly as found here: http://www.gamasutra.com/view/feature/3383/simple_intersection_tests_for_games.php?page=2 and it simply fails, I've compared to other sweep sphere code and it seems to be the same code/formula. [depressed] Does anyone have an idea or got some working code? This is a test with values demonstrating it doesn't work.
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include <math.h>

//simple vector class
struct VECTOR
{
VECTOR(float ix, float iy, float iz)
{
x=ix;y=iy;z=iz;
}
float x,y,z;
float dot(const VECTOR &v) const
{
return (x*v.x) + (y*v.y) + (z * v.z);
};
VECTOR	operator - ( const VECTOR& v ) const			{    return VECTOR(x - v.x, y - v.y, z - v.z);	}
};
typedef double SCALAR;

//code from gamasutra
template< class T >
//swap the values of a and b
inline void SWAP( T& a, T& 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, SCALAR& r2 )
{
const SCALAR q = b*b - 4*a*c;
if( q >= 0 ) //<------------------------------------ q is -29600000
{
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;
}

//my test
int main(int numargs, char*myargs)
{
//first sweep sphere
VECTOR old1(100.f, 0.f, 0.f);
VECTOR new1(-100.f, 0.f, 0.f);
//second sweep sphere
VECTOR old2(45.f, 60.f, 0.f);
VECTOR new2(45.f, -60.f, 0.f);

SCALAR m1;
SCALAR m2;
if( SphereSphereSweep(10.f,old1, new1, 10.f, old2, new2, m1, m2 ) )
{
int asd =0;//always comes in here
}
return 0;
}


any help? thanks [Edited by - chris2001net on April 29, 2010 4:46:22 PM]

#### Share this post

##### Share on other sites
Advertisement
Quote:
 VECTOR old1(100.f, 0.f, 0.f);VECTOR new1(-100.f, 0.f, 0.f);VECTOR old2(45.f, 60.f, 0.f);VECTOR new2(45.f, -60.f, 0.f);
Are you positive that this configuration should result in an intersection?

Maybe just as a test you could try a configuration for which it can be said with certainty that an intersection will occur, such as the first sphere going from (-100, 0) to (100, 0) and the second sphere going from (0, -100) to (0, 100).

#### Share this post

##### Share on other sites
gah just figured out its time based what I need something different a swept sphere to line segment test.

[Edited by - chris2001net on April 30, 2010 6:56:20 AM]

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
64
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 16
• 10
• 29
• 20
• 9
• ### Forum Statistics

• Total Topics
633413
• Total Posts
3011759
• ### Who's Online (See full list)

There are no registered users currently online

×

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