Sprite rotation around its center

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); 
+4
source share
1 answer

this is the correct use of origin. but now your position has also changed to the center, it is no longer in the upper left corner, but in the center. and it floats for width/2 and height/2 from position to establishing origin.

enter image description here

therefore, if your texture is 20x20, you need to subtract X by 10 (width / 2) and Y by 10 (height / 2), and you will have the starting position.

+7
source

Source: https://habr.com/ru/post/1491994/


All Articles