Sign in to follow this  
chris2001net

Sphere-Sphere Sweep Test

Recommended Posts

chris2001net    215
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


Link to post
Share on other sites
jyk    2094
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


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this