954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

OpenGL world to camera collisions

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

jamesl22
Light Poster
36 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

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

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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

jamesl22
Light Poster
36 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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)

jamesl22
Light Poster
36 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: