View Full Version : Collision Detection

James Lovejoy

05-19-2011, 01:28 PM

Hi,

My entire OpenGL world is built with cubes. Therefore, I decided to use bounding boxes for my collisions. I already have a procedure that returns whether there is a collision between your current position and any of the bounding boxes. It returns either 1 (collision on x), 2 (collision on y), 3 (collision on z) or 0 (no collisions).

My update camera procedure looks like this:

void updateCamera()

{

glRotatef(xrot,1.0,0.0,0.0); //rotate our camera on the x-axis (left and right)

glRotatef(yrot,0.0,1.0,0.0); //rotate our camera on the y-axis (up and down)

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

}

What I want to know is how I can translate the information provided by my collision procedure to stop the camera flying through things?

Thanks,

James

dorbie

05-20-2011, 01:04 AM

This can be a black art due to timestep & numerical issues but one typical way is to move the eye to the point of the collision minus some distance whence it traveled. Then take the residual vector and dot product it with the collision normal. Subtract the dot product * normal from the remaining motion vector and add that motion to the collision eye position (you may have to switch a sign or two).

The result will be the eye sliding along the collision surface.

For complete robustness you need some kind of volume collision that is compatible with your typical velocities otherwise you risk sticking at corners or penetrating cracks depending on your implementation.

James Lovejoy

05-20-2011, 11:59 AM

Hi,

Thanks for the reply. I'm afraid you lost me. Could you give me some example code (c++) or point me to a tutorial?

James Lovejoy

05-22-2011, 05:54 AM

Anyone?

hana09

05-28-2011, 10:08 AM

hello OPENGL users;

I need a help if possible. I am a beginner to use OPENGL and would like to develope collision detection to my application. I know that opengl does not support collision detection, I went over internet but all what I found is hard and not sure which algorithm is better with notice that the objects that I need to detect collision for one of them is not convex shape and other one is a sphere.

can you please, provide me with any helpful links, materials?

Thank you in advance.

NeuroFuzzy

05-29-2011, 12:09 AM

@hana, you might want to use an external collision library. I haven't used one, but google (http://www.google.com/search?q=collision+detection&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:unofficial&client=firefox-a#sclient=psy&hl=en&client=firefox-a&hs=7JF&rls=org.mozilla:en-US%3Aunofficial&source=hp&q=collision+detection+li brary&aq=f&aqi=g1g-v3g-j1&aql=&oq=&pbx=1&bav=on.2,or.r_gc.r_pw.&fp=f17ae7 72ca1ea813&biw=1323&bih=607) might turn up some results.

@OP

So... your collision function... do you enter a radius of collision?

Anyways, for the camera's position in 3d space, take the rightmost column of the matrix found by:

glTranslated(xpos,ypos,zpos);

glRotatef(-yrot,0.0,1.0,0.0);

glRotatef(-xrot,1.0,0.0,0.0);

(just the inverse of the view matrix here)

Then, do your collision function, so check if your camera is near a wall. if it's too close, find the face's normal, and push the camera out in the direction of the normal, by an amount either found in your calculation of the collision, arbitrarily selected.

sooo take the rightmost vector of the above matrix, do your collision check, push the rightmost vector away from the wall, and then set that vector to be your camera's position.

[edit]

ALSO you can do what dorbie said, and mess with the velocity,but I prefer not to get fancy xD

The intended velocity, plus the change in movement from the collision code, will approximately (or exactly if you do it right) equal the overall velocity as per described in dorbie's post (the correct velocity)

Dark Photon

05-29-2011, 01:49 PM

I am a beginner to use OPENGL and would like to develope collision detection to my application. I know that opengl does not support collision detection, I went over internet but all what I found is hard and not sure which algorithm is better with notice that the objects that I need to detect collision for one of them is not convex shape and other one is a sphere.

Check out Bullet (http://bulletphysics.org/wordpress/).

Free download (http://code.google.com/p/bullet/downloads/list), build the examples, and try. Supports OpenGL and is very capable. Some big companies now using it.

Also, for a quick overview of some basic physics/collision detection concepts, take a look at the SIGGRAPH 2010 course slides on this:

* Recent Advances in Real-Time Collision a... Course Slides) (http://sglab.kaist.ac.kr/~sungeui/Collision_tutorial/)

In particular, check out the Yoon 3pm, Young 2:30pm, and Coumans 4:15pm presentations if you're pressed for time. That said, what time you spend up-front getting your head inside the problem domain will save you big later.

James Lovejoy

06-01-2011, 04:50 PM

Hi,

I kind of understand your solution... would this also work with colliding with the floor and if so, how would I use this to impliment basic gravity.

James

DHeshMan

03-07-2012, 07:02 PM

James,

Since I'm new at this as well, this may or may not help. Instead of rotating and translating your world around your camera, you might want to consider leaving the world stationary and moving your camera within your world.

By using the gluLookAt(eye_x, eye_y, eye_z, lookat_x, lookat_y, lookat_z, up_x, up_y, up_z) command, you can explicitly specify your camera's eye position and if it is about to collide with a box (or be inside a box) you can modify it's position before redrawing the screen.

I prefer to think of my world in world coordinates and allow objects to move within that frame of reference (including my camera).

void RenderScene(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

gluLookAt(Eye_x, Eye_y, Eye_z, At_x, At_y, At_z, 0, 0, 1); // assuming Z-direction is up

glPushMatrix();

Draw_World();

glPopMatrix();

glutSwapBuffers();

}

Regards

Dave

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.