Matrix transformation desyncs coordinates

I only understand the basics of matrices, apologies in advanced.

I’m currently developing on a 2D base that uses a matrix to displace game-tiles by a given rotation, `56` in the case of the given example, to create a cheap perspective. However, it seems doing so desyncs the coordinates of the tiles from the other, non-rotated, instances. This may be due to the camera system, which displaces all game-objects by a given number, `1` `5` `6`, to pan around.
I’m under the impression the matrix shrinks the sprite’s rendered coordinates and therefor makes it appear to move slower. However, I’m not sure how to remedy this while maintaining the perspective effect.

The Matrix I use-

``````Matrix matrix = Matrix.CreateTranslation(-origin) *
Matrix.CreateScale(1f, 1f, 0) *
Matrix.CreateTranslation(0, (H.rotation.X/90)*64, 0) *
Matrix.CreateTranslation(origin);
...
spriteBatch.Begin(transformMatrix: matrix);
spriteBatch.Draw(texture_output, new Vector2(H.position.X, H.position.Y), Color.White)
``````

https://imgur.com/a/mxdPxME
Sorry about the quality. You can see how the rotated axis, X, causes the ground to be out of sync with the marker, vertically. To be clear, this appears as a partial parallax effect on the Y-Axis, greatly undesired.

What is being drawn here, a background tile? Do you use the same transform matrix when drawing the monkey head?

Why do you want a “cheap perspective” effect, is that a style choice? Regular perspective transforms using a perspective matrix are just as cheap performance-wise.

What is being drawn here, a background tile? Do you use the same transform matrix when drawing the monkey head?

Yes, the matrix is applied to every rendered instance, with the instance’s rotation variable acting as the driving difference. The tiles are the only on-screen object with a rotation that isn’t default, `55` on the X-Axis.

Why do you want a “cheap perspective” effect, is that a style choice? Regular perspective transforms using a perspective matrix are just as cheap performance-wise.

It is mainly style, yes. It also, should, allow me to quickly prototype different perspectives without authoring new content.
As the post says, I’m not particularly familiar with matrices, especially dedicated types. The main feature must be that a given matrix can be selectively applied, so instances can move along the same plane with different rotations.

The code you’ve shown is wrapped inside a loop over all tiles? If all the tiles are using the same matrix, then the loop should be around the `spriteBatch.Draw`. Creating a new batch for every tile is pretty expensive.

So you are drawing the monkey head with a different matrix, based on a different rotation value? Of course its position will be out of sync with the background tiles. Your matrix shifts the y-position based on the rotation value → different rotation == different y-position.

I think you want to look into how camera’s are usually handled in 3D rendering. There’s a matrix that represents the camera, and this matrix will be the same for all objects. Each object then has its own matrix too, representing it’s position/rotation/scale in the world.

1 Like