Public Group

# 3rd person camera rotation translation problem

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

## Recommended Posts

Hi, I'm creating a 3rd person camera which should follow an aircraft over a plane. The problem is that when I make the rotation of the camera follow the rotation of my aircraft the camera follows my aircraft fine in the rotation but stops following the aircraft when i start translating. If I make the rotation of my aircraft follow the rotation of my camera, the translation works fine and my camera follows my aircraft, but when I start rotation, it stops following my aircraft... So I'm completely clueless why this is happening.

An another thing is that when I make the rotation of the camera follow the aircraft I have to use the airplanes position and rotation to show effect, else it wont move. And if i make the rotation of the aircraft follow the camera, then I have to use the rotation and position of the camera, else it wont move either. Thanks in advance. Here is the code:

 // camera.cpp void Camera::updateMatrix(vector3 Position, vector3 Rotation) { m_Position = Position; m_Rotation = Rotation; m_WorldMatrix.Init(m_Position, m_Rotation.x, m_Rotation.y, m_Rotation.z); m_ViewMatrix = m_WorldMatrix; m_ViewMatrix.Invert(); } void Camera::lookAt(vector3 eye, vector3 target, vector3 up) { vector3 zaxis = (target - eye); // The "look-at" vector. zaxis.Normalize(); vector3 xaxis = up.Cross(zaxis);// The "right" vector. xaxis.Normalize(); vector3 yaxis = zaxis.Cross(xaxis); // The "up" vector. m_WorldMatrix.cell[0] = xaxis.x, m_WorldMatrix.cell[1] = yaxis.x, m_WorldMatrix.cell[2] = zaxis.x, m_WorldMatrix.cell[4] = xaxis.y, m_WorldMatrix.cell[5] = yaxis.y, m_WorldMatrix.cell[6] = zaxis.y, m_WorldMatrix.cell[8] = xaxis.z, m_WorldMatrix.cell[9] = yaxis.z, m_WorldMatrix.cell[10] = zaxis.z; m_ViewMatrix = m_WorldMatrix; m_ViewMatrix.Invert(); } // game.cpp update function void Game::Tick( float a_DT ) { m_Screen->Clear( 0 ); if(GetAsyncKeyState(VK_LEFT)) { camera->m_Position.x -= 0.1f; } if(GetAsyncKeyState(VK_RIGHT)) { camera->m_Position.x += 0.1f; } if(GetAsyncKeyState(VK_UP)) { camera->m_Position.z += 0.1f; } if(GetAsyncKeyState(VK_DOWN)) { camera->m_Position.z -= 0.1f; } if(GetAsyncKeyState(0x57)) // w move upwards { camera->m_Position.y += 0.1f; } if(GetAsyncKeyState(0x53)) // s move downwards { camera->m_Position.y -= 0.1f; } // rotation in the yaw, pitch, roll if(GetAsyncKeyState(0x41)) // a (roll angle around z) { camera->m_Rotation.z += 0.5f; } if(GetAsyncKeyState(0x44)) // d (roll angle around z) { camera->m_Rotation.z -= 0.5f; } if(GetAsyncKeyState(0x46)) // f (yaw angle around x) { //camera->m_Rotation.x += 0.5f; airplaneObject->rotation.y += 0.5f; } camera->m_Rotation = airplaneObject->rotation; // airplaneObject->rotation = camera->m_Rotation; // camera->m_Position = airplaneObject->m_Matrix.Transform ( vector3 ( 0, 0, -1.5f ) ); airplaneObject->position = camera->m_WorldMatrix.Transform(vector3(0,0, 1.5f)); camera->lookAt(camera->m_Position, airplaneObject->position, vector3(0,1,0)); camera->updateMatrix(camera->m_Position, camera->m_Rotation); } 

Now translation works fine but rotation doesn't. If I were to switch between:

camera->m_Rotation = airplaneObject->rotation;
// airplaneObject->rotation = camera->m_Rotation;

// camera->m_Position = airplaneObject->m_Matrix.Transform ( vector3 ( 0, 0, -1.5f ) );
airplaneObject->position = camera->m_WorldMatrix.Transform(vector3(0,0, 1.5f));

And this:

// camera->m_Rotation = airplaneObject->rotation;
airplaneObject->rotation = camera->m_Rotation;

camera->m_Position = airplaneObject->m_Matrix.Transform ( vector3 ( 0, 0, -1.5f ) );
// airplaneObject->position = camera->m_WorldMatrix.Transform(vector3(0,0, 1.5f));

Now the rotation would work fine, but the translation would not. Edited by Veer.

##### Share on other sites
You can close this topic. I fixed the problem. I shouldn't use the transform matrix for the offset. because it was giving me a delay.

all I had to do was

airplaneObject->rotation = camera->m_Rotation;
airplaneObject->position = camera->m_Position + vector3 ( 0, 0, 1.5f );

And now it works just fine. Thanks anyway. Edited by Veer.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013394
×