在XNA中更改旋转矩阵的原点

时间:2012-08-28 19:22:05

标签: matrix xna rotation

我试图让4个Vector2对象代表精灵的4个角,围绕精灵旋转,围绕它的中心旋转。但是,使用以下代码,Vector2对象在Client Space中绕0,0旋转,而不是围绕对象的中心旋转。使用矩阵变换,有没有办法围绕对象的中心而不是全局坐标(0,0)旋转Vector2对象?

到目前为止,这是旋转的功能:

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;

    }

1 个答案:

答案 0 :(得分:0)

在添加spritePosition之前先旋转四个角并在执行旋转和缩放后添加精灵位置可能要容易得多。

只需将您的四个角放入spriteSize的相应组合中,并在完成后Vector2.Transform执行spritePositionvectorArray添加到{{1}}中的四个向量