Flip Y-Axis

I’m trying to flip the Y-axis in the camera, so up is +ve Y because I need this in the world calculations to make my life easier, everything was fine until I tried

Matrix.CreateScale(1, -1, 1);

Now Nothing is showing on screen. Is this the right way to do it? Should I do other things to fix the negative scale sprite-wise?
My Camera class does the matrix like this

        TransformMatrix = new Matrix { M11 = _zoom, M22 = -_zoom, M33 = 1f, M41 = Position.X * _zoom, M42 = Position.Y * _zoom, M44 = 1f };

For the context, I work with 2D.

I would use Matrix.CreateOrthographicOffCenter like shown here:

Also I wouldn’t recommend manually setting the components of the transform. Instead create it using matrix multiplications such as…

TransformMatrix = 
      Matrix.CreateOrthographicOffCenter(...) * 
      Matrix.CreateScale(_zoom, _zoom, 1) * 
      Matrix.CreateTranslation(new Vector3(-CameraPosition, 0f))

etc, as it tends to be much more maintainable and extensible code

I have tried to use the solutions I found in other thread, but the result is the same, nothing is showing on screen, or the coordinates don’t flip.

Also, I set up the components manually for some optimization, because the ready methods do a lot of unnecessary calculation for me.

It might not be drawing your sprites because mirroring them vertically changes their vertex order from clockwise to counter-clockwise. So the GPU thinks you’re looking at the back of the triangles and not the front. Try this:

GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;

1 Like

I was able to solve it thanks to @Quasar, the negative scale indeed changed the vertex order, and the flipped culling i.e., RasterizerState.CullClockwise fixed it, for anyone with the same problem I’ve used this:

TransformMatrix = Matrix.CreateTranslation(-Position) * Matrix.CreateScale(_zoom, -_zoom, 1);

or

TransformMatrix = new Matrix { M11 = _zoom, M22 = -_zoom, M33 = 1f, M41 = -Position.X * _zoom, M42 = Position.Y * _zoom, M44 = 1f };

Matrix.CreateOrthographicOffCenter didn’t work for me, idk why, but I’m guessing because Matrix.CreateOrthographicOffCenter gives a projection matrix, but SpriteBatch.Draw expects a world matrix, maybe.

Also, as a side note this

GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;

either add it after the SpriteBatch.Begin or in it like this:

_spriteBatch.Begin(rasterizerState: RasterizerState.CullClockwise, transformMatrix: Camera?.TransformMatrix);

Else, the SpriteBatch.Begin will default to RasterizerState.CullCounterClockwise.

1 Like