Skip to main content
gamedev.net
🔒 Locked

Camera Problem

Started by pebben Jun 7, 2004 at 5:55 PM 3 replies 750+ views
Original Post
pebben
pebben
Hi There Programmers! I have created a camera that looks at the player, like Tomb Raider you know. I use the D3DXMatrixLookAtLH function, and it works fine to look at the player, but the problem is that it doesn´t seem to move right? What is wrong in my code? Please take a look. D3DXMATRIX m_PlayerMatToWorld; m_PlayerMatToWorld = ((TransformNode*)g_GameManager->NodePlayer)->m_toWorld; //Get the Player´s Position. (Don´t care about the type casting) D3DXVECTOR3 vLookAt; vLookAt.x = m_PlayerMatToWorld(3,0); //a D3DXVECTOR3 with Player´s Position. vLookAt.y = m_PlayerMatToWorld(3,1); vLookAt.z = m_PlayerMatToWorld(3,2); D3DXVECTOR3 vMoveCamBack; //The Cam are placed some meters behind the player. D3DXVec3TransformNormal(&vMoveCamBack, &D3DXVECTOR3(0.0f, 40.f, -200.0f), &m_CameraMatToWorld); if (FirstTimeUpdateMatrices == true) //To Get A good starting points for the lerp function. { FinalPosLerpCamera = vMoveCamBack; FirstTimeUpdateMatrices = false; } else //Now the Nice camera lerp can start. { D3DXVECTOR3 vGoalPos; vGoalPos.x = vMoveCamBack.x; vGoalPos.y = vMoveCamBack.y; vGoalPos.z = vMoveCamBack.z; //GoalPos == The Place the camera is lerping to. D3DXVec3Lerp( &FinalPosLerpCamera, &FinalPosLerpCamera, &vGoalPos, 0.003 * elapsedMs); } D3DXMatrixLookAtLH(&m_matFromWorld, &FinalPosLerpCamera, &vLookAt, &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //And Finally create the matrix. m_pDevice->SetTransform( D3DTS_VIEW, &m_matFromWorld ); /Have A Nice Day -Patrick [edited by - pebben on June 7, 2004 6:58:01 PM]
/Have A Nice Day -Patrick
Palidine
Palidine
please define: "it doesn´t seem to move right"

-me
pebben
pebben
The translation is wrong, it doesn´t move along with the player. It´s just looking at the player.
/Have A Nice Day -Patrick
Palidine
Palidine
i don't know DirectX but it looks like you are moving the camera relative to the camera:


m_PlayerMatToWorld = ((TransformNode*)g_GameManager->NodePlayer)->m_toWorld; //Get the Player´s Position. (Don´t care about the type casting)


<snip...>

//notice that here you are doing something with m_CameraMatToWorld

//not with m_PlayerMatToWorld as you probably should be doing

D3DXVECTOR3 vMoveCamBack; //The Cam are placed some meters behind the player.

D3DXVec3TransformNormal(&vMoveCamBack, &D3DXVECTOR3(0.0f, 40.f, -200.0f), &m_CameraMatToWorld);


in other words you are never doing anything with vMoveCamBack to make it relative to the player position.

-me

[edited by - Palidine on June 7, 2004 7:20:08 PM]
pebben
pebben
Thanx For your opinion, it solved my problem. But how could I miss it? (feel stupid) Here is the working code for anyone who wants to borrow it.

D3DXMATRIX m_PlayerMatToWorld;
m_PlayerMatToWorld = ((TransformNode*)g_GameManager->NodePlayer)->m_toWorld; //Get the Player´s Position. (Don´t care about the type casting)

D3DXVECTOR3 vLookAt;
vLookAt.x = m_PlayerMatToWorld(3,0); //a D3DXVECTOR3 with Player´s Position.
vLookAt.y = m_PlayerMatToWorld(3,1);
vLookAt.z = m_PlayerMatToWorld(3,2);

D3DXVECTOR3 vMoveCamBack; //The Cam are placed some meters behind the player.
D3DXVec3TransformCoord(&vMoveCamBack, &D3DXVECTOR3(0.0f, 40.f, -200.0f), &((TransformNode*)g_GameManager->NodePlayer)->m_Combined);

if (FirstTimeUpdateMatrices == true) //To Get A good starting points for the lerp function.
{
FinalPosLerpCamera = vMoveCamBack;
FirstTimeUpdateMatrices = false;
}
else //Now the Nice camera lerp can start.
{
D3DXVECTOR3 vGoalPos; vGoalPos.x = vMoveCamBack.x; vGoalPos.y = vMoveCamBack.y; vGoalPos.z = vMoveCamBack.z; //GoalPos == The Place the camera is lerping to.
D3DXVec3Lerp( &FinalPosLerpCamera, &FinalPosLerpCamera, &vGoalPos, 0.003 * elapsedMs);
}

D3DXMatrixLookAtLH(&m_matFromWorld, &FinalPosLerpCamera, &vLookAt, &D3DXVECTOR3(0.0f, 1.0f, 0.0f)); //And Finally create the matrix.
D3DXMatrixInverse(&m_matToWorld, NULL, &m_matFromWorld);

m_object->VSetTransform(&m_matToWorld, &m_matFromWorld);
/Have A Nice Day -Patrick

Topic Locked

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

Sign in to reply to this topic.