问题描述
当我的Sprite旋转原点固定在窗口的左上角时(与 sprite.Draw 和 sprite.Draw2D 相同),我遇到了这个问题
方式,如果我改变旋转中心,它仍然在左上方。我需要精灵绕其Z轴旋转。
i have this problem when my Sprite rotation origin is fixed at top left corner of window (same with sprite.Draw and sprite.Draw2D)Either way if i change rotation center it's still at top left. I need sprite to rotate around its Z axis.
编辑:
我已经尝试过:
I have tried this:
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);
但是效果似乎不太好...
But it does not seem to works well...
推荐答案
绘制时,它在正确的位置吗?
When you draw it, is it in the correct place?
我相信乘法顺序是相反的,并且您不应该根据变换中的玩家位置进行变换。
I believe that the multiplication order is reversed, and that you shouldn't be transforming by the players position in the transform.
// 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);
编辑
Edit
您也可以使用 Matrix.Transformation
方法一步来获取矩阵。
You could also use the Matrix.Transformation
method to get the matrix in one step.
这篇关于C#DirectX Sprite起源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!