Sign in to follow this  

3D Sphere Collisions

This topic is 3850 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;
		pb1=position;
		pb2=boulder->position;
		xaxis=Normalize(pb2-pb1);							// Find X-Axis
		a=Dot(xaxis,velocity);					// Find Projection
		U1x=xaxis*a;								// Find Projected Vectors
		U1y=velocity-U1x;
		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
		U2y=boulder->velocity-U2x;
		V1x=(U1x*f_diff1+U2x*f_diff2-(U1x*f_diff1-U2x*f_diff2))*elasticity;						// Now Find New Velocities
		V2x=(U1x*f_diff1+U2x*f_diff2-(U2x*f_diff2-U1x*f_diff1))*elasticity;
		V1y=U1y;
		V2y=U2y;
				
		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[i]->position - objects[j]->position;
Vector v = objects[i]->velocity - objects[j]->velocity;
double r = objects[i]->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

This topic is 3850 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.

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