3D collisions

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
tkondal
Posts: 8
Joined: Thu May 25, 2006 4:00 am

3D collisions

Post by tkondal »

Hi all.

I started working with openGL recently and have been able to successfully load 3d models and to render them with lighting effects. This is all done on Windows. I'm planning on porting this app to the PSP once I complete it. I would like to know if anyone has references or already-written code/classes for 3D collisions of spherical objects.

Thanks.
ufoz
Posts: 86
Joined: Thu Nov 10, 2005 2:36 am
Location: Tokyo
Contact:

Post by ufoz »

um... that's pretty trivial, here's some pseudocode just from memory:

Code: Select all

bool spheres_collide(vec3d pos1, vec3d pos2, float rad1, float rad2)
{
    vec3d dist = pos2-pos1;
    float dist_squared = dot(dist,dist);
    return &#40;dist_squared < sqr&#40;rad1 + rad2&#41;&#41;;
&#125;
Edit: gah phpbb is retarded. "You cannot make another post so soon after your last; please try again in a short while." - making another post so soon my ass, I'm editing my own damn post I just made.
tkondal
Posts: 8
Joined: Thu May 25, 2006 4:00 am

Post by tkondal »

Thanks for the snippet ufoz, but I think a part of my post didn't appear:


"I would also like to know how to calculate the direction of the spheres after the collision has occured. In this case, it would be a sphere colliding with a static object. Sort of like a ball hitting a wall. How can I determine the position vector or angle of reflection of the ball? I know how to do it for a 2D scenario, but I'm not sure about the 3D case."

Thanks again.
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

tkondal wrote:Thanks for the snippet ufoz, but I think a part of my post didn't appear:


"I would also like to know how to calculate the direction of the spheres after the collision has occured. In this case, it would be a sphere colliding with a static object. Sort of like a ball hitting a wall. How can I determine the position vector or angle of reflection of the ball? I know how to do it for a 2D scenario, but I'm not sure about the 3D case."

Thanks again.
It's basically the same as in 2D, if you use vector math to do it. What you need is the ball direction before the hit and the normal of the wall or surface and you're ready. Calculate the dotproduct of both vectors plus the dot product (square length) of the direction vector, then the reflection vector is the normal - 2*(n.d) / (d.d)*d, where . is the dotproduct, n is the normal and d the direction vector, as well as * the (scalar) multiplication.
The reflection vector is then your new direction vector (which you could again scale by a value < 1 to denote the effect of friction if your direction vector contains the movement speed as its length).
When two spheres hit, the surface normale is the normalized vector from the center of the second sphere to the point of intersection.
This assumes that the wall to hit, or the second sphere are static (ie, immovable). If you want the results of an elastic collision, you need a little different formula and calculate the movement vector of both objects after the hit, which depend on the masses of the two objects. But this goes pretty much into a complete physics simulation. Read here for the formula of elastic collision if you need it though: http://en.wikipedia.org/wiki/Elastic_collision
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
tkondal
Posts: 8
Joined: Thu May 25, 2006 4:00 am

Post by tkondal »

Raphael wrote:
tkondal wrote:Thanks for the snippet ufoz, but I think a part of my post didn't appear:


"I would also like to know how to calculate the direction of the spheres after the collision has occured. In this case, it would be a sphere colliding with a static object. Sort of like a ball hitting a wall. How can I determine the position vector or angle of reflection of the ball? I know how to do it for a 2D scenario, but I'm not sure about the 3D case."

Thanks again.
It's basically the same as in 2D, if you use vector math to do it. What you need is the ball direction before the hit and the normal of the wall or surface and you're ready. Calculate the dotproduct of both vectors plus the dot product (square length) of the direction vector, then the reflection vector is the normal - 2*(n.d) / (d.d)*d, where . is the dotproduct, n is the normal and d the direction vector, as well as * the (scalar) multiplication.
The reflection vector is then your new direction vector (which you could again scale by a value < 1 to denote the effect of friction if your direction vector contains the movement speed as its length).
When two spheres hit, the surface normale is the normalized vector from the center of the second sphere to the point of intersection.
This assumes that the wall to hit, or the second sphere are static (ie, immovable). If you want the results of an elastic collision, you need a little different formula and calculate the movement vector of both objects after the hit, which depend on the masses of the two objects. But this goes pretty much into a complete physics simulation. Read here for the formula of elastic collision if you need it though: http://en.wikipedia.org/wiki/Elastic_collision
Thank you very much Raphael. I will try to implement it as you're suggesting.
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

When two spheres hit, the surface normale is the normalized vector from the center of the second sphere to the point of intersection
Which is the same as the normalised vector between the spheres' centres.

Jim
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

Jim wrote:
When two spheres hit, the surface normale is the normalized vector from the center of the second sphere to the point of intersection
Which is the same as the normalised vector between the spheres' centres.

Jim
That's right :D Thanks for the clear up
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Post Reply