Monogame旋转精灵

时间:2015-06-28 20:09:45

标签: c# monogame image-rotation

我正在尝试在Monogame中旋转一个精灵,但由于某种原因我不能正确!如果有人解释轮换之谜,我将非常感激!

这是我用来绘制和旋转我的精灵的代码行,只是注意变量y是我在每次更新时将其递增0.1的角度,这仅用于测试目的。如何让大炮围绕图像内的原点旋转?例如,它的中心点就像直升机螺旋桨? Check the video查看此行代码的结果。

        spriteBatch.Draw(cannon.image, new Rectangle(300, 300, cannon.image.Width, cannon.image.Height), null, Color.White, y, new Vector2(0, 0), SpriteEffects.None, 0f);

2 个答案:

答案 0 :(得分:10)

这看起来像你正在使用的超载:

public void Draw (
     Texture2D texture,
     Vector2 position,
     Nullable<Rectangle> sourceRectangle,
     Color color,
     float rotation,
     Vector2 origin,
     float scale,
     SpriteEffects effects,
     float layerDepth
)

问题是您需要将原点设置为旋转。在MonoGame / XNA中,原点以像素为单位指定,相对于纹理的大小。所以要计算精灵的中心,你需要做这样的事情:

var origin = new Vector2(texture.Width / 2f, texture.Height / 2f);

然后调用sprite批处理绘制方法,如下所示:

spriteBatch.Draw(cannon.image, new Rectangle(300, 300, cannon.image.Width, cannon.image.Height), null, Color.White, y, origin, SpriteEffects.None, 0f);

最后,请不要使用y作为轮换的变量名,对于读取代码的其他程序员来说,这可能会非常混乱。更好的名称是rotation

答案 1 :(得分:0)

答案:在新的Vector2(x,y)中,我只需要设置图像中的x和y值。