# What is causing this problem with my object rotation?

I'm having an issue here with rotation in OpenGL. Before I changed my rendering function the rotation I had for my object worked fine, but now it seems to be messed up.

I changed my rendering function because I wanted to be able to clip the image (render frames of the image), whereas before I was just rendering things on a 1 object, 1 image basis.

The rendering is fine, just the rotation to clarify that. Here's my current rendering code:

void DrawRotateAdv(int x, int y, int width, int height, float sourceX, float sourceY, float imageWidth, float imageHeight,
GLuint texture, float angle, bool blendFlag)
{
glTranslatef((GLfloat) x + (width / 2),  (GLfloat) y + (height / 2), 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);

if (blendFlag)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

glBindTexture(GL_TEXTURE_2D, texture);
float texscaleX = 1.0f / (float)(imageWidth / width);
float texscaleY = 1.0f / (float)(imageHeight / height);
sourceX = sourceX / (float)imageWidth;
sourceY = sourceY / (float)imageHeight;

glBegin(GL_QUADS);

// Top-left vertex (corner)
glTexCoord2f(sourceX, sourceY);
glVertex2i( (-width / 2), (-height / 2));

//Bottom-left vertex (corner)
glTexCoord2f(sourceX, sourceY + texscaleY);
glVertex2i( (width / 2), (-height / 2));

//Bottom-right vertex (corner)
glTexCoord2f(sourceX + texscaleX, sourceY + texscaleY);
glVertex2i( (width / 2), (height / 2));

//Top-right vertex (corner)
glTexCoord2f( sourceX + texscaleX, sourceY);
glVertex2i( (-width / 2) , (height / 2));

glEnd();
glLoadIdentity();
}


I think that perhaps my translation is wrong? Unsure though, guess that's a stab in the dark on my part. As for the getting the angle:

     angle = atan2((cEnemy->position.y - (float)position.y), cEnemy->position.x -        (float)position.x) / 3.14159265f * 180;


But I don't think that's the issue, like I said it worked before-hand perfectly. Screenshot of the output:

• Describing things as "messed up" doesn't really help narrow down what the problem is. Can you add a screenshot of the bad rendering output? – Nathan Reed Oct 26 '11 at 23:06
• Yeah, sorry about that. Its hard to describe... – danran Oct 26 '11 at 23:37
• Added, to say that the object should be facing the enemy (green virus) – danran Oct 26 '11 at 23:45
• Sorry, it's still not clear what the problem is. Can you add an arrow or something to point it out, or a picture of desired result vs actual result? – Nathan Reed Oct 27 '11 at 0:33
• It's facing the angle of the green line, whereas before i changed my rendering function it would face the blue line (the angle i want it to). But like i said, before i changed my rendering function everything was fine. Hence why i'm leaning more so to the problem being with the rendering code... – danran Oct 27 '11 at 11:57

## 1 Answer

So it looks like the rotation is off by 90 degrees. This could be caused by your tex coords / vert coords being mismatched, which it looks like they might be. As a test, set the angle to zero; the object should point along the positive X axis (to the right, ordinarily).

Looking at just the first couple of verts,

// Top-left vertex (corner)
glTexCoord2f(sourceX, sourceY);
glVertex2i( (-width / 2), (-height / 2));

//Bottom-left vertex (corner)
glTexCoord2f(sourceX, sourceY + texscaleY);
glVertex2i( (width / 2), (-height / 2));


It doesn't make sense that when you go from top-left to bottom-left, you're changing the Y part of the texture coordinates but the X part of the vertex coordinates. Shouldn't you change the Y part of both? And similarly for the other verts.