1
\$\begingroup\$

I have a rotating object, a cube, which I rotate in OpenGL as follows:

gl.glPushMatrix();
gl.glTranslatef(400.0f, 300.0f, 1300.0f);
gl.glRotatef(m_x, 4.0f, 0.0f, 0.0f);
gl.glRotatef(m_y, 0.0f, 4.0f, 0.0f);    
gl.glRotatef(m_z, 0.0f, 0.0f, 42.0f);
gl.glCallList(++shapeNumber); // cube 2
gl.glPopMatrix();

(m_x, m_y, and m_z are the change in the rotation each frame.)

I want to detect a collision between the camera and that rotating object, and to do that I need to get the coordinates of the rotated object in real time. How can I accomplish that?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hi, you should avoid legacy OpenGL \$\endgroup\$
    – elect
    Aug 13, 2015 at 12:03

1 Answer 1

2
\$\begingroup\$

OpenGL's glTranslate and glRotate methods don't actually perform translation or rotations on any objects. Instead, they compute transformation matrices and apply those matrices to OpenGL's matrix stack, which in turn yields a final transformation matrix that is applied to the object vertices by the GPU when a frame is actually rendered.

In order to compute the coordinates of an object after the application of any transformation, then, you're going to have to do it yourself on the CPU. This involves the construction of an equivalent transformation matrix for your combined rotation/translation operation on the CPU (most game libraries and frameworks have "matrix" classes, such as this one from LWJGL, or you can roll your own; the math behind the various matrix operations is well-documented).

Once you have the matrix, multiply it by all the original vertices of your object to yield the transformed vertices and perform your collision detection on that resulting object (in general one simplifies this process to only transform the bounding cube or similar low-resolution collision mesh, but since you are already using cubes in your specific example you can continue to use them directly).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .