I am trying to figure out how to use the beginning in the Draw method to rotate the sprite around its center. I was hoping someone could explain the correct use of the origin parameter in the Draw method.
If I use the following Draw method (without specifying rotation and source), the object is drawn in the correct / expected location:
spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
However, if I use the beginning and rotation, as shown below, the object rotates around the center, but the object floats above the expected location (about 20 pixels).
Vector2 origin = new Vector2(myTexture.Width / 2 , myTexture.Height / 2 ); spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, ballRotation, origin, SpriteEffects.None, 0);
Even if I set ballRotation to 0, the object will still be drawn above the expected location
spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, origin, SpriteEffects.None, 0);
It seems that simply by setting the origin, the location of the object will change.
Can someone tell me how to use the source parameter correctly.
Decision:
Davor's answer made the use of the source clear. To do this, the code required the following change:
Vector2 origin = new Vector2(myTexture.Width / 2 , myTexture.Height / 2 ); destinationRectangle.X += destinationRectangle.Width/2; destinationRectangle.Y += destinationRectangle.Height / 2; spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, ballRotation, origin, SpriteEffects.None, 0);