• 14
• 12
• 9
• 10
• 13

# 3D Sphere Collisions

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

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

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 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.

Vector p = objects->position - objects[j]->position;Vector v = objects->velocity - objects[j]->velocity;double r = objects->radius + objects[j]->radius;//quadratic equationdouble 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; //determinantif(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	}}