OpenGL rotation

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

Recommended Posts

consider the example of a cube at center (0,0,0) when i rotate this cube it rotate about its center i.e (0,0,0) correct. but when i changed the center to (0,0,1) and rotate, it doesnot rotate about its center (0,0,1) but it rotate about (0,0,0). so what i want is that, the cube is any where on the screen it rotate about its center i mean when cube is at center(0,0,1) then it rotate at center(0,0,1) when cube is at center (2,3,4) then it rotate at center(2,3,4).i m using C# with OpenGL.

Share on other sites
Translate the cube to the origin, perform the rotation, then translate back. OpenGL transforms are applied in reverse relative to the code, so what you want might look something like this:

glTranslate(x, y, z);
glRotate(/*whatever*/);
glTranslate(-x, -y, -z);
RenderCube();

Share on other sites
Translating after rotating will move you relative to the rotation. The only way to counter this is to apply the operations in the reverse order after rendering the cube, or using the matrix stack to remove the need all together, like this:
glPushMatrix();//move to the center of the cubeglTranslatef(0.0f,0.0f,1.0f);//rotate the cubeglRotatef(xRot,1.0f,0.0f,0.0f);glRotatef(yRot,0.0f,1.0f,0.0f);glRotatef(zRot,0.0f,0.0f,1.0f);//draw the cube...glPopMatrix();

Share on other sites
Both of our solutions are correct, depending on what the OP means by 'changing the center'.

@the OP: If you are moving your cube by actually changing the position of the corners, you should use the code I posted. But if your cube corners are defined relative to the origin and you are simply repositioning via translation, use gorax's code.

I assumed the former because we often get that question here. However, the latter is the preferred way to handle object transformations.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 15
• 11
• 11
• Forum Statistics

• Total Topics
634149
• Total Posts
3015832
×

Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!