# Simple 3D Collision DetectionTutorials

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

## Recommended Posts

Does anyone know of any tutorials that explain simple collision detection. The only things I can find talk about really advanced stuff that assume that you know the basics. In case it matters, I am using C++ and OpenGL. [Edited by - Eddy999999 on October 17, 2006 7:52:29 PM]

##### Share on other sites
Quote:
 Original post by Eddy999999Does anyone know of any tutorials that explain simple collision detection. The only things I can find talk about really advanced stuff that assume that you know the basics. In case it matters, I am using C++ and OpenGL.
How simple?

There are some articles in the gdnet articles section that you might check out. There's even one called 'Simple Intersection Tests for Games' (although some of the tests described aren't really that simple).

##### Share on other sites
Ya, I saw that article. However, like you said, some of the stuff isn't really that "simple". I want something that is easy for a newbie in 3D collision detection. I don't really care how "good" it is, as long as it semi-works. :P

The reason I am asking is because I am following the tutorials on nehe.gamedev.net, and I got to lesson 10, where you make a little world that you can walk around in. Its very simple, not lighting, no collision detection, etc. So I was adding some stuff to it, just to practice. I have already added lighting, but I wanted to see if I could do any simple collision detection yet.

##### Share on other sites
The most simple form of collision detection is to treat everything as a shpere.

This is also the fastest technique, so usually even if things are being compared as compex shapes, they get treated as a sphere first to see if they are possibly intersecting.

First you should learn how to use 3d vector math if you are not fammilliar with it[/edit]

Sphere-to-Sphere detection is simple - all you need to know is the center of each object, its maximum radius, and the distance between two objects.

Let p1&p2 be the positions, r1&r2 be the radii, D be the distance between the positions, and P be the Penetration depth

D = sqrt( (p1 - p2)*(p1 - p2) )

Once you know the distance, just compare it against r1+r2

P = r1+r2 - D

if P is greater than 0, then the two spheres are penetrating each other! If you want them not to penetrate, then you must move them P distance apart.

You can calculate the normal of the collision surface by normalizing the direction between the positions:
N = (p1 - p2) / D;

Then move the points in the direction of the normal:
NewP1 = p1 + N * P/2
NewP2 = p2 - N * P/2

or Lets say P1 is a static object, and P2 is a dynamic object:
NewP1 = p1
NewP2 = p2 - N * P

##### Share on other sites
That helps, Phil, and it was very simple. However, I was more looking for how to detect if a sphere is colliding with a plane. If I do spheres for every object, then the player will be able to get closer to a wall near the edges than in the middle. Is there an easy way to do this as well?

##### Share on other sites
Also, anything that provides code, or at least psuedo-code, would be preferable.

##### Share on other sites
Sphere-Plane collsion is easy - just find the distance between the center of the sphere and the plane. if the distance is smaller than the radius of the sphere then there's a collision. to find the distance between a point and a plane you need 3 pieces of information:
-the point P
-a point on the plane V
-the normal of the plane N which is normalized = length of one.
then the distance d between the point and the plane is:
d = |(P-V) X N|
where X is the cross product and |N| is the magnitude of the vector n. then check if
d < r where r is the radius. or if you want to do it faster then do:
D = (P-V) X N were D is a vector and then check if
D dot D < r*r where D dot D is the dot product of D with itself which conveniently is the lenght of the vector squared.

a great link that shows how to calculate things like the above - along with lots of other things is here:

http://www.softsurfer.com/algorithm_archive.htm

##### Share on other sites
Thanks a ton daniel! Thats exactly what I was looking for! Now I just have to figure out how to check whether or not the sphere is actually colliding with the polygon itself, or just the plane :P. But this gives me something to start with.

EDIT: Thanks for the link too!

##### Share on other sites
Quote:
 Original post by Eddy999999Now I just have to figure out how to check whether or not the sphere is actually colliding with the polygon itself, or just the plane
Sphere-to-polygon collision detection and response can be done fairly straightforwardly if you have a 'closest point on triangle to point' support function available.

Setting aside the broad phase for the moment, you can simply find the closest point on each triangle in your world to the sphere center (you can treat N-gons as triangle fans for the purpose of this test).

If you find that a closest point returned by this test is within the sphere, you can use the vector from the sphere center to this point, along with the sphere radius, to resolve the collision (I believe how to do this was described in a previous post).

Really the only tricky part is the 'triangle closest point' test. I would recommend googling, or maybe visiting geometrictools.com, to find a reference for (and perhaps implementation of) this particular test.

##### Share on other sites
Thanks, I'll look into it. Eventually, I want to be able to have poly to poly collision detection, but since this is my first time doing 3D collsion detection, I think I'll save that for another time :P.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015210
×