Advertisement

Rotation and Horizontal/Vertical flip with Tiled/Monogame

Started by March 06, 2018 03:43 AM
0 comments, last by Jesse Finnegan 6 years, 9 months ago

I've got a bit of a odd question that I 'think' is still considered beginner territory. Excuse me if it is not.

My question, is whether or not there is a better bit of logic for my code, to convert from Tiled to Monogame as far as sprite flipping and rotation are concerned?

See Tiled's file format stores rotation (only 90 degree intervals) and whether or not a tile is horizontally/vertically flipped with three Boolean values. 

These values are HorizontalFlip, VerticalFlip and DiagonalFlip.  A Diagonal Flip I figured out is either doing a horizontal flip and rotating 270 degrees clockwise, or flipping vertically and rotating 90.

I wanted to convert these to work with Monogame's draw function, which has Horizontal/Vertical flipping plus rotation.  What I came up with was the code bellow.


private static SpriteEffects CalculateEffects(bool hFlip, bool vFlip, bool dFlip)
{
  SpriteEffects effect = SpriteEffects.None;
  if (!dFlip)
  {
    if (hFlip)
    {
      effect |= SpriteEffects.FlipHorizontally;
    }

    if (vFlip)
    {
      effect |= SpriteEffects.FlipVertically;
    }
  }
  else
  {
    // A Diagonal flip is wierd to calculate
    if (hFlip && vFlip)
    {
      effect = SpriteEffects.FlipHorizontally;
    }

    if (!hFlip && vFlip)
    {
      effect |= SpriteEffects.FlipHorizontally;
      effect |= SpriteEffects.FlipVertically;
    }

    if (hFlip && !vFlip)
    {
      effect = SpriteEffects.None;
    }

    if (!hFlip && !vFlip)
    {
      effect = SpriteEffects.FlipVertically;
    }
  }
  return effect;
}

private static float CalculateRotation(bool hFlip, bool vFlip, bool dFlip)
{
  float rotation = 0.0f;

  // A diagonal flip is weird to calculate
  if (dFlip)
  {
    // Rotate 90 degrees
    rotation = MathHelper.ToRadians(90.0f);
  }

  return rotation;
}

 

This topic is closed to new replies.

Advertisement