Skip to main content
gamedev.net
🔒 Locked

Collision Detection

Started by SlimShady38 Jul 5, 2000 at 10:00 PM 6 replies 350+ views
Original Post
SlimShady38
SlimShady38
I''m looking for a good Collision Detection Example. I read all the ones at flipcode.com and they really don''t help that much. I learn better from examples not theory. thanks, 38
Julio
Julio
I guess you''re kinda outta luck then. flipcode has some of the best collision detection tutorials around. like that new ellipsoid collision detection tutorial. good stuff. what kind of collision detection were you looking for 2d or 3d?

JoeMont001@aol.com | Polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
SlimShady38
SlimShady38
I''m looking for 3D. But I learn from examples not from someone telling me how. I like looking a source code.
Julio
Julio
then you want bounding sphere collision detection. (or some variation of that)
    
float radius,x,y,z;
int loop;
for(loop=0; loop<NUM_VERTICIES; loop++)
{
x=yourobject.x;
y=yourobject.y;
z=yourobject.z;
radius=(float)sqrt(x*x + y*y + z*z);
if(radius>yourobject.radius)
youobject.radius=radius;
}
return 0;
}

of course the sqrt function is VERY slow. there are ways around this. but that''s up to you.

JoeMont001@aol.com | Polarisoft.n3.net
My HomepageSome shoot to kill, others shoot to mame. I say clear the chamber and let the lord decide. - Reno 911
OldManDave
OldManDave
There's also Bounding Box collision detection. Here's how it works. Say you have a tree, it's an irregular object, but what you do is you approximate a rectangular solid that approximates the size to the closest. If my tree's height was 10 units, and its width (maximum) was 10 units, and it's "depth" was also 10 units, I'd define the bounding box like this:

        
// I'm just using screen coordinates here, but in actuality
// these coordinates should be in world coordinates

// because camera is also in world coordinates

// (most of the time at least)
bbox.min_x = -5 // from the center

bbox.min_y = -5
bbox.min_z = -5

bbox.max_x = 5
bbox.max_y = 5
bbox.max_z = 5

// ...

// And when you want to check collision

if (cam.x >= bbox.min_x && cam.y >= bbox.min_y && cam.z >= bbox.min_z && cam.x <= bbox.max_x && cam.y <= bbox.max_y && cam.z <= bbox.max_z)
{
// collision with the object

}


That's over-simplified, but you get it idea I hope.

The advantages over Bounding sphere collision? Well, if you have irregular shaped objects, or wide objects, or tall objects, most of the time, Bounding box will give you the best approximation. Unless your collision object is a sphere. Also, this is a lot faster than the sphere collision because it uses none of the sqrt() stuff, or square stuff... not even an addition happened here.

Good luck!
OldManDave.

Edited by - OldManDave on July 6, 2000 1:45:43 PM
Gladiator
Gladiator
Hello... Here is an optimization (pretty big one) for the square root... The original distance formula goes like this

d² = x² + y² + z²

in order to find "d" you need to do the following

d = (x² + y² + z²)½

which means that you have use sqrt() and since the square root is way too expensive (too slow that is), you can simply leave the formula the way it was before (therefore the original one..) .. that way you dont have to use the square root function to find the radius... here is how it''s done...

I''m going to use a couple lines of Julio''s code to show you how it''s done

    
radius = x*x + y*y + z*z; // don''t use square root here..

if(radius*radius > syourobject.radius) // then square the radius to fill in for not using the square root..



that''s basically how it''s done... now it may not always work for what you want to do, but in many cases it does work.. so good luck.. and hope this helps at least a bit..

..-=ViKtOr=-..
SlimShady38
SlimShady38
Ok, The bounding box works Good for what i am doing. Now what is the best idea to do after the collision. I heard adding the polygon''s Normal to the player/camera position works?

What do you guys think?

thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.