1

我的 XNA 游戏使用 Farseer Physics,这是一个 2d 物理引擎,带有用于物理引擎数据的可选渲染器,以帮助您进行调试。可视化调试数据非常有用,因此我将其设置为根据相机的状态进行绘制。这完美地工作,除了z轴旋转。看,我有一个支持移动、缩放和 z 轴旋转的相机类。我的调试类使用 Farseer 的调试渲染器创建矩阵,使调试数据根据相机绘制,它做得很好,除了一件事.. z 轴旋转使用屏幕的左上角 ( 0, 0),而我的相机使用视口的中心作为 (0, 0) 旋转。有人对我有什么建议吗?如果我可以让调试抽屉从中心旋转,它将与我的相机完美配合。

        public void Draw(Camera2D camera, GraphicsDevice graphicsDevice)
    {
        // Projection (location and zoom)
        float width = (1f / camera.Zoom) * ConvertUnits.ToSimUnits(graphicsDevice.Viewport.Width / 2);
        float height = (-1f / camera.Zoom) * ConvertUnits.ToSimUnits(graphicsDevice.Viewport.Height / 2);
        //projection = Matrix.CreateOrthographic(width, height, 1f, 1000000f);
        projection = Matrix.CreateOrthographicOffCenter(
            -width,
            width,
            -height,
            height,
            0f, 1000000f);

        // View (translation and rotation)
        float xTranslation = -1 * ConvertUnits.ToSimUnits(camera.Position.X);
        float yTranslation = -1 * ConvertUnits.ToSimUnits(camera.Position.Y);
        Vector3 translationVector = new Vector3(xTranslation, yTranslation, 0f);
        view = Matrix.CreateRotationZ(camera.Rotation) * Matrix.Identity;
        view.Translation = translationVector;

        DebugViewXNA.RenderDebugData(ref projection, ref view);
    }
4

2 回答 2

0

一般来说,为了执行围绕点 (x, y, z) 的旋转,需要将操作分解为 3 个概念部分:

  1. T 是一个平移矩阵,通过 (-x, -y, -z) 进行平移
  2. R 是围绕相关轴旋转的旋转矩阵。
  3. T^-1 是转换回 (x, y, z) 的矩阵

您所追求的矩阵是这 3 个相乘的结果,以相反的顺序:

M = T^-1 * R ^ T

您应该使用的 x,y,z 是相机的位置。

于 2011-07-13T19:46:47.230 回答
0

解决此类问题的一种常见方法是将相关对象移动到“中心”,旋转并将其移回。所以在这种情况下,我建议应用一个转换,将相机“向上和穿过”屏幕尺寸的一半,应用旋转然后将其移回。

于 2011-07-13T04:38:14.177 回答