C#directx sprite origin

当我的Sprite旋转原点固定在窗口的左上角时我有这个问题(与sprite.Drawsprite.Draw2D相同)无论哪种方式,如果我改变旋转中心它仍然在左上角。 我需要精灵绕其Z轴旋转。

编辑:我试过这个:

hereMatrix pm = Matrix.Translation(_playerPos.X + 8, _playerPos.Y + 8, 0); sprite.Transform = Matrix.RotationZ(_angle) * pm; sprite.Draw(playerTexture, textureSize, new Vector3(8, 8, 0), new Vector3(_playerPos.X, _playerPos.Y, 0), Color.White); 

但它似乎不太好用……

画画的时候,它在正确的地方吗?

我相信乘法顺序是相反的,你不应该通过变换中的玩家位置进行变换。

 // shift centre to (0,0) sprite.Transform = Matrix.Translation(-textureSize.Width / 2, -textureSize.Height / 2, 0); // rotate about (0,0) sprite.Transform *= Matrix.RotationZ(_angle); sprite.Draw(playerTexture, textureSize, Vector3.Zero, new Vector3(_playerPos.X, _playerPos.Y, 0), Color.White); 

编辑

您还可以使用Matrix.Transformation方法一步获取矩阵。

我已经为你找到了解决方案,这是一个简单的方法,你可以在每次想要绘制精灵时使用它。 使用此方法,您可以使用所需的旋转中心旋转精灵。

 public void drawSprite(Sprite sprite, Texture texture, Point dimension, Point rotationCenter, float rotationAngle, Point position) { sprite.Begin(SpriteFlags.AlphaBlend); //First draw the sprite in position 0,0 and set your desired rotationCenter (dimension.X and dimension.Y represent the pixel dimension of the texture) sprite.Draw(texture, new Rectangle(0, 0, dimension.X, dimension.Y), new Vector3(rotationCenter.X, rotationCenter.Y, 0), new Vector3(0, 0, 0), Color.White); //Then rotate the sprite and then translate it in your desired position sprite.Transform = Matrix.RotationZ(rotationAngle) * Matrix.Translation(position.X, position.Y, 0); sprite.End(); }