Dar*_*und 2 c# image-rotation monogame
我试图在Monogame中旋转一个精灵,但由于某种原因我无法正确!如果有人解释轮换之谜,我将非常感激!
这是我用来绘制和旋转我的精灵的代码行,只是注意变量y是我在每次更新时将其递增0.1的角度,这仅用于测试目的.如何让大炮围绕图像内的原点旋转?例如,它的中心点就像直升机螺旋桨?检查视频以查看此行代码的结果.
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);
Run Code Online (Sandbox Code Playgroud)
cra*_*mes 10
这看起来像你正在使用的重载:
public void Draw (
Texture2D texture,
Vector2 position,
Nullable<Rectangle> sourceRectangle,
Color color,
float rotation,
Vector2 origin,
float scale,
SpriteEffects effects,
float layerDepth
)
Run Code Online (Sandbox Code Playgroud)
问题是您需要将原点设置为旋转.在MonoGame/XNA中,原点以像素为单位指定,相对于纹理的大小.所以要计算精灵的中心,你需要做这样的事情:
var origin = new Vector2(texture.Width / 2f, texture.Height / 2f);
Run Code Online (Sandbox Code Playgroud)
然后调用sprite批处理绘制方法,如下所示:
spriteBatch.Draw(cannon.image, new Rectangle(300, 300, cannon.image.Width, cannon.image.Height), null, Color.White, y, origin, SpriteEffects.None, 0f);
Run Code Online (Sandbox Code Playgroud)
最后,请不要使用y旋转的变量名称,对于读取代码的其他程序员来说可能会非常混乱.一个更好的名字rotation.