Skip to main content
gamedev.net
🔒 Locked

rotation

Started by soofian 2 replies 600+ views
Original Post
soofian
soofian
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.
Zakwayda
Zakwayda
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();
Gorax
Gorax
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();
Zakwayda
Zakwayda
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.

Topic Locked

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

Sign in to reply to this topic.