Jump to content
  • Advertisement
Sign in to follow this  
lipsryme

DX11 Converting FreeCamera from XNA(C#) to DirectX (C++)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
I'm currently trying to convert my old C# code for implementing a free moving camera to my new dx11 based engine but I'm having a bit of a problem with the conversion of the direct input mouse stuff I think.

First let me show you the original C# code:

//Check movement bools
if (Current.IsKeyDown(Keys.W)) Position += Direction * Speed;
if (Current.IsKeyDown(Keys.S)) Position -= Direction * Speed;
if (Current.IsKeyDown(Keys.A)) Position += Vector3.Cross(Up, Direction) * Speed;
if (Current.IsKeyDown(Keys.D)) Position -= Vector3.Cross(Up, Direction) * Speed;
//Rotate Camera
Quaternion rx = Quaternion.CreateFromAxisAngle(Up, (-MathHelper.PiOver4 / 150) * (CurrentMouse.X - mouseReference.X));
Direction = Vector3.Transform(Direction, Matrix.CreateFromQuaternion(rx));
Quaternion ro = Quaternion.CreateFromAxisAngle(Vector3.Cross(Up, Direction), (MathHelper.PiOver4 / 100) * (CurrentMouse.Y - mouseReference.Y));
Direction = Vector3.Transform(Direction, Matrix.CreateFromQuaternion(ro));
//Calculate new View matrix from updated members
View = Matrix.CreateLookAt(Position, Position + Direction, Up);

note: mouseReference is a mousestate that was received once at the beginning

And now my translated code:

XMVECTOR vec = XMVector3Cross(this->GetCamUp(), this->GetCamTarget());
XMFLOAT3 direct;
XMStoreFloat3(&direct, vec);
// Check movement bools
if(currentKeyState[DIK_W] & 0x80)
{
this->camPosition.x += this->direction.x * (float)gametime * this->speed;
this->camPosition.y += this->direction.y * (float)gametime * this->speed;
this->camPosition.z += this->direction.z * (float)gametime * this->speed;
}
if(currentKeyState[DIK_S] & 0x80)
{
this->camPosition.x -= this->direction.x * (float)gametime * this->speed;
this->camPosition.y -= this->direction.y * (float)gametime * this->speed;
this->camPosition.z -= this->direction.z * (float)gametime * this->speed;
}
if(currentKeyState[DIK_A] & 0x80)
{
this->camPosition.x -= direct.x * (float)gametime * this->speed * 4;
this->camPosition.y -= direct.y * (float)gametime * this->speed * 4;
this->camPosition.z -= direct.z * (float)gametime * this->speed * 4;
}
if(currentKeyState[DIK_D] & 0x80)
{
this->camPosition.x += direct.x * (float)gametime * this->speed * 4;
this->camPosition.y += direct.y * (float)gametime * this->speed * 4;
this->camPosition.z += direct.z * (float)gametime * this->speed * 4;
}

// Rotate camera
XMVECTOR directionVector;
XMVECTOR rx = XMQuaternionRotationAxis(this->GetCamUp(), (-(float)M_PI_4 / 150) * (currentMouseState.lX - referenceMouseState.lX));
directionVector = XMLoadFloat3(&this->direction);
directionVector = XMVector3Rotate(directionVector, rx);
XMVECTOR ro = XMQuaternionRotationAxis(XMVector3Cross(this->GetCamUp(), this->GetCamTarget()),
((float)M_PI_4 / 100) * (currentMouseState.lY - referenceMouseState.lY));
directionVector = XMVector3Rotate(directionVector, ro);

// Calculate new View Matrix from updated members
XMMATRIX viewMat = XMMatrixLookAtLH(this->GetCamPosition(), this->GetCamPosition() + directionVector, this->GetCamUp());
XMStoreFloat4x4(&this->viewMatrix, viewMat);


From what I understand the quaternion rotation should work even if it's a little different from the XNA Quaternion operations.
Now I think my problem with this seems to be with the DirectInput MouseState which seems to be different than the one XNA uses.
Can someone direct me in the right direction ? tongue.png

The camera somehow seems to fixate on the center of the screen and when I move my mouse it flickers for a second like it wants to move but gets thrown back to the center. Any ideas ? Edited by lipsryme

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!