Jump to content
  • Advertisement
Sign in to follow this  

Collision Detection??

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have tried now for several days to get collision detection to work in my OpenGL game. Here is what I have: Level is loaded from 3ds using the 3ds loading tutorial from Nehe's site. This means that faces, vertex data and similar stuff is all saved to structures, which are then used in the RenderScene() function to draw the scene. Anyways... I have a character model that moves on the screen when I supply input. With this character model comes three vectors: one facing left to right, the second goes front to back, and the third goes above to below. I intended to use these vectors for detecting collisions with faces in the level, using another tutorial from GameTutorials.com (a line colliding with a triangle). But the problems are: 1) I cannot for the love of god store the face's vertex data into a 3d vector so I can compare it to either of the three lines. 2) I cannot seem to even compare the three lines to a static, self-generated, triangle in the world space (though I could compare that triangle to a static self-generated line, which worked just fine....). I am quite confused, and if someone has any input on this, please let me know. I am sure most of you have gotten collision detection to work somehow, and these tutorials I am speaking of should be no new news I guess :)

Share this post

Link to post
Share on other sites
I'm not quite sure I understand what you're asking... So could you try to explain a little more thorough?

Why can't you store vertex data as a vector? Why can't it be compared to the lines? How are you storing your vertex data?

I find it hard to visualize, or understand the problem you're having. Could you try and explain it again, but more thorough? To be honest, you do seem confused, and your confusing me as well. :)

Share this post

Link to post
Share on other sites
Wait now... nothing to do with OpenGL? Oookey.. I guess OpenGL does not really mean OpenGL then. Where was it moved??

Anyways, here is what my code looks like now. Have managed to rewrite it enough to actually get collision to work, but the problem now is that it only checks for collision for the first face/polygon in the scene, and skips the rest, which is pretty strange. I might be missing something vital here.

void CollisionDetection()
bCollFB = bCollLR = bCollAB = false;

// Calculate collision vectors.
vCollFB[0].x = g_fCharMoveX - 20.0f;
vCollFB[1].x = vCollFB[0].x + 40.0f;
vCollFB[0].y = 0.0f;//g_fCameraY;
vCollFB[1].y = vCollFB[0].y;
vCollFB[0].z = g_fCharMoveZ;
vCollFB[1].z = vCollFB[0].z;

vCollLR[0].x = g_fCharMoveX;
vCollLR[1].x = vCollLR[0].x;
vCollLR[0].y = 0.0f;
vCollLR[1].y = vCollLR[0].y;
vCollLR[0].z = g_fCharMoveZ - 20.0f;
vCollLR[1].z = vCollLR[0].z + 40.0f;

vCollAB[0].x = g_fCharMoveX;
vCollAB[1].x = vCollAB[0].x;
vCollAB[0].y = -20.0f;
vCollAB[1].y = vCollAB[0].y + 40.0f;
vCollAB[0].z = g_fCharMoveZ;
vCollAB[1].z = vCollAB[0].z;

// Make the graphical vector representations.
CVector3 vLine[6];
vLine[0].x = vCollFB[0].x;
vLine[0].y = vCollFB[0].y;
vLine[0].z = vCollFB[0].z;
vLine[1].x = vCollFB[1].x;
vLine[1].y = vCollFB[1].y;
vLine[1].z = vCollFB[1].z;
vLine[2].x = vCollLR[0].x;
vLine[2].y = vCollLR[0].y;
vLine[2].z = vCollLR[0].z;
vLine[3].x = vCollLR[1].x;
vLine[3].y = vCollLR[1].y;
vLine[3].z = vCollLR[1].z;
vLine[4].x = vCollAB[0].x;
vLine[4].y = vCollAB[0].y;
vLine[4].z = vCollAB[0].z;
vLine[5].x = vCollAB[1].x;
vLine[5].y = vCollAB[1].y;
vLine[5].z = vCollAB[1].z;

// Go through each object in the scene.
for(int i = 0; i < g_3DModel.numOfObjects; i++)
// Make sure we have valid objects just in case. (size() is in the vector class)
if(g_3DModel.pObject.size() <= 0) break;

// Get the current object that we are displaying
t3DObject *pObject = &g_3DModel.pObject;

// Go through each face and check for collision.
for (int j = 0; j < pObject->numOfFaces; j++)
// Get the face's vertices.
vTriangle[0] = pObject->pVerts[pObject->pFaces[j].vertIndex[0]];
vTriangle[1] = pObject->pVerts[pObject->pFaces[j].vertIndex[1]];
vTriangle[2] = pObject->pVerts[pObject->pFaces[j].vertIndex[2]];

// Render a blue triangle for representing the face collision area.
glColor3ub(64, 64, 64);
glVertex3f(vTriangle[0].x, vTriangle[0].y, vTriangle[0].z);
glVertex3f(vTriangle[1].x, vTriangle[1].y, vTriangle[1].z);
glVertex3f(vTriangle[2].x, vTriangle[2].y, vTriangle[2].z);
glColor3ub(255, 255, 255);

// If we have not already collided, check for collision with current face.
if (!bCollLR)
bCollLR = IntersectedPolygon(vTriangle, vCollLR, 3);

if (!bCollFB)
bCollFB = IntersectedPolygon(vTriangle, vCollFB, 3);

if (!bCollAB)
bCollAB = IntersectedPolygon(vTriangle, vCollAB, 3);

// Render the collision vectors. Use different colors if collision is occuring.
if (bCollFB)
glColor3ub(0, 255, 255);
glColor3ub(255, 0, 0);
glVertex3f(vLine[0].x, vLine[0].y, vLine[0].z);
glVertex3f(vLine[1].x, vLine[1].y, vLine[1].z);
if (bCollLR)
glColor3ub(255, 0, 255);
glColor3ub(0, 255, 0);
glVertex3f(vLine[2].x, vLine[2].y, vLine[2].z);
glVertex3f(vLine[3].x, vLine[3].y, vLine[3].z);
if (bCollAB)
glColor3ub(255, 255, 0);
glColor3ub(0, 0, 255);
glVertex3f(vLine[4].x, vLine[4].y, vLine[4].z);
glVertex3f(vLine[5].x, vLine[5].y, vLine[5].z);
glColor3ub(255, 255, 255);

// Reset the triangle vector array for the next face.
vTriangle[0].x = vTriangle[0].y = vTriangle[0].z = 0;
vTriangle[1].x = vTriangle[1].y = vTriangle[1].z = 0;
vTriangle[2].x = vTriangle[2].y = vTriangle[2].z = 0;

Share this post

Link to post
Share on other sites
Original post by azjerei
Wait now... nothing to do with OpenGL? Oookey.. I guess OpenGL does not really mean OpenGL then. Where was it moved??

The GL in OpenGL stands for graphics library. If it stood for collision detection library, it would be called OpenCDL...

Share this post

Link to post
Share on other sites
Well, it IS for OpenGL, so I would classify it at that. Nevermind... I hope someone can help me out. I am quite stumped as to why it does not seem to go through each face in each object (only the very first face in the scene counts).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!