Jump to content
  • Advertisement
Sign in to follow this  
Eddy999999

Simple 3D Collision DetectionTutorials

This topic is 4380 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

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Eddy999999
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.
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 this post


Link to post
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 this post


Link to post
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.


[edit]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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Eddy999999
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
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!