1

我试图让代表精灵的 4 个角的 4 个 Vector2 对象围绕精灵旋转,精灵本身围绕其中心旋转。然而,使用我的以下代码,Vector2 对象在客户端空间中围绕 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;

    }
4

1 回答 1

0

在添加 spriteposition之前先旋转四个角可能会容易spritePosition得多,然后在执行旋转和缩放之后添加 spriteposition。

Just make your four corners into the corresponding combinations of spriteSize and do the Vector2.Transform once that is done add the spritePosition to the four vectors in vectorArray

于 2012-08-28T19:39:20.213 回答