1
votes

For my current project, I'm trying to include a sprite which simply rotates on the spot. This should be easy, but For some reason, XNA is adding an offset to the position of my sprite.

Here is the draw method I'm using:

spriteBatch.Draw(
             texture,   //texture2D
             position, //position Vector2
             null,  //source Rect (null draws whole texture)
             Color.White, //Color
             rotation, //rotation (float)
             new Vector2(position.X + (texture.Width / 2), position.Y +(texture.Height / 2)), //origin
             1f, //scale (float)
             SpriteEffects.None, //spriteEffects
             1); //layer depth

position is arbitrary here, currently it is set to be the middle-bottom of the viewport. rotation is an incremental float (increments of 0.1f) and when stepping through the code, the origin point and position both hold the correct values.

But no matter what I try, the sprite always gets drawn at some distance from the origin point. It rotates around the origin point, but instead of it being a central axis, the sprite is arching across the screen.

I have tried every variant of the XNA 4.0 built-in draw method. Any which mention rotation have this effect. (it is fine with the basic (texture, position, color) method, position-wise. I have also tried changing the texture for something else, honestly I've tried everything I can think of.

I'm very appreciative for any help. If a more thorough example is required, I will share it, but honestly there isn't really anything happening outside of this method, which would effect it.

Thanks for taking the time to read!

1

1 Answers

2
votes

I think your problem is here:

new Vector2(position.X + (texture.Width / 2), position.Y +(texture.Height / 2))

The origin is meant to be calculated only for the dimension of your texture, so if you want it spin around its center you only need to set:

new Vector2(texture.Width / 2, texture.Height / 2)

You've already set the position in the second parameter.