sphere collision help please

Started by
8 comments, last by b_myeye 14 years, 10 months ago
I know there is lots of posts in regards to this on this forum and on google but through all my research almost 2 days now I have not found a working solution to my issue. If there is a solution posted and I have somehow missed it I apologize I am very tired of researching with no results so I thought I would try asking for help. The easiest scenario to explain it in is billiards but with balls that have different sizes( masses ). Every sample equation / code I have found of other people doing this is always using a "Cartesian coordinate system" which I am trying to avoid if possible but if I don't have a choice then I am willing to use that type of solution. each ball object has these properties: - velocity - radius - x - y - rotation - mass The current method that the ball moves is using the "rotation" which is the angle that it is facing and it follows that line using the velocity. From the rotation and velocity the new x/y are populated. I would like to point out that even though I am very comfortable with programming I have no issue with pointing out that math is definitely not my strong point. Anyway the results of two objects( balls ) colliding is what I am looking to find. I guess another way of putting it is the response AFTER the collision. The collision itself is very simple and is not what I need help with. The desired output that I would like to get from the collision of two balls is the new velocity and the new rotation( the facing angle or direction that the ball travels on ) of each ball. I am not interested in the spin which is a major factor in a real world billiard type of collisions. Any help would be appreciated in the form of math equations , code snippet or any other.
Advertisement
Quote:Every sample equation / code I have found of other people doing this is always using a "Cartesian coordinate system" which I am trying to avoid if possible but if I don't have a choice then I am willing to use that type of solution.
I'm not sure why you're trying to avoid that. You're specifying the positions of your balls in Cartesian coordinates. Do you understand what Cartesian coordinates are?
Im not much of a math wiz myself but i believe when something hits something else, it's always the "law of reflection" that determines the angle it "bounces off at" - weather it's light or pool balls.

so i think here's what you do... (draw this out on paper it might help you understand what im talking about better)

#1) when you detect that the balls have hit, make an imaginary line between their center points.
#2) get the normal to this line and use that to make a line that bisects the line from step 1 (this is an "imaginary wall" that both balls have hit)
#3) make the balls reflect so that the angle they are hitting the imaginary wall at is the same angle they are leaving the imaginary wall at.

This link talks about the law of reflection as it pertains to light but the principles are the same (:

http://www.glenbrook.k12.il.us/gbssci/phys/CLass/refln/u13l1c.html

BTW extra credit - after you understand how this works you can do away with getting the normal to the line in step 2 but i'll leave figuring that part out up to you hehe (;

Edit: darn i realized this only works if the 2 balls are going the same speed. hopefully it still helps some...

[Edited by - Atrix256 on May 28, 2009 8:51:43 PM]
As Sneftel pointed out, I don't think you want to avoid Cartesian coordinates. Cartesian does sound fancy, but I'm guessing that is the coordinate system you are using. Maybe you should take another look at those solutions you've been looking at?

I'd also like to point out that velocity is usually considered to be a vector that explains the direction an object is traveling while the magnitude of that vector is equal to the speed of the object. I think you're using the term velocity to mean speed which may be confusing you when reading the papers.
Sneftel:

Yes I do know what Cartesian coordinate is.

Artrix256:

Thank you for your input but I already have the axis of collision( I think ). Due to my unfortunate understanding of Physics / Trig( or lack of it ) I am unable to properly implement a solution using the Conservation of momentum and the Conservation of energy. However I am able to make a ball bounce off of a flat wall but when there is two balls with different masses and different movement speed( velocity ) and the possibility of collision from any angle I am cross eyed.

Peter5895:

Yes you are absolutely right Cartesian coordinates does sound very fancy and I would feel very cool if I could find a way of using it in a regular every day conversation because I would feel really smart ;) . And you are probably right in that I am not using the proper terminology to define the terms.

Today is another day so I am having another go at it if anyone else would like to add their input I welcome it.
If you sit down and learn vectors, dot product and cross product, it makes game development a WHOLE lot easier. It helps with a lot of things.

Those 3 things, along with remembering SOH-CAH-TOA for trig are seriously most of the math you will ever need IMO (:
VERTEX3D subtract_vector(VERTEX3D a,VERTEX3D b)
{
VERTEX3D c;

c.x=a.x - b.x;
c.y=a.y - b.y;
c.z=a.z - b.z;

return c;
}

float vector_length(VERTEX3D c)
{
return (float)sqrt((c.x * c.x) + (c.y * c.y) + (c.z * c.z));
}

nt sphere_collision(VERTEX3D pos1, VERTEX3D pos2,float radius1,float radius2)
{

//calculate a limit distance (radius1 + radius2)
float distance = radius1 + radius2;
//make a vector
VERTEX3D result = subtract_vector(pos1, pos2);
//calculate current distance from two bodies
float current_distance = vector_length(result);
if(current_distance < distance)
return 1;
else
return 0;

}
Its works
Dmitriy Deordiy.
Dmitry has the basic solution, bu there is one thing missing. You have to account for the situations where the balls are just resting while touching, and the situation where they are moving apart.
Since little is done without defining the borders in which we consider that a collision has happend, this is necessary.
So when the balls are within some predefined interval, we go to DEFCON1 and acknowledge we MIGHT have a collision, then we check their relative velocity to differentiate between a collision, moving apart of just touching.
Quote:Original post by dmitriyd
VERTEX3D subtract_vector(VERTEX3D a,VERTEX3D b)
{
VERTEX3D c;

c.x=a.x - b.x;
c.y=a.y - b.y;
c.z=a.z - b.z;

return c;
}

float vector_length(VERTEX3D c)
{
return (float)sqrt((c.x * c.x) + (c.y * c.y) + (c.z * c.z));
}

nt sphere_collision(VERTEX3D pos1, VERTEX3D pos2,float radius1,float radius2)
{

//calculate a limit distance (radius1 + radius2)
float distance = radius1 + radius2;
//make a vector
VERTEX3D result = subtract_vector(pos1, pos2);
//calculate current distance from two bodies
float current_distance = vector_length(result);
if(current_distance < distance)
return 1;
else
return 0;

}
Its works
Dmitriy Deordiy.


Formatting tags on gamedev.net.
This might be a good read:
http://www.geocities.com/vobarian/2dcollisions/2dcollisions.pdf

This topic is closed to new replies.

Advertisement