Rotating object around own center?

Hello. I’m having a big problem trying to make a car rotate, the big problem, is cause the car apears to be stuck in the center ‘world-axis’ (0,0,0). So, I saw a code that rotates and moves a camera around the world just the way I need… But i dont understood how it works… So, if anyone can post a code for a newbie like me…
(I just wanna make a car that runs in the scene)

Here’s the code that ‘rotates the camera around it’s own center’ or something like this ohhh! and sorry for the poor english! hehehe!

void CCamera::RotateView(float angle, float x, float y, float z)
{
CVector3 vNewView;
CVector3 vView;
vView.x = m_vView.x - m_vPosition.x; // This gets the direction of the X
vView.y = m_vView.y - m_vPosition.y; // This gets the direction of the Y
vView.z = m_vView.z - m_vPosition.z; // This gets the direction of the Z
float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);
vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vView.x;
vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta) * vView.y;
vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta) * vView.z;
vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.x;
vNewView.y += (cosTheta + (1 - cosTheta) * y * y) * vView.y;
vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta) * vView.z;
vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.x;
vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta) * vView.y;
vNewView.z += (cosTheta + (1 - cosTheta) * z * z) * vView.z;
m_vView.x = m_vPosition.x + vNewView.x;
m_vView.y = m_vPosition.y + vNewView.y;
m_vView.z = m_vPosition.z + vNewView.z;
}

Look at the functions glRotate and gluLookAt. They may do what you want.

To get an object to rotate about the center you essentially need to translate back to the center, then rotate the object and finally translate it back to where it was before you did anything.In the code you provided us, the camera is being altered. Therefore the location and direction that YOU are looking is being changed. So the WHOLE scene would be rotated. What you want to do to just rotate the car is apply linear trasformations to it. Lets say that T(x,y,z) was translation by the vector transpose([x y z]), and the Rotation transform about the y axis was defined as Ry(angle), then your resulting transform would X would be this:

T(x,y,z)*Ry(angle)*T(-x,-y,-z) = X

Note: the (x, y, z) is the current coordinates of the object before the transfomrations, so if you to T(-x,y,z) you bring it back to the origin.

(i’m not 100% positive if i got the two translations in the right place or if they are flipped around…can someone check that for me?)

The T(x,y,z) and R(angle) functions represent matrices. There is a good tutorial on these in the articles section on NeHe .

In OpenGL code, you should be able to do this:

glTranslatef(-x,y,-z);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glTranslatef(x, y, z);

  • Halcyon