Jump to content
  • Advertisement
Sign in to follow this  
AhmedCoeia

Code review for DirectX 11.1 Camera 2D/3D

This topic is 1439 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 All,

 

Before asking for code review, I would like to note an issue. I have implemented two parts of the camera, ortho and perspective. 

 

Now I'm writing a 2D Game and I've used that setup 

// near 0, far 100

m_Camera.SetOrthographic(WINDOW_WIDTH, WINDOW_HEIGHT,0.0f, 100.0f);
m_Camera.SetPosition(XMFLOAT3(0, 0, -1.0f));
m_Camera.LookAt(XMVectorSet(0,0,0,0));
and I'm using DirectXTK sprite and I use the view matrix of the camera to transform the sprites
 
XMMATRIX m = m_Graphics.getViewMatrix();
m_Graphics.getSpriteBatch()->Begin(SpriteSortMode_Deferred, nullptr, nullptr, nullptr, nullptr, nullptr, m);

The current is, whenever I set the position of camera to less than -1.0f in Z, I can't see anything in the screen. It only works with Z = -1.0f. 

That's the first issue. 

 

 

The 2nd issue, is when I try to Walk the camera right or left, they always get different speed, however I'm using the same amount to strafe right or left. it looks like that it gets an initialized forward vector and the vector adds to the amount to walk much more then strafing right or left. 

 

Here is the camera class:

 

I use both for 2D and 3D FPS setup, the same updateviewmatrix, I just change the projection from ortho to perspective and vice versa

void cCamera::Initialize()
{
Reset();
}




void cCamera::Reset()
{
// config for left hand camera system
m_Position = XMFLOAT3(0.0f, 0.0f, 0.0f);
m_Forward = XMFLOAT3(0.0f, 0.0f, 1.0f);
m_Up = XMFLOAT3(0.0f, 1.0f, 0.0f);
m_Right = XMFLOAT3(1.0f, 0.0f, 0.0f);


UpdateViewMatrix();
}
 
XMMATRIX cCamera::ViewMatrix()
{
return XMLoadFloat4x4(&m_ViewMatrix);
}
 


XMMATRIX cCamera::ViewProjectionMatrix()
{
XMMATRIX viewMatrix = XMLoadFloat4x4(&m_ViewMatrix);
XMMATRIX projectionMatrix = XMLoadFloat4x4(&m_ProjectionMatrix);


return XMMatrixMultiply(viewMatrix, projectionMatrix);
}




void cCamera::SetPosition(const XMFLOAT3& position)
{
m_Position = position;
}


void cCamera::SetPerspective(float fov, float aspect, float nearPlane, float farPlane)
{
m_FieldOfView = fov;
m_AspectRatio = aspect;
m_NearPlaneDistance = nearPlane;
m_FarPlaneDistance = farPlane;


XMMATRIX projectionMatrix = XMMatrixPerspectiveFovLH(m_FieldOfView, m_AspectRatio, m_NearPlaneDistance, m_FarPlaneDistance);
XMStoreFloat4x4(&m_ProjectionMatrix, projectionMatrix);
}
void cCamera::SetOrthographic(float viewWidth, float viewHeight, float zn, float zf)
{
XMMATRIX projectionMatrix = XMMatrixOrthographicLH(viewWidth, viewHeight, zn, zf);
XMStoreFloat4x4(&m_ProjectionMatrix, projectionMatrix);
}
void cCamera::LookAt(const XMVECTOR &target)
{
XMVECTOR p = XMLoadFloat3(&m_Position);
XMVECTOR look = target - p;
XMStoreFloat3(&m_Forward, look);
}


void cCamera::UpdateViewMatrix()
{
  XMVECTOR eyePosition = XMLoadFloat3(&m_Position);
XMVECTOR direction = XMLoadFloat3(&m_Forward);
XMVECTOR upDirection = XMLoadFloat3(&m_Up);


XMMATRIX viewMatrix = XMMatrixLookToLH(eyePosition, direction, upDirection);
XMStoreFloat4x4(&m_ViewMatrix, viewMatrix);


}




void cCamera::Update(float dt)
{
if (GetAsyncKeyState('W') & 0x8000)
Walk(0.5 * dt);


if (GetAsyncKeyState('S') & 0x8000)
Walk(-0.5 * dt);


if (GetAsyncKeyState('A') & 0x8000)
Strafe(-25 * dt);


if (GetAsyncKeyState('D') & 0x8000)
Strafe(25 * dt);


if (GetAsyncKeyState('E') & 0x8000)
MoveUp(15.0f * dt);


if (GetAsyncKeyState('R') & 0x8000)
MoveUp(-15.0f * dt);


UpdateViewMatrix();
}


void cCamera::MoveUp(float d)
{
// mPosition += d*Up
XMVECTOR s = XMVectorReplicate(d);
XMVECTOR u = XMLoadFloat3(&m_Up);
XMVECTOR p = XMLoadFloat3(&m_Position);
XMStoreFloat3(&m_Position, XMVectorMultiplyAdd(s, u, p));
}


void cCamera::Strafe(float d)
{
// mPosition += d*Right
XMVECTOR s = XMVectorReplicate(d);
XMVECTOR r = XMLoadFloat3(&m_Right);
XMVECTOR p = XMLoadFloat3(&m_Position);
XMStoreFloat3(&m_Position, XMVectorMultiplyAdd(s, r, p));
}


void cCamera::Walk(float d)
{
// mPosition += d*Forward
XMVECTOR s = XMVectorReplicate(d);
XMVECTOR l = XMLoadFloat3(&m_Forward);
XMVECTOR p = XMLoadFloat3(&m_Position);
XMStoreFloat3(&m_Position, XMVectorMultiplyAdd(s, l, p));
}


void cCamera::Pitch(float angle)
{
// Rotate up and forward vector about the right vector.


XMMATRIX R = XMMatrixRotationAxis(XMLoadFloat3(&m_Right), angle);


XMStoreFloat3(&m_Up, XMVector3TransformNormal(XMLoadFloat3(&m_Up), R));
XMStoreFloat3(&m_Forward, XMVector3TransformNormal(XMLoadFloat3(&m_Forward), R));
}


void cCamera::RotateY(float angle)
{
// Rotate the basis vectors about the world y-axis.


XMMATRIX R = XMMatrixRotationY(angle);


XMStoreFloat3(&m_Right, XMVector3TransformNormal(XMLoadFloat3(&m_Right), R));
XMStoreFloat3(&m_Up, XMVector3TransformNormal(XMLoadFloat3(&m_Up), R));
XMStoreFloat3(&m_Forward, XMVector3TransformNormal(XMLoadFloat3(&m_Forward), R));
}

Share this post


Link to post
Share on other sites
Advertisement

Can you show the code to call the cCamera::Update function? What purpose does it's parameter dt have? (time delta?)

Which values does XMVectorMultiplyAdd return for strafing left or right?

 

What happens if you split up your vector operations in the following way:

Multiply m_Right by dt

Add m_Position to the result

Edited by Lordadmiral Drake

Share this post


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