View Full Version : collision detection and bounding boxes
misteriousnex
12-08-2008, 12:32 PM
Hi everyone,
I'm in need of some help with collision detection. I've decided to use AABBs as they seem to be the simplest method of handling collision detections (i'm using cubes exclusively for my program so it's the easiest choice by far). I have an understanding of how collision detection works and some of the maths involved, but when it comes to implementing it in code I get stumped. I understand the collision detection functions etc, but I have no clue how to add the necessary vectors etc into the drawing function for drawing a cube.
I have an idea that glVertex3D(x,y,z) should have an array in it for each co-ordinate in the vertex, but that's about as far as I can understand.
I've spent the past week googling it to death and going over the famous red book as well as the openGL game programming book but with no luck as they all include a vector class which is too complicated as I need to be able to explain my code, or only the collision detection code itself with no reference to how to include it in the shapes drawn.
I might be a bit cheeky and presumptious, but does anyone know of a simple (like a 5 year old) explanation of how to add it into the drawing routines or can show some sort of code (pseudo or otherwise) that will explain it?
Many thanks :)
.oisyn
12-08-2008, 01:24 PM
I don't quite understand what it is that you are having problems with. The main subject of your topic seems to be collision detection, but the actual problem seems more about drawing boxes. You do understand those two subjects are quite unrelated to each-other, right?
So, if you're question is more like: "how do you draw a box?", you draw a box by drawing it's faces. A box has 6 of them, with each face having 4 vertices each. I don't know how you currently store the boxes, but you are able to compute the 8 unique vertices of the box, right? Well, just put these 8 verts into an array in some order, and draw a 3D box on the piece of paper, and number the vertices with their corresponding indices in the array. Then for each side of the box, figure out which 4 verts make up the face for that side. Put the verts in the right order (either clockwise or counterclockwise depending on your coordinate system), and then simply draw that face using 4 glVertex() calls (as a quadlist primitive)
misteriousnex
12-09-2008, 12:21 AM
Hi,
I apologise if what I said didn't make much sense, I'm still trying to understand it myself.
I am able to draw a cube with glVertex3(f/d)(x,y,z) and create a cube on screen. I assume that creating an array to store the min and max values of x,y,z (say -1,1 as min,max) and then creating one face with
glVertex3f(minx,maxy,maxz);
glVertex3f(minx,miny,maxz);
glVertex3f(maxx,miny,maxz);
glVertex3f(maxx,maxy,maxz);
would allow you to create a cube by doing the same for all 6 faces, I'm not even sure that's correct :s
Unless the array is declared as such
Cube[3][8] =
{-1,1,1,
-1,-1,1,
1,-1,1,
1,1,1,
-1,1,-1,
-1,-1,-1,
1,-1,-1,
1,1,-1}
and then the cube is drawn with
glVertex3f(Cube[0][0],Cube[0][1],Cube[0][2]);
etc for each other vertex.
If that then works, I'm stumped on how to figure out how to use it for collision detection, unless to work out the size of the cube I use
CubeMax = Cube[0][2] - Cube[0][0] where CubeMax is the max size.
I have a feeling I'm completely lost here
SmokingRope
12-09-2008, 02:17 AM
The opengl stuff you're using to draw the cube is completely independent from what you're going to use to do collision detection.
You might want to start by thinking about how you will move the cube around on the screen. One way would be to use the glTransform() function. You could also change the coordinates that are in your array (Cube).
Once you have decided how to move the cube around the screen, you will also want to keep track of the size of the cube. With the cube's position, and it's size, you can then write code to do collision detection.
Since you're using AABB's you might want to do something like:
if( boxA.MIN_XCOORDINATE - boxB.MAX_XCOORDINATE < 0 &&
boxA.MAX_XCOORDINATE - boxB.MIN_XCOORDINATE > 0 )
{ collide(boxA, boxB); }
.oisyn
12-09-2008, 03:04 AM
It's important to realize that collision detection has nothing to do with rendering. A box is just an entity in your system, with different representations. On one hand you have a mathematical representation (such as a center and an extents vector) which makes it easy for you to do collision detection. On the other hand you have a graphical representation, a mesh made out of polygons, with polygons having more properties than just it's vertices (such as materials and texture coordinates and such). You don't use the mesh for collision detection, neither do you use the mathematical representation for drawing. Of course, you could do that, but then you're just making things harder for yourself.
Aside from the different representations, an entity (and I mean every entity, not necessarily only those that are boxes) also has some other properties like a transformation matrix to place the entity somewhere in the world, a velocity vector describing it's current speed and direction of movement, and flags like whether it is collidable or whether it is under the influence of gravity. The collision detection system takes some of these properties to update the transformation matrices of all entities. This matrix is also used by other systems such as the renderer, so it knows where to draw the mesh representation of the entity.
vBulletin, Copyright ©2000-2010, Jelsoft Enterprises Ltd.