# Ray-Model Collision Problem

Hello,

maybe someone can help me with this. I want to do ray-model collision detection. Obviously I have to consider the model’s transformations or else some rays won’t collide with the rotated model. When drawing the model I use this code to do transformations:

[i]glMatrixMode(GL_MODELVIEW); // Modeling transformation
glLoadIdentity(); // Initialize the model matrix as identity

glTranslatef(Position.X, Position.Y, Position.Z); // translation
glScalef(Scale, Scale, Scale); // scaling
// Rotation
glRotatef(Rotation.X, 1.0, 0.0, 0.0);
glRotatef(Rotation.Y, 0.0, 1.0, 0.0);
glRotatef(Rotation.Z, 0.0, 0.0, 1.0);[/i]

After that I use this to get the model view matrix:

glGetFloatv(GL_MODELVIEW_MATRIX, ModelMatrix);

In the collision test I use the retrieved matrix to transform the vertices:

[i]Vector3 v0 = Model.Vertex[Polygon[l_index].a];
Vector3 v1 = Model.Vertex[Polygon[l_index].b];
Vector3 v2 = Model.Vertex[Polygon[l_index].c];

v0 *= ModelMatrix;
v1 *= ModelMatrix;
v2 *= ModelMatrix;

…//collision detection[/i]

But somehow the scaling transformation screws it up. When I omit the scale transform the collision detection works fine, but I dont see the model anymore. I have to scale the model heavily (1% of the original size) to see it at all. But if I do this, only one ray hits the model, all others miss.

Does anybody have an idea what I might be doing wrong?
Do I apply the transformations in the wrong order?

Hi,

Since you are scaling with the same value for all three axes, the order of rotation and scaling does not matter here. But in most cases you will want to put the glScalef call after the glRotatef calls so that the scaling is applied to the model before the rotation.

Retrieving the GL_MODELVIEW_MATRIX results in a column-major matrix, but at first sight you seem to compensate for that by postmultiplying the vertices with the matrix instead of premultiplying. This depends on your matrix class implementation of course.

If you make the model smaller, it seems quite logical that fewer rays intersect with the model. Maybe you could try rendering the rays and/or intersection points to see whether you get the desired result.

FYI, collision detection is usually performed by transforming the rays from ray-space to model space rather than transforming from model space to ray-space. This can change a lot of computation time for models with static geometry.

Cheers,
Nico

Hi Nico,

Thanks for your help, I will switch the transformation as you suggested. I did in fact have problems with the performance.

It works now, it was a really stupid mistake. I retrieve the min- and max-values of the model to know in what area I have to send the rays. But I forgot to transform them into world/ray-space Thanks again,
Christian