OpenGL rotating and translating issue

In my project, i have a guy which is a square in the center. i have him moving around, and he can move towards wherever the mouse is, the thing is, the red square isn't showing up where it should be after rotating it. This is a 2d game, which is why i am rotating on the z axis

This is what i have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        glPushMatrix();
	  glRotatef(angle, 0, 0, 1);
	  glTranslatef(guyX, guyY, 0);
	  glColor4ub(255, 0, 0, 255);
	  glBegin(GL_QUADS);

	  glVertex2f(guyX - guyW, guyY - guyH);
	  glVertex2f(guyX + guyW, guyY - guyH);
	  glVertex2f(guyX + guyW, guyY + guyH);
                  glVertex2f(guyX - guyW, guyY + guyH);

                  //guyW and guyH are the width and height

	  glEnd();
	  glPopMatrix();


any ideas?
Last edited on
I would have a closer look at the order of the rotates and translates.
1
2
3
4
5
6
7
8
	  glTranslatef(guyX, guyY, 0);  //  <- you're moving the guy here
	  glColor4ub(255, 0, 0, 255);
	  glBegin(GL_QUADS);

	  glVertex2f(guyX - guyW, guyY - guyH);  // <- and doing it again here
	  glVertex2f(guyX + guyW, guyY - guyH);
	  glVertex2f(guyX + guyW, guyY + guyH);
                  glVertex2f(guyX - guyW, guyY + guyH);


The consequence of moving it a second time will sckew where he's being drawn.

Do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
    glPushMatrix();
    glRotatef(angle, 0, 0, 1);  // <- A
    glTranslatef(guyX, guyY, 0);  // <- B
    glColor4ub(255, 0, 0, 255);
    glBegin(GL_QUADS);

    glVertex2f(-guyW, -guyH);
    glVertex2f( guyW, -guyH);
    glVertex2f( guyW,  guyH);
    glVertex2f(-guyW,  guyH);

    glEnd();
    glPopMatrix();


What jpphelan is suggesting is flipping around 'A' and 'B' lines so that you translate first, then rotate. Which you do first will make a big difference on the result. I *think* you are doing it right (rotate first), but try swapping it around to see if that's wrong.
wow i feel like an idot now....... jpphelan was right. It works. THanks so much for helping me, it was really buigging me that i couldn't figure it out.
So translate first, then rotate? Weird, I could've swore it was the other way around.

Actually, now that I think about it more, yeah, that makes sense. I guess I get that mixed up =x

Oh well, it's working now, so congrats. =)
The whole rotate and translate thing can be pretty weird for opengl. Spent weeks and weeks with problems like these :P This can be closed I guess.
Topic archived. No new replies allowed.