• 14
• 12
• 9
• 10
• 13

# Rotation on the z-axis, off center

This topic is 2808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to make an easy-to-use rendering system, and for the most part, it works. As I'm implementing physics (linear and rotational) and I need it to be able to move as well as rotate shapes (around the shapes center). But I have found that if I rotate a shape off the center of the window (as in 0, 0 for my settings) it swings around in a big circle instead of rotating around it's center. I know that if I use glTranslatef to set the default coordinates to the shapes position and then rotate it, but I want to know if there's an easier way for me to rotate a shape around the z-axis off center. I'm just looking for an alternative, although the glTranslatef method will be easy to implement.

In a nut shell, is there an easy way to rotate a shape around it's center without translating the whole scene

Edit:
By the way, this is 2D

[Edited by - AGlass0fMilk on July 14, 2010 8:45:17 PM]

##### Share on other sites
Sounds like you're translating and then rotating. You should do it the other way (and yes, remember that in OpenGL transformations are specified in reverse order).

This should explain my point:

EDIT: I assume the base coordinates for your sphere without transformations is [0,0,0], right? Otherwise, that explains it - make sure the sphere's center is there, apply its transformations, then translate it to its final position.

##### Share on other sites
That does look like what it's doing, I'll post the render code:

	float red = toRGBf(color.colR); //Those are custom color functions	float green = toRGBf(color.colG);	float blue = toRGBf(color.colB);	float alpha = toRGBf(color.colA);	glPushMatrix();	if(rotation < 0.0f) //I like clean 0-360 degrees, not Rotation = 3535		rotation = 360.0f;	if(rotation > 360.0f)		rotation = 0.0f;	glRotatef(rotation, 0.0f, 0.0f, 1.0f);	glBegin(GL_QUADS);		glColor4f(red, green, blue, alpha);		glVertex3f(Pos.x, Pos.y + width, 0.0f);		glColor4f(red, green, blue, alpha);		glVertex3f(Pos.x + length, Pos.y + width, 0.0f);		glColor4f(red, green, blue, alpha);		glVertex3f(Pos.x + length, Pos.y, 0.0f);		glColor4f(red, green, blue, alpha);		glVertex3f(Pos.x, Pos.y, 0.0f);	glEnd();	glPopMatrix();

I do not translate the scene to accommodate the shapes set position, instead, I keep the grid as it is and then draw the shape at the set position using it's length and width variables as well

##### Share on other sites
Yes, you're translating it: Pos.x and Pos.y have that translation. Instead you need to use those values with glTranslatef(), calling it before glRotatef(). While passing the vertex positions, replace Pos.x and Pos.y with 0 instead.

EDIT: and by the way, bear into mind that you'll be rotating along one of its corners, not around its center.