# How can i flip non centered Sprites?

Dear People

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

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
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.

{
return degrees * .017453292519f;
}
{
}

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

{
}
{
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.