# very weird collision detection problem in OpenGL

This topic is 5457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, trying to do some 3d collision detection in opengl. I am working with all cubes, and i am only getting collision on the plane parallel to the X axis on the z coord of the main point. Heres the code: bool Collision(GLfloat ulfx,GLfloat ulfy,GLfloat ulfz, GLfloat xsz,GLfloat ysz,GLfloat zsz, GLFrame cam) { GLfloat coord1[3] = {ulfx, ulfy, ulfz}; GLfloat coord2[3] = {ulfx, ulfy, ulfz + zsz}; GLfloat coord3[3] = {ulfx + xsz, ulfy, ulfz +zsz}; GLfloat coord4[3] = {ulfx+xsz, ulfy, ulfz}; GLfloat coord5[3] = {ulfx, ulfy+ysz, ulfz}; GLfloat coord6[3] = {ulfx, ulfy+ysz, ulfz+zsz}; GLfloat coord7[3] = {ulfx+ xsz, ulfy+ysz, ulfz+zsz}; GLfloat coord8[3] = {ulfx+xsz, ulfy+ysz, ulfz}; // check the z axis first, this is the only plane that // works properly if(cam.GetOriginZ() > coord1[2] && cam.GetOriginZ() < coord2[2]) { if(cam.GetOriginY() > coord1[1] && cam.GetOriginY() < coord5[1]) { if(cam.GetOriginX() > coord1[0] && cam.GetOriginX () < coord4[0]) return true; } } if(KEY_DOWN(VK_MENU)) int temp = 0; return false; } ok, that one set of if's is for the only working face of the cube. if i try even for the face behind the one that works, i get nothing. Anyone have any ideas as to what i can do? If i try for a line perpendicular to this one, disable the one that works, it still works on the same plane, even with the wrong coords...i dont get it...

##### Share on other sites
I'm a little puzzled at what you are trying to do... Are you trying to determine if a point(the camera) is inside of a cube?

##### Share on other sites
I'd think this would call for some debugging. Place your camera inside the cube, and then step through the collision function in your debugger. See if it's getting to the inner ifs, and if not, why not.

If you have any sort of console or text output system, you could add some debug prints in there, such as "Point passed z test", "Point passed y test", and so on. This would give you some real-time info as to what is going on.

Anyway, your code looks like it should work. It looks like you're specifying the minimum corner of your box, and a size in each direction, which I assume is what you mean to do.

##### Share on other sites
The reason why it is only working for one face is because you are only checking for one face.

You have a cube, lets say face 1 is in front of you and face 2 is the plane directly oposite face 1. Now in order to check collision you will need to see if you are inside the cube. Therefore being in behind face 1 and in front of face 2. That would put you inside the cube. At the moment you are checking if you are behind face 1 and behind face 2.

3D collision is not as simple as that because of of the z-axis.

I could be talking crap though. Perhaps I'm missing something. Take a look at this site:

http://www.realtimerendering.com/int/

##### Share on other sites
Quote:
 Original post by Mr_RiddThe reason why it is only working for one face is because you are only checking for one face. You have a cube, lets say face 1 is in front of you and face 2 is the plane directly oposite face 1. Now in order to check collision you will need to see if you are inside the cube. Therefore being in behind face 1 and in front of face 2. That would put you inside the cube. At the moment you are checking if you are behind face 1 and behind face 2.3D collision is not as simple as that because of of the z-axis. I could be talking crap though. Perhaps I'm missing something. Take a look at this site:http://www.realtimerendering.com/int/

The way i check if a point is in a cube( or any convex area ) is test if the points is behind all of the faces. Do a dotproduct with the point and all of the faces' normals if at one point the result is greater than zero the point is in front of one of the faces and thus outside the cube.

Paul

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 12
• 10
• 9
• 15
• 22