Sign in to follow this  
v.mommersteeg

3rd person camera rotation translation problem

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:


[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);
}
[/CODE]

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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this