在其中心周围旋转精灵

Ach*_*hta 5 xna xna-4.0

我试图弄清楚如何在Draw方法中使用原点来围绕其中心旋转精灵.我希望有人可以解释Draw方法中origin参数的正确用法.

如果我使用以下Draw方法(没有指定任何旋转和原点),则在正确/预期的位置绘制对象:

spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用如下所示的原点和旋转,则对象围绕中心旋转但对象浮动在预期位置上方(大约20像素).

Vector2 origin = new Vector2(myTexture.Width / 2 , myTexture.Height / 2 );
spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, ballRotation, origin, SpriteEffects.None, 0);
Run Code Online (Sandbox Code Playgroud)

即使我将ballRotation设置为0,对象仍然被绘制在预期位置之上

spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, origin, SpriteEffects.None, 0);
Run Code Online (Sandbox Code Playgroud)

似乎只是通过设置原点,对象的位置会发生变化.

有人可以告诉我如何正确使用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);
Run Code Online (Sandbox Code Playgroud)

Dav*_*ric 8

这是正确使用原产地.但现在你的位置也变成了中心位置,它不再位于左上角,它位于中心位置.和它的漂浮width/2,并height/2从位置befor塞汀原点.

在此输入图像描述

因此,如果您的纹理是20x20,则需要将X减去10(宽度/ 2),将Y减去10(高度/ 2),您将具有原始位置.