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

Try:
http://nehe.gamedev.net/tutorials/lesson.asp?l=30

Rob.

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:
void gluSphere( GLUquadricObj *qobj,
GLdouble radius,
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
“glGetPosition(GLUquadricObj *qobj)” existing?

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[2][3]; create two objects with 3 variables.

object[0][0] = x data object 1
object[0][1] = y data
object[0][2] = z data

object[1][0] = x data object 2
object[1][1] = y data
object[1][2] = z data

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

// Draw second sphere
glPushMatrix();
glTranslatef( object[1][0], object[1][1], object[1][2]); // 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:
void gluSphere( GLUquadricObj *qobj,
GLdouble radius,
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
“glGetPosition(GLUquadricObj *qobj)” existing?

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