PDA

View Full Version : bounding box collision detection problems



jarasez
08-20-2008, 02:27 PM
Hello!
I'm trying to implement camera movement collision detection against some walls, but the functions don't do much thing :)
Could anyone tell me what the problem is?

Those are the functions:


bool is_collision(GLfloat a[6], GLfloat b[6])
{




if(a[0] > b[1])
return false;
if(a[1]< b[0])
return false;
if(a[2] > b[3])
return false;
if(a[3] < b[2])
return false;
if(a[4] > b[5])
return false;
if(a[5] < b[4])
return false;

printf("collision");
return true;

}




//functions for moving

bool orientMe(float ang) {


float lpx = sin(ang);
float lpz = -cos(ang);

cameraBox[0] = lpx - 10;
cameraBox[1] = lpx + 10;
cameraBox[2] = y - 10;
cameraBox[3] = y +10;
cameraBox[4] = lpz - 10;
cameraBox[5] = lpz + 10;



if (is_collision(cameraBox, wallsBox)){
lx = sin(ang);
lz = -cos(ang);

glLoadIdentity();

gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,0.0f);
return true;
}
else { glLoadIdentity();
gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,0.0f);}
return false;
}


bool moveMeFlat(int i) {


float lpx = x + i*(lx)*0.1;
float lpz = z + i*(lz)*0.1;


cameraBox[0] = lpx - 10;
cameraBox[1] = lpx + 10;
cameraBox[2] = y - 10;
cameraBox[3] = y +10;
cameraBox[4] = lpz - 10;
cameraBox[5] = lpz + 10;

if (is_collision(cameraBox, wallsBox)){
x = x + i*(lx)*0.1;
z = z + i*(lz)*0.1;

glLoadIdentity();
gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,0.0f);
return true;
}
else {
glLoadIdentity();
gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,0.0f);
}
return false;
}


void inputKey(int key, int x, int y) {

switch (key) {
case GLUT_KEY_LEFT : angle -= 0.1f;
if (!orientMe(angle)) { angle+= 0.1f;}
break;
case GLUT_KEY_RIGHT : angle +=0.1f;
if (!orientMe(angle)) { angle-= 0.1f;}
break;
case GLUT_KEY_UP : moveMeFlat(1);break;
case GLUT_KEY_DOWN : moveMeFlat(-1);break;
}
}


(sorry, I'm rather new with openGL)

Thanks you very much!