# collision

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
Oh man, i had hoped collision would be easier

##### Share on other sites
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.

##### Share on other sites
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 :)

##### Share on other sites
Quote:
 Original post by Beaverbutt8Ok...... 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[i] < min2[i]) return false;        if (max2[i] < min1[i]) return false;    }    return true;}

##### Share on other sites
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/

##### Share on other sites
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 :)

##### Share on other sites
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:

// collision!
}else{
// no collision
}

##### Share on other sites
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?

##### Share on other sites
if you have a camera, and its openGL, it must have a position. i assume your using gluLookAt or something with a little camera class right? use the position values from your camera class.

##### Share on other sites
Oooh - duh :p - why didn't i think of that before? :P

##### Share on other sites
just do something like this:

if (x > boxleft && x < boxleft && y > boxtop && y < boxbottom)
{
...
}

that for 2d for 3d use:

if (x > boxleft && x < boxleft && y > boxtop && y < boxbottom && z > boxfront && z < boxback)
{
...
}

##### Share on other sites
Quote:
 Original post by JasonL220that for 2d for 3d use:if (x > boxleft && x < boxleft && ...){...}

That if will never run...
I haven't done any 3d collision stuff yet, but, assuming you've done 2d collision detection, just think of that:

You check if obj1's right side is to the left of obj2's left side:
/---\  /---\ |obj|  |obj|| 1 |  | 2 |\---/  \---/

Or if obj1's left side is to the right of obj2's right side:
/---\  /---\ |obj|  |obj|| 2 |  | 1 |\---/  \---/

If either of those are true, then, in 2D, they can't possible be colliding...
3D is just more complicated, because they may not be on the same axis, so the top-left edge of a box might be at position 5, and the bottom-left edge might be at position 20 - creating a very lopsided box... so then you have to figure out where the left side of that box is at the height(s) the other collision object is at:
(side view)..<------\ ...\      \ ....\ obj2 \ /---\\      \ |obj| \------> | 1 |\---/

So, in the little diagram above, you have to know the math to see where the top, left of obj2 is. The math to figure out how far away that 4th line of obj2 is from the top-left. Turn that distance into an offset on the (in this view) x axis. Then you can do the collision detection with the top-right of obj1, and that point on the left side of obj2.
Imagine if obj2 is spinning on an extra axis as well:
(front view)//---\ ||obj| || 2 | |/---\ \|obj| .| 1 | .\---/ (side view)<--<----\ .\  \    \ ..\  \obj2\ /---\ \    \ |obj|-->----> | 1 | \---/

So... forget about openGL and programming for a minute. If you had those two boxes in real life, would you be able to find out where that colliding point is on obj2?

Again, I haven't programmed any 3D collision detection, but this is what seem's logical to me, and I haven't really been able to deduce any other possible way of detection (except obivously using sphere's would make it slightly easier... but also slightly/much less accurate).

Take another look at jeffjets' links - they are good ones... for starters read through the last 2 articles in his first link, and the 3 before the advertisement in the middle of the page - you can't get much more basic than that...

 sorry about all the dots... the "typewriter" feature of the forums dont like leading spaces... or lines ending with an escape character (\))[/edit]

##### Share on other sites
You could just use a library like coldet.

http://photoneffect.com/coldet/

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628305
• Total Posts
2981967

• 9
• 13
• 11
• 12
• 11