Note that there are some explanatory texts on larger screens.

plurals
  1. PORotating a sprite around its center
    primarykey
    data
    text
    <p>I am trying to figure out how to use the origin in Draw method to rotate a sprite around its center. I was hoping somebody could explain the correct usage of origin parameter in Draw method.</p> <p>If I use the following Draw method (without any rotation and origin specified) the the object is drawn at the correct/expected place:</p> <pre><code>spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0); </code></pre> <p>However, if I use the origin and rotation like shown below, the object is rotating around is center but the object is floating above the expecting place (by around 20 pixels.)</p> <pre><code>Vector2 origin = new Vector2(myTexture.Width / 2 , myTexture.Height / 2 ); spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, ballRotation, origin, SpriteEffects.None, 0); </code></pre> <p>Even if I set the ballRotation to 0 the object is still drawn above the expected place</p> <pre><code>spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, origin, SpriteEffects.None, 0); </code></pre> <p>Is seems that just by setting the origin, the placement of the object changes.</p> <p>Can somebody tell me how to use the origin parameter correctly.</p> <hr> <h1>Solution:</h1> <p>Davor's response made the usage of origin clear. The following change was required in the code to make it work:</p> <pre><code>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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload