Flip, Rotate Sprite Separately?

hello I have recently picked up monogame again and so far I like it.
however while writing my drawTile function I wanted to allow the user to pass in the following

  • rotation (degrees) {0=up, 1=left, 2=down,3=right}
  • flipx (bool) {false=nomirror, 1=mirroredx}
  • flipy (bool) {false=nomirror, 1=mirroredy}

the problem is when I looked up spriteBatch.Draw() method it looks as if its rotated and not mirrored when fliped.

my code is currently this…

    static class Graphics
    {
        static GraphicsDeviceManager graphics;

        static void DrawTile(int tile, int x, int y, int w=1, int h=1)
        {
            int sx = (int)Math.Floor((decimal)tile % Globals.TILEBANK_W);
            int sy = (int)Math.Floor((decimal)tile / Globals.TILEBANK_W);
            int ex = sx + w;
            int ey = sy + h;

            for (int oy=sy; oy<ey; oy++)
            for (int ox=sx; ox<ex; ox++)
            {
                int oi    = ox + oy * Globals.TILEBANK_W;
                int index = (int)Math.Floor((decimal)(oi / (Globals.TILEBANK_W * Globals.TILEBANK_H)));
            
                Texture2D tileBank = TileBank.Get(index);
                Rectangle tileRect = new Rectangle
                (
                    (int)Math.Floor((decimal)(oi % Globals.TILEBANK_W) * Globals.TILE_W),
                    (int)Math.Floor((decimal)(oi / Globals.TILEBANK_W) * Globals.TILE_W),
                    (int)Globals.TILE_W,
                    (int)Globals.TILE_W
                );

                Globals.SpriteBatch.Draw(tileBank, new Rectangle(x, y, Globals.TILE_W, Globals.TILE_H), tileRect, Color.White);
            }
        }

    }

SpriteBatch.Draw() has multiple overloads. The one I think you’ll want, based off your code, is this:

public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth);

SpriteEffects is an enum that acts as a bit field. It looks like this:

[Flags]
public enum SpriteEffects
{
    None = 0,
    FlipHorizontally = 1,
    FlipVertically = 2
}

In your case, you’ll want to pass in a SpriteEffects value that matches the flipx and flipy values you have. If both are true, you can flip the sprite both horizontally and vertically by passing in SpriteEffects.FlipHorizontally | SpriteEffects.FlipVertically as the SpriteEffects value in Draw().

1 Like

but mirroring and rotating are two different things.
what does the flips flags do?

I need to both mirror and rotate but the docs say flip just rotates it 180 degrees, which is what I don’t want.

That’s what I mentioned. SpriteEffects is basically a set of flip flags. If set to FlipHorizontally, it will actually flip your sprite horizontally, not rotate it 180 degrees. If you want to rotate, pass in the rotation value to the Draw() overload. You can both flip and rotate with that overload if you wish.

1 Like

ok thankyou for clarifying that.