Simple 3D Collision DetectionTutorials

Started by
11 comments, last by Eddy999999 17 years, 6 months ago
Quote:Original post by Eddy999999
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.


Do not preocupy yourself much with poly to poly collision detection, you will find that 1) it is too computationally expensive to use and 2) it isnt that useful anyway, the really good ones are the combinations of the following shapes: Ray, plane, Sphere, box (both axis aligned and object aligned), convex polyhedron.

Edit: As a side note, ray-triangle/poly is particularly useful for finding out if/where a bullet hit after what is called the "broad" phase of collision detection.

Best of luck!
Advertisement
Yep. Simplest of the simplest is detecting Sphere-triangle intersection, and resolving the intersection by pushing the sphere out of the way.

This si simply achieved by finding the closest point on the triangle to the sphere centre, and pushing the sphere along the vector (sphereCentre - closestPoint) far enough so the sphere stops intersecting.

I have an example of such collision here.

http://uk.geocities.com/olivier_rebellion/simple_collision.zip

It's using a verlet integrator, so it's a bit unusual in that sense. But it makes things very simple and straight forward.

void UpdateSphere(Vector &prevSpherePos, Vector &spherePos, float sphereRadius, float dt){    const Vector gravityVector(0.0f, -9.81f, 0.0f);    // integrate    Vector temp     = spherePos;    spherePos      += (spherePos - prevSpherePos) + gravityVector * (dt*dt);    prevSpherePos   = spherePos;    for(int i = 0; i < numSceneTriangles; i ++)    {        // find closest point        Vector closestPoint = PointTriangleClosest(spherePos, sceneTriangle.V[0], sceneTriangle.V[1], sceneTriangle.V[2]);                    // work out distance of point to sphere centre        Vector dirPush = (spherePos - closestPoint);        float distance = dirPush.Length();        // intersection, push sphere away from closest point        if(distance < sphereRadius)            spherePos += dirPush * (sphereRadius - distance) / distance;    }}


voila!

you *should* have a sphere moving about under gravity (code untested).

Everything is better with Metal.

Thanks for the advice kwizatz, and thanks for the code oliii. I'll take a look at it when I get a chance.
God is not all-powerful, as he cannot build a wall he cannot jump.Stelimar Website: eddy999999.ed.funpic.org/Stelimar/index.html

This topic is closed to new replies.

Advertisement