简体   繁体   中英

DirectX 11 2D Camera rotation

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM