【问题标题】:Changing the origin of a rotation matrix in XNA在 XNA 中更改旋转矩阵的原点
【发布时间】:2012-08-28 19:22:05
【问题描述】:

我试图让代表精灵的 4 个角的 4 个 Vector2 对象围绕精灵本身围绕其中心旋转。然而,使用下面的代码,Vector2 对象在客户端空间中围绕 0,0 旋转,而不是围绕对象的中心旋转。使用矩阵变换,有什么方法可以让 Vector2 对象围绕对象的中心而不是全局坐标 (0,0) 旋转?

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

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;

    }

【问题讨论】:

  • 精灵的位置对应于它的右下角这一事实让我相信你在调用SpriteBatch.Draw()时指定了错误的原点。
  • @ColeCampbell 我在SpriteBatch.Draw() 调用中将精灵的中心指定为原点。这允许 SpriteBatch 围绕对象的中心旋转精灵,就像我尝试对 Vector2 对象做的那样。我也需要解决位置问题,但它没有我的矩阵问题那么严重,我觉得我可以在以后解决它。

标签: matrix xna rotation


【解决方案1】:

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

只需将您的四个角变成spriteSize 的相应组合,然后执行Vector2.TransformspritePosition 添加到vectorArray 中的四个向量中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 2020-10-21
    相关资源
    最近更新 更多