Matrix 更改XNA中旋转矩阵的原点
我试图得到4个矢量2对象,代表精灵的4个角,围绕精灵旋转,精灵本身围绕其中心旋转。但是,在我下面的代码中,Vector2对象在客户机空间中围绕0,0旋转,而不是围绕对象的中心旋转。使用矩阵变换,是否有任何方法可以围绕对象的中心而不是全局坐标(0,0)旋转Vector2对象 以下是迄今为止的旋转功能:Matrix 更改XNA中旋转矩阵的原点,matrix,xna,rotation,Matrix,Xna,Rotation,我试图得到4个矢量2对象,代表精灵的4个角,围绕精灵旋转,精灵本身围绕其中心旋转。但是,在我下面的代码中,Vector2对象在客户机空间中围绕0,0旋转,而不是围绕对象的中心旋转。使用矩阵变换,是否有任何方法可以围绕对象的中心而不是全局坐标(0,0)旋转Vector2对象 以下是迄今为止的旋转功能: public Vector2[] CheckCollision() { //Get the 4 corners of the sprite locally /
public Vector2[] CheckCollision()
{
//Get the 4 corners of the sprite locally
//We can get all 4 corners from only 2 vectors
Vector2 topLeft = new Vector2(position.X - spriteSize.X, position.Y - spriteSize.Y);
//Not sure why position is representing the
//bottom right instead of the center here....
Vector2 bottomRight = position;
Vector2 bottomLeft = new Vector2(topLeft.X, bottomRight.Y);
Vector2 topRight = new Vector2(bottomRight.X, topLeft.Y);
//Create transformation matrix
Matrix transform = Matrix.CreateRotationZ(MathHelper.ToRadians(this.direction)) *
Matrix.CreateScale(this.scale);
//Transform the vectors
topLeft = Vector2.Transform(topLeft, transform);
bottomRight = Vector2.Transform(bottomRight, transform);
bottomLeft = Vector2.Transform(bottomLeft, transform);
topRight = Vector2.Transform(topRight, transform);
Vector2[] vectorArray = new Vector2[4];
vectorArray[0] = topLeft;
vectorArray[1] = bottomRight;
vectorArray[2] = bottomLeft;
vectorArray[3] = topRight;
return vectorArray;
}
在添加
spritePosition
之前先旋转四个角,然后在执行旋转和缩放之后添加spritePosition,可能会容易得多
只需将您的四个角设置为相应的
精灵大小组合
,然后执行矢量2。转换
完成后,将精灵位置
添加到矢量数组中的四个矢量
精灵位置对应于其右下角的事实让我相信您是正确的调用SpriteBatch.Draw()
@ColeCampbell时指定的原点不正确我在调用SpriteBatch.Draw()时指定了精灵的中心作为原点。这允许SpriteBatch围绕对象的中心旋转精灵,就像我尝试处理Vector2对象一样。我也需要解决位置问题,但它不像矩阵问题那么严重,我觉得我可以在以后解决它。