View Full Version : Collision detection

GLUTtony

12-08-2008, 08:28 AM

Hi everyone,

I've read through these forums and I know this topic has been covered many times, but I've spent the best part of a week trying to get my head around collision detection after scouring the internet and books trying to understand it.

I believe I have made a start and would like an opinion of whether I'm on the right track or barking up the wrong tree. This is my understanding of it so far:

Assuming there are two cubes made up of quads, we can use an if statement to check if the x,y,z co-ordinates of (the center of one cube + the x,y,z distance from the centre to the faces) corresponds to the same x,y,z values of the second cube. If they do then a collision has occurred and we do whatever code follows.

Am I correct, roughly, or completely off?

I would appreciate some help as this is turning my head from sideshow bob to homer simpson with frustration.

Many thanks :)

_NK47

12-08-2008, 08:38 AM

for cubes there are boundig boxes or AABB (axialy aligned bounding box). basically all they have is the minima and maxima of the vertices or better the geometry. if any point is inside this two points then you collide with something. roughly you where correct. the simplest collision check is done with spheres because you dont have to update their orientation and the data representation is quite compact (radius and center point). look up bounding volumes/bounding boxes/aabb on google for some tutorials.

p.s. obviously the best fit for boxes are boxes.

GLUTtony

12-08-2008, 10:00 AM

Thanks _NK47,

I've looked at AABBs and OBBs and have a hard time understanding them. They might be simple but the code I've found normally only contains details for 2 cubes at the most or no references to the rest of the associated code (headers, rendering etc) or how to incorporate the collision code into a program. But I'm slowly getting the hang of it now.

GLUTtony

12-08-2008, 11:08 AM

Ok,

So I got the basic understanding and knowhow on how to make a bounding box, now I'm stuck trying to interpolate it into code when drawing the cube.

I know I'm meant to replace the normal vertex co-ordinates with most likely an array to hold them, then I assume that to move them you add the value of the translation to the array values, something like this:

int xpos, ypos, zpos;

Vectors[-1,1,-1,1,-1,1] //where each is the max and min value of each x,y,z co-ordinate.

void cube(){

glBegin(GL_QUADS)

//Draw front face of a cube

glVertex3f(Vectors[0]+xpos,Vectors[2]+ypos,Vectors[5]+zpos);

glVertex3f(Vectors[1]+xpos,Vectors[2]+ypos,Vectors[5]+zpos);

glVertex3f(Vectors[1]+xpos,Vectors[3]+ypos,Vectors[5]+zpos);

glVertex3f(Vectors[0]+xpos,Vectors[3]+ypos,Vectors[5]+zpos);

//Draw rest of surfaces....

glEnd();

}

//additional code

int Draw(void){

glLoadIdentity();

glTranslated(xpos,ypos,zpos);

Cube();

}

I've left out the modelview and other stuff as it's not essential to what I'm trying to understand. But then to do collision detection you just check if a cube's current position is next to another.

I apologise if it seems all over the place or doesn't make sense, I'm having a really hard time understanding it. If anyone can throw any light on it it'd be much appreciated.

_NK47

12-09-2008, 01:53 AM

"then I assume that to move them you add the value of the translation to the array values, something like this:"

its all better to have a unit cube with length 1 centered at the origin (0,0,0) and do all translation, rotation, scaling with the modelview matrix.

"check if a cube's current position is next to another"

that would not work properly. you need to know how big the bounding box is. every object has different size and thus different bounding box. again, the box has minima and maxima points which specify the box boundaries. if a point is inside those two points (or equal) then collision took place.

something like this:

bool OOB::isColliding(const Point& p) const

{

if(p.x >= m_min.x && p.y >= m_min.y && p.z >= m_min.z

&& p.x <= m_max.x && p.y <= m_max.y && p.z <= m_max.z)

return true;

return false;

}

the check is pretty trivial, more important is to transform the bounding volumes correct if they move, rotate or scale.

Powered by vBulletin® Version 4.2.5 Copyright © 2018 vBulletin Solutions Inc. All rights reserved.