Hi,

I am writing a game in C++ and OpenGL. My whole environment is based on cubes so I guessed bounding box collisions were best. I can currently move around and my program is detecting collisions (I used this tutorial: http://www.3dcodingtutorial.com/Collision-Detection/Collision-Boxes.html). Now I want to make it so that when the camera hits a bounding box it slides along it (like in any FPS game).

My update camera procedure looks like this:

``````void updateCamera()
{
glRotatef(xrot,1.0,0.0,0.0);  //rotate the camera on the x-axis (left and right)
glRotatef(yrot,0.0,1.0,0.0);  //rotate the camera on the y-axis (up and down)

glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of the camera
}``````

Thanks,

James

## All 4 Replies

Your camera has some position associated with it, check that position for potential collision and if collision moving up should mean a slight sliding to the right or left depending on the view vector

Hi,

What code would I use in order to check for collisions. I have my camera point set up and the min/max values for x,y,z of the objects. But I am unsure of how to compare these two. Can you give me an exaple.

Thanks

Use that min/max values to setup a bounding box for your camera. Then it would be the same comparison as your other bounding box collision test.

My setupbox procedure is giving me a segfault and I can't find what is wrong. Here is the code:

``````struct Vector3
{

float x;
float y;
float z;

};

struct tBoundingBox
{
Vector3    max;
Vector3    min;
};

const int cubes = 2;

tBoundingBox boxArray[cubes];

int currentArray;

void setupBox(float minx, float maxx, float miny, float maxy, float minz, float maxz)
{
boxArray[currentArray].min.x  =  minx;
boxArray[currentArray].max.x  =  maxx;
boxArray[currentArray].min.y  =  miny;
boxArray[currentArray].max.y  =  maxy;
boxArray[currentArray].min.z  =  minz;
boxArray[currentArray].max.z  =  maxz;

if (currentArray == cubes)
{
currentArray = 0;
return;
}
else
{
currentArray++;
return;
}
}``````

What is wrong? The code is called twice per frame when the cubes are drawn. (2 cubes are drawn)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.