Whatever8596235

12-12-2007, 08:12 AM

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:

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);

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:

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

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?

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:

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);

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:

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

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?