0

I'm trying to make a 2D game using DirectX 11 and I get distortion issues when I rotate the camera

By rotating the camera I mean rotating it about its Z axis with the X axis being left/right on the screen, the Y axis being up/down on the screen, and the Z axis protruding from the screen. so when I rotate the camera, the rest of the world should appear to be orbiting around the center of the screen

Here I have a perfectly square sprite

and when I rotate the camera it becomes stretched

note this doesn't happen when I rotate the sprite itself, only the camera

my current setup with my camera is

void Camera::SetOrthoMatrix(float width, float height, float nearZ, float farZ) //called when initializing the camera
{
    orthoMatrix=XMMatrixOrthographicOffCenterLH(0.0f, width, 0.0f, height, nearZ, farZ);
}

void Camera::UpdateMatrix() //called whenever the camera's position or rotation changes
{
    worldMatrix = XMMatrixTranslation(-pos.x, -pos.y, 0.0f) * XMMatrixRotationZ(rot);
}

XMMatrix Camera::GetViewMatrix()
{
    return orthoMatrix * worldMatrix
}

My sprite class just takes it's own world matrix and multiplies it by camera.GetViewMatrix() and then I draw the sprite. I'm fairly certain that the distortion is caused by the orthographic matrix, but I can't figure out a way to fix it.

yoy
  • 1
  • 1
  • *"when I rotate the camera"* - what exactly do you mean? Do you mean rotate the camera around the sprint ensuring you keep a constant radius from the sprite center? Do you mean spinning the camera around the fixed axis between the camera and the sprite? Or do you mean literally spinning the camera line a bottle so you only get a glimpse of the sprite once each revolution? – David C. Rankin May 20 '22 at 03:59
  • By rotating the camera I mean rotating it about its Z axis with the X axis being left/right on the screen, the Y axis being up/down on the screen, and the Z axis protruding from the screen. so when I rotate the camera, the rest of the world should appear to be orbiting around the center of the screen. – yoy May 20 '22 at 04:14
  • Got it. That makes it clear. I would suggest you copy the explanation from your comment and edit your question and add it there. That way the question itself provides the explanation. – David C. Rankin May 20 '22 at 04:18

1 Answers1

0

I figured it out by changing XMMatrixOrthographicOffCenterLH to XMMatrixOrthograpghicLH. then reordering the matrix multiplication from sprite, orthographic, camera to sprite, camera, orthographic.

this changes the coordinate system a little, before I had 0,0 be the bottom left corner of the screen but with XMMatrixOrthograpghicLH the origin is at the center of the screen. It's not quite what I wanted but it's a minor inconvenience.

yoy
  • 1
  • 1