1
\$\begingroup\$

I'm using OpenGL and people suggest using glRotate for sprite rotation, but I find that strange. My problem with it is that it rotates the whole matrix, which sort of screws up all my collision detection and so on and so forth. Imagine I had a sprite at position (100, 100) and in position (100, 200) is an obstacle and the sprite's facing it. I rotate the sprite away from the obstacle and when move upwards my y axis, even though the projection shows like it's going away from the obstacle, the sprite will intersect it. So I don't see another way of a rotating a sprite and not screwing up all collision detection other than doing mathematical operations on the image itself. Am I right or am I missing something?

\$\endgroup\$
  • 2
    \$\begingroup\$ I think it's hard to imagine what you are talking about, I think you need to upload a screenshot. \$\endgroup\$ – concept3d Oct 23 '13 at 16:45
3
\$\begingroup\$

It sounds like you're just getting the order of your matrix operations wrong. Assuming you have a circular collision volume you just want to make sure you rotate your sprite on it's centre axis. To do that you have to first have your sprite centred in the screen either by translating it to the centre or drawing it about the centre. Then you rotate it before you translate it to it's current location.

The only trick is that in opengl does operations in column-major order, which means you apply them in reverse order in code.

so it would be:

glPushMatrix();
glTranslatef(...); // lastly translating
glRotatef(...); // second you would rotate
glScalef(...); // if you had a scaling you would apply that first
// sprite drawn about centre
glPopMatrix();

This is just a quick stab, if you're using per pixel collision then it's a whole other problem.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.