collision

Started by
13 comments, last by gosper 18 years, 5 months ago
Hey guys, Does anyone know how to do some simple bounding box collision detection in OpenGL, and cut down on the math as much as possible? Also, how does one light a scene with OpenGL? Thanks :) Mike
Advertisement
OpenGL is a Rendering API. There is close to no functions in it that will help you to do collision detection. Thats first.
Second, to do collision detection, you dont need a rendering API, you can simply utilize any collision detection library, like ColDet or some other one.
And lastly, to light up a scene, you need to enable lights by using glEnable(GL_LIGHTING) and initialize some lights attributes. See NeHe's tutorial on his page - http://nehe.gamedev.net

Good luck.
Oh man, i had hoped collision would be easier
Quote:Oh man, i had hoped collision would be easier
Collision detection can be anywhere from really easy to really hard. If you just need simple bounding-box collision detection, you shouldn't have too much trouble. I'd go to the articles section here on gdnet and look for the article called 'simple intersection tests for games' or something like that. It might not be that one, but there's one by Gomez that includes tests for both stationary and moving axis-aligned bounding boxes.
o_0
~

Ok...... The math is a little.. err... complex. Could you give me an example for collsion detection with a simple bounding box in OpenGL?








Mike :)
Quote:Original post by Beaverbutt8
Ok...... The math is a little.. err... complex. Could you give me an example for collsion detection with a simple bounding box in OpenGL?
The first thing to realize is that collision detection has little if anything to do with OpenGL; most collision detection algorithms are independent of particular graphics APIs or platforms.

The code for intersection between two AABBs should be easy to find, but I'll go ahead and sketch it out (no guarantee of correctness):
bool IntersectAABBS(Vector3 min1, Vector3 max1, Vector3 min2, Vector3 max2){    for (int i = 0; i < 3; ++i) {        if (max1 < min2) return false;        if (max2 < min1) return false;    }    return true;}
If you are just starting off with collision you may want to look into sphere collision detection. You may think that it will be harder but I actually found it easier. There is a little less math to it because you can just test the distance between the 2 center points and if the distance is less than the sum of the 2 radii then you collided =) It's not too hard to do distance to a plane and then alter it to a triangle also.

Here are some places I looked at and learned my collision detection from. I know it's tough to learn from scratch when you have nobody to directly talk to about it.

http://www.gamespp.com/algorithms/collisiondetection/
http://www.cs.montana.edu/~charon/cs515/
http://www.peroxide.dk/download/tutorials/tut10/pxdtut10.html
So, basicly -

A tree is located at x1 z1
if ( x == 1 && z == 1 )
{
x -= 2;
z -= 2;

}

or something like that, right?









Happy Thanksgiving :)
Not too sure what you mean there, all that function you just posted is doing is subtracting 2 from x and z if they both are equal to 1, it is not doing any collision check really. Collision involves 2 objects. For example your player and a world object(terrain, monster, bullet, etc). Say your player is at (px, py, pz). In this example lets say a bullet is at (bx, by, bz). You want to test if your player was hit by the bullet.

If you are using spheres you make the center the posiiton of the object and then use a radius so that your entire player and the bullet is enclosed each in their own sphere. If you are using AABB you make your box so the objects are also enclosed. Then you test if those 2 spheres/AABB intersect. If they intersect then you had a collision. You can then either not let your player move there or in this case consider him dead because he was shot =)

If you are using sphere to sphere collision you can just calculate the distance between the 2 center points and then see if that distance is less than the radius of the 2 spheres combined.

Distance between sphere P and B formula:

dist = sqrt(pow((B.center.x - P.center.x), 2) + pow((B.center.y - P.center.y), 2) + pow((B.center.z - P.center.z), 2));

then the test:

if(dist < (P.radius + B.radius)){
// collision!
}else{
// no collision
}
Seems to make sense. So, lets say theres a cube with a bounding sphere in OpenGL. Right my program is just a camera going around a forest. But i don't think that it is detecting any variable for it's position. So, what should i do?

This topic is closed to new replies.

Advertisement