# Collision detection

Hi!

If I have e.g one ball and three boxes moving around in an OpenGL application, I’d like to detect when the ball collides with the various boxes.

I would like to know which box the ball has collided into, and perform some actions based on this.

Can anyone guide me to a nice tutorial on this? Or maybe you can even give me a code snippet?

Thanks!!

Best regards,
Torbjørn

I think I have gotten a bit further by reading lesson 30 at nehe.gamedev.net.

My application makes it possible to actually just use speres. For each frame I can check if the distance between e.g. two balls is smaller than radiusBall_1+radiusBall_2.

I guess I can use this function:
GLint slices,
GLint stacks )

for drawing the spheres, but how can I check the position of the spheres at each frame?

Is there some function like

So: How can I check the position of the sphere?

Thanks!

Torbjørn

you need to keep track of the balls position yourself. maybe make some kind of structure like this:

typedef struct
{
float posx, posy, posz;
}object_t;

and have one instance of object_t for each sphere. when you move the sphere’s, change the values in the struct. then to draw them, translate to the position. that way you’ll know where all the balls are at any given time.

b

Maybe you should go back through the lesson’s 1 to 30…

You would see how to keep track of an object, via variables.

example:

GLfloat object; create two objects with 3 variables.

object = x data object 1
object = y data
object = z data

object = x data object 2
object = y data
object = z data

// moving things around.
// First object, note when accessing a array, it starts at 0 not 1.
glPushMatrix();
glTranslatef( object, object, object); // move our object
gluSphere( …); // add relivent data for sphere.
glPopMatrix();

// Draw second sphere
glPushMatrix();
glTranslatef( object, object, object); // move our object
gluSphere( …); // add relivent data for sphere.
glPopMatrix();

Originally posted by torbjorn:
[b]I think I have gotten a bit further by reading lesson 30 at nehe.gamedev.net.

My application makes it possible to actually just use speres. For each frame I can check if the distance between e.g. two balls is smaller than radiusBall_1+radiusBall_2.

I guess I can use this function:
GLint slices,
GLint stacks )

for drawing the spheres, but how can I check the position of the spheres at each frame?

Is there some function like

So: How can I check the position of the sphere?

Thanks!

Torbjørn[/b]

Thanks guys!

I guess I should run through all of the nehe-lessons Anyway, I think I’ll be able to solve my task now!

Thanks!

Torbjørn