Sign in to follow this  
Anddos

help me convert a third person camera in d3d11 to d3d9

Recommended Posts

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

here is my attempt with d3d9

[code]
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 );
*/

}
[/code]

here is a picture of the d3d11 app

http://imageshack.us/f/5/thirdpersoncam.jpg Edited by Anddos

Share this post


Link to post
Share on other sites
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. Edited by simpler

Share this post


Link to post
Share on other sites
how would i do this line then

camForward = XMVector3Normalize(camTarget - camPosition); // Get forward vector based on target

Share this post


Link to post
Share on other sites
[source lang="cpp"]D3DXVec3Normalize(&camForward, &(camTarget - camPosition));[/source]

I recommend taking a look at [url="http://msdn.microsoft.com/en-us/library/windows/desktop/ff729728(v=vs.85).aspx"]this[/url] page to see the DirectXMath counterpart for all D3DXMath functions.

Share this post


Link to post
Share on other sites
so far i have

[code]
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));

[/code]

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 Edited by Anddos

Share this post


Link to post
Share on other sites
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

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