Skip to main content
gamedev.net
🔒 Locked

Quad Rotation Problem

Started by archaos90 Apr 8, 2009 at 10:51 AM 2 replies 1.1k views
Original Post
archaos90
archaos90
I'm having problems with rotating a sprite in OpenGL. The graphics is set to 2D and I have my own coordinate system management (for several good reasons), which is why I am not translating. The problem is that while rotating, the very z-axis it rotates around itself seems to be located at (0,0). What I want is that it should rotate around the middle of the sprite, meaning it should only rotate and not move from the upper-right corner down to the lower-left corner and then continue rotating outside of the screen the rest of the 270 degrees. Here's the OpenGL code:

glBindTexture(GL_TEXTURE_2D, *texture->getGLTexture());
glPushMatrix();
glRotatef(angle, 0, 0, 1);   
   
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex3f(x1, y1, 0.0f);
	
glTexCoord2i(1, 0);
glVertex3f(x2, y1, 0);
	
glTexCoord2i(1, 1);
glVertex3f(x2, y2, 0);
	
glTexCoord2i(0, 1);
glVertex3f(x1, y2, 0);
glEnd();        
          
glPopMatrix();   

if(angle < 360)
{
         angle++;
}
else
{
         angle = 0;
}
Help would be appreciated.
CadetUmfer
CadetUmfer
Try this:
glBindTexture(GL_TEXTURE_2D, *texture->getGLTexture());glPushMatrix();GLfloat halfw = (x1-x0)/2;GLfloat halfh = (y1-y0)/2;glTranslatef(halfw, halfh, 0);glRotatef(angle, 0, 0, 1);glBegin(GL_QUADS);    glTexCoord2i(0, 0);    glVertex3f(x1-halfw, y1-halfh, 0);    glTexCoord2i(1, 0);    glVertex3f(x2+halfw, y1-halfh, 0);    glTexCoord2i(1, 1);    glVertex3f(x2+halfw, y2+halfh, 0);    glTexCoord2i(0, 1);    glVertex3f(x1-halfw, y2+halfh, 0);glEnd();        glPopMatrix();   if(angle < 360) {    ++angle;} else {    angle = 0;}


Translate by half the width and height before you rotate, then adjust for this when you send the vertices.
Anthony Umfer
archaos90
archaos90
Doesn't work. All that happened when I did as you said was that the sprites got scaled by a factor of 0.5.
zedz
zedz
glPushMatrix();

glTraslate( center of sprite )
glRotatef(angle, 0, 0, 1);



glBegin(GL_QUADS);

glTexCoord2i(0, 0);

glVertex3f(-half sprite size, -half sprite size, 0.0f);
lVertex3f(half sprite size, -half sprite size, 0.0f);

etc

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.