3

我需要的是根据相机位置、Y 旋转和 Z 旋转来计算我的lookat 3d 矢量在哪里,我会假设任何大于 0 的数字都足以让相机的距离可以看到。

这是我用来控制相机并计算视图和投影矩阵等的静态类。

涉及职位和轮换的班级成员:

public static Vector3 Position { get; set; } //Publicly available for use outside the class
public static Vector3 Rotation { get; set; }
private static Vector3 camPos = new Vector3(0.0f, 200.0f, 300.0f); //However we work with these for manipulation of values
private static Vector3 camTarget = new Vector3(0, 0, -1200.0f);

private static float rotY = 0, rotZ = 0; //I believe that these are the correct axis following the right hand catasian coordinate system

我的相机的更新功能:

public static void Update()        
{
    //Controls here

    //Update Position here
    Position = camPos;            

    //Update camTarget based on Position, rotY and rotZ <- Need help with this bit

    //After calculating camTarget update Rotation
    Rotation = camTarget;

    UpdateMatrices();
}

UpdateMatrices() 处理所有其他事情并按应有的方式工作,但在这里它只是以防万一您想看到它:

public static void UpdateMatrices()
{
    Matrix rotationMatrix = Matrix.CreateRotationX(MathHelper.ToRadians(90));
    Vector3 transformedReference = Vector3.Transform(Vector3.Forward, rotationMatrix);

    View = Matrix.CreateLookAt(Position, Rotation, Vector3.Forward);
    Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, nearClip, farClip);
    World = Matrix.Identity;

    CamBoundingFrustrum = new BoundingFrustum(View * Projection);
}

有人愿意分享这个秘密吗?

4

1 回答 1

3

与其使用 ,不如从旋转矩阵Vector3.Forward中提取.Forward.Up向量:

    public void UpdateMatrices()
    {
        /// assumes Rotation is a Vector3 containing rotation around each axis expressed in radians
        Matrix rotationMatrix = Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z);
        /// View = Matrix.CreateLookAt(Position, Rotation, Vector3.Forward);

        View = Matrix.CreateLookAt(Position, Position + rotationMatrix.Forward, rotationMatrix.Up);
        Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, nearClip, farClip);

        /// World matrix needs to match your translation and rotation used above
        /// XNA expects matrices to be concatenated in SRT (scale, rotation, then translate) order:
        World = rotationMatrix * Matrix.CreateTranslation(Position);

        CamBoundingFrustrum = new BoundingFrustum(View * Projection);
    }

请注意,您必须将相机的平移(通过您存储的向量,或从相机的世界矩阵.Translation属性)添加到RotationMatrix.Forward以获得真实目标。

矩阵.Forward.Backward.Left.Right.Up和属性与旋转矩阵变换.Down的等效属性相同。Vector3所以,RotationMatrix.Forward指向你正在看的方向,与那个方向.Up正交,等等。

如果您只使用Vector3.Forward,则不会得到转换后的向量,并且可能会发生奇怪的事情。

其他注意事项:

  1. 不要让一切都是静态的。在此期间创建相机类的实例Game.Initialize()并将其添加为属性。static应该谨慎使用——它会导致各种有趣的线程问题,让你发疯。
  2. 我还建议切换到四元数而不是偏航/滚动/俯仰(如果您还没有的话),因为它可以避免万向节锁定,但这是一个不同的话题。
于 2013-04-01T15:43:55.623 回答