Jump to content
  • Advertisement
Sign in to follow this  

3D Sphere Collisions

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

I've incorporated some pretty basic 3D sphere collisions and response. It works pretty well in about 95% of all cases but in certain situations where the spheres start in positions that are already colliding by a substantial amount, the spheres start to orbit around each other in a pretty interesting manner, though definitely not what I'm looking for. The code is posted below, the collision response is borrowed from gamedev, http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=30 and I'm wondering if anyone knows what might be causing them to orbit rather than bounce off each other like they normally do. Thanks in advance.

        CVector3 difference = position - boulder->position;
	float d_diff = Magnitude(difference) - (boulder->radius + radius);

	if(d_diff <= 0)

		float f_diff1 = weight / (boulder->weight + weight);
		float f_diff2 = boulder->weight / (boulder->weight + weight);
		CVector3 pb1,pb2,xaxis,U1x,U1y,U2x,U2y,V1x,V1y,V2x,V2y;
		double a,b;
		xaxis=Normalize(pb2-pb1);							// Find X-Axis
		a=Dot(xaxis,velocity);					// Find Projection
		U1x=xaxis*a;								// Find Projected Vectors
		xaxis=Normalize(pb1-pb2);							// Do The Same As Above
		b=Dot(xaxis,boulder->velocity);					// To Find Projection
		U2x=xaxis*b;								// Vectors For The Other Ball
		V1x=(U1x*f_diff1+U2x*f_diff2-(U1x*f_diff1-U2x*f_diff2))*elasticity;						// Now Find New Velocities
		velocity=V1x+V1y;						// Set New Velocity Vectors
		boulder->velocity=V2x+V2y;						// To The Colliding Balls

		position = position + velocity*(time_diff);
		boulder->position = boulder->position + boulder->velocity*(time_diff);	


Share this post

Link to post
Share on other sites
You have to do a swept test, it gets rid of the problem if they are moving too fast that they jump through each other.

its basically the distance formula combined with the velocities*time minue radius1+radius2

and u use the quadratic equation to make time = the rest

Vector p = objects->position - objects[j]->position;
Vector v = objects->velocity - objects[j]->velocity;
double r = objects->radius + objects[j]->radius;

//quadratic equation
double a = v.x*v.x + v.y*v.y + v.z*v.z;
double b = 2*(p.x*v.x + p.y*v.y + p.z*v.z);
double c = p.x*p.x + p.y*p.y + p.z*p.z - r*r;
double det = b*b - 4*a*c; //determinant

if(det >= 0) {
double colTime = (-b - sqrt(det))/(2*a);

//using -deltaTime, incase the spheres are
//already colliding, it moves them back to
//the place where they would of collided
if(colTime >= -deltaTime && colTime < moveTime) {
//move the objects forward by this time
//then apply your collision response


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!