help me convert a third person camera in d3d11 to d3d9

Started by
4 comments, last by Anddos 11 years, 5 months ago
I have found a nice example for a third person such like you see in the grand theft auto games, its in d3d11 but i want to convert it to d3d9 , i dont feel i know enough for d3d11 yet so i want to stay at d3d9 and implement this code

here is the directx 11 code

void UpdateCamera()
{
//d3d11 code
camTarget = charPosition;
camTarget = XMVectorSetY(camTarget, XMVectorGetY(camTarget)+5.0f);
camRotationMatrix = XMMatrixRotationRollPitchYaw(camPitch, camYaw, 0);
camPosition = XMVector3TransformNormal(DefaultForward, camRotationMatrix );
camPosition = XMVector3Normalize(camPosition);
camPosition = (camPosition * charCamDist) + camTarget;
camForward = XMVector3Normalize(camTarget - camPosition); // Get forward vector based on target
camForward = XMVectorSetY(camForward, 0.0f); // set forwards y component to 0 so it lays only on
camForward = XMVector3Normalize(camForward);
camRight = XMVectorSet(-XMVectorGetZ(camForward), 0.0f, XMVectorGetX(camForward), 0.0f);
camUp =XMVector3Normalize(XMVector3Cross(XMVector3Normalize(camPosition - camTarget), camRight));
camView = XMMatrixLookAtLH( camPosition, camTarget, camUp );
}


here is my attempt with d3d9


void UpdateCamera()
{

camTarget = charPosition;
camTarget.y = ((camTarget.y)+5.0f);
camRotationMatrix = D3DXMatrixRotationYawPitchRoll(&yawPitchRollMat,-camPitch, camYaw, 0); //d3d9
camPosition = D3DXVec3TransformNormal(&DefaultForward,&DefaultForward, camRotationMatrix );
//so far i am stuck on this line, because camPosition should be D3DXVECTOR3* but if i set it to that, then the code below is broken , please can anyone help with coverting this to work with d3d9.



camPosition = D3DXVec3Normalize(&camPosition,&camPosition);
camPosition = ((*camPosition * charCamDist)) + camTarget;
camvec3 = camTarget - camPosition;
camForward = D3DXVec3Normalize(&camvec3,&camvec3); // Get forward vector based on target
camForward = XMVectorSetY(camForward, 0.0f); // set forwards y component to 0 so it lays only on
camForward = XMVector3Normalize(camForward);
camRight = XMVectorSet(-XMVectorGetZ(camForward), 0.0f, XMVectorGetX(camForward), 0.0f);
camUp =XMVector3Normalize(XMVector3Cross(XMVector3Normalize(camPosition - camTarget), camRight));
camView = D3DXMatrixLookAtLH( camPosition, camTarget, camUp );
*/

}


here is a picture of the d3d11 app

http://imageshack.us/f/5/thirdpersoncam.jpg
:)
Advertisement
You can change
[source lang="cpp"]camPosition = D3DXVec3TransformNormal(&DefaultForward,&DefaultForward, camRotationMatrix );[/source]
To
[source lang="cpp"]D3DXVec3TransformNormal(&camPosition,&DefaultForward, camRotationMatrix );[/source]
Since the first argument gets set to the result of the operation as well. Now camPosition don't have to be a pointer any more.
how would i do this line then

camForward = XMVector3Normalize(camTarget - camPosition); // Get forward vector based on target
:)
[source lang="cpp"]D3DXVec3Normalize(&camForward, &(camTarget - camPosition));[/source]

I recommend taking a look at this page to see the DirectXMath counterpart for all D3DXMath functions.
so far i have


camTarget = charPosition;
camTarget.y = ((camTarget.y)+5.0f);
camRotationMatrix = D3DXMatrixRotationYawPitchRoll(&yawPitchRollMat,-camPitch, camYaw, 0); //d3d9
D3DXVec3TransformNormal(&camPosition,&DefaultForward, camRotationMatrix );
D3DXVec3Normalize(&camPosition,&camPosition);
camPosition = ((camPosition * charCamDist)) + camTarget;
D3DXVec3Normalize((&camForward),&(camTarget - camPosition)); // Get forward vector based on target

camForward.y = camForward.y = 0.0f; // set forwards y component to 0 so it lays only on
D3DXVec3Normalize((&camForward),&(camForward));



now i am stuck

would this be

camRight = XMVectorSet(-XMVectorGetZ(camForward), 0.0f, XMVectorGetX(camForward), 0.0f);

camRight.z = -camForward.z,
camRight.y = 0.0f,
camRight.z = camForward.x
:)
i cannot for the life of me get this line right

//camUp =XMVector3Normalize(XMVector3Cross(XMVector3Normalize(camPosition - camTarget), camRight)); //d3d11

D3DXVec3Normalize(&camUp,D3DXVec3Cross(D3DXVec3Normalize(&(camPosition - camTarget), &camRight))); //d3d9
:)

This topic is closed to new replies.

Advertisement