我试图弄清楚如何使用 Draw 方法中的原点围绕其中心旋转精灵。我希望有人可以解释 Draw 方法中 origin 参数的正确用法。
如果我使用以下 Draw 方法(未指定任何旋转和原点),则在正确/预期的位置绘制对象:
spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
但是,如果我使用如下所示的原点和旋转,则对象围绕中心旋转,但对象浮动在预期位置上方(大约 20 像素。)
Vector2 origin = new Vector2(myTexture.Width / 2 , myTexture.Height / 2 );
spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, ballRotation, origin, SpriteEffects.None, 0);
即使我将 ballRotation 设置为 0 对象仍然绘制在预期的位置上方
spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, origin, SpriteEffects.None, 0);
似乎仅通过设置原点,对象的位置就会发生变化。
有人可以告诉我如何正确使用 origin 参数。
解决方案:
Davor 的回答明确了原产地的用法。代码中需要进行以下更改才能使其正常工作:
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);