How can i flip non centered Sprites?

Dear People :slightly_smiling:

I just ran into some problems with my sprite class, i have some sprites that are “centered” and some sprites aren’t centered and if i use the spritebatch parameter “spriteeffect” it spins not correctly from the center.

How could i fix this behavior or how could i properly implement a solution in my SpriteClass.

Thank you very much :slightly_smiling:

Use the float “rotation” parameter instead of SpriteEffect, and set the center of rotation using the Vector2 “origin” parameter. This won’t help with mirroring, but mirroring sprites shouldn’t be affected by whether sprites are centered or not.

I’m pretty sure rotation is using radians as its unit, so for 95 degrees, it’s pi / 2. I’m pretty sure there’s a conversion function somewhere for converting from degrees to radians, but I don’t remember where it is at the moment.

for the origin you just place in the origin parameter for spritebatch this
, new Vector2(image.Width *.5, image.Height *.5),

to get radians from degrees you just basically multiply them by the magic number 0.0745329
to get degrees from radians same thing multiply rads by 57.29577
these numbers come from 360 / (2pi) , and (2pi) / 360
I have no idea why we don’t just use 0 to 1 though radian’s have some relation to a circle they have nothing to do with actual rotating or sin cos rotation is more about pythagorean theorum.

public static float DegreesToRadians(float degrees)
{
return degrees * .017453292519f;
}
public static float RadiansToDegrees(float radians)
{
return radians * 57.2957795130f;
}

A couple more that might help or you might need later.

public static Vector2 RadiansToDirection(float radians)
{
return new Vector2(-(float)(Math.Sin(radians)), (float)(Math.Cos(radians)));
}
public static float DirectionToRadians(Vector2 direction)
{
return (float)Math.Atan2(direction.X, direction.Y);
}

Just beware that atan 2 works off polar quadrants i think so you have to adjust for that.
to say math uses up as 0 degrees but wants y,x and xna right is 0 degrees because the rotation has to be based on some set of quadrants which is different then maths.

    public static void DrawToScreenCentered(Texture2D textureobj, Rectangle scrdraw_rect, Rectangle texturerect, float rot)
    {
        Vector2 toff = new Vector2(texturerect.Width * .5f, texturerect.Height * .5f);
        BxEngien.Spritebatch.Draw(textureobj, scrdraw_rect, texturerect, spritecolor, rot, toff, flipeffect, 0);
    }
    public static void DrawToScreenCentered(Texture2D textureobj, Vector2 position, Rectangle texturerect,float rot, float scale)
    {
        Vector2 toff = new Vector2(texturerect.Width * .5f, texturerect.Height * .5f);
        BxEngien.Spritebatch.Draw(textureobj, position, texturerect, spritecolor, rot,Vector2.Zero, scale, flipeffect, 0);
    }
    public static void DrawToScreenCentered(Texture2D textureobj, Rectangle scrdraw_rect, Rectangle texturerect, float rot, float scale)
    {
        scrdraw_rect.Width = (int)(scrdraw_rect.Width * scale);
        scrdraw_rect.Height = (int)(scrdraw_rect.Height * scale);
        Vector2 toff = new Vector2(texturerect.Width * .5f, texturerect.Height * .5f);
        BxEngien.Spritebatch.Draw(textureobj, scrdraw_rect, texturerect, spritecolor, rot, toff, flipeffect, 0);
    }
    public static void DrawToScreenRects(Texture2D textureobj, Rectangle screenrect, Rectangle texturerect, float rot)
    {
        screenrect.X += (int)(screenrect.Width * .5f);
        screenrect.Y += (int)(screenrect.Height * .5f);
        Vector2 toff = new Vector2(texture_offset.X + texturerect.Width * .5f, texture_offset.Y + texturerect.Height * .5f);
        BxEngien.Spritebatch.Draw(textureobj, screenrect, texturerect, SpriteColor, rot, toff, flip_effect, 0);
    }

note
Just replace bxengien with a reference to your passed graphicsdevice, i have mine in my engien as a static updated reference. the third version is basically for if your working with a spritesheet or image with multiple pictures it allows you to do the with that the same as you would with a single image. The fourth version adjust for what centering does to screendraw coordinates if you desire to cancel out that translation effect.
rot is rotation pass in radians, scale you can just pass 1.0 if you don’t want to scale it.