1

所以我正在用相机制作游戏,它的工作原理与模拟人生 2 的相机类似。我想要完成的是一台相机,它可以:

  • 围绕焦点旋转。
  • 提供三个必要的矩阵(World、View 和 Proj),每帧重新计算世界和视图矩阵。
  • 当位置向量发生变化时,假设它的 X 部分,它应该向左/向右移动,而不是沿 X 轴移动,如果你明白我的意思的话。
  • 相对焦点向量。
  • 但是,我在移动左/右部分时遇到问题。我会给你我拥有的代码。请注意,在这个版本的代码中,我还没有尝试实现它。

    private float worldRotation = 0;
    public void RotateAroundFocusPoint(float relativeDegrees)
    {
        worldRotation += MathHelper.ToRadians(relativeDegrees);
    }
    
    public Matrix World, View, Proj;
    public Vector3 Position, Focus /*relative*/;
    
    public void Update()
    {
        World = Matrix.CreateTranslation(-Position - Focus) *
            Matrix.CreateRotationY(worldRotation);
        View = Matrix.CreateLookAt(-Focus, Vector3.Zero, Vector3.Up);
    }
    

    在 3D 编程方面,我是一个菜鸟,所以我希望你能帮助我!

    提前致谢,

    安东尼

    4

    1 回答 1

    0

    所以我自己想通了。这是一种解决方法,但我使用以下方法实现了它:

    private float worldRotation = 0;
    public void RevolveAroundFocusPoint(float relativeDegrees)
    {
        worldRotation += MathHelper.ToRadians(relativeDegrees);
    }
    
    public Vector3 Modified
    {
        get
        {
            Vector3 f = Vector3.Transform(Focus, Matrix.Invert(Matrix.CreateRotationY(worldRotation)));
            return new Vector3(f.X, Position.Y, f.Z);
        }
    }
    public float Multiplier(float value, Vector3 v3)
    {
        return value / /*Zoom / */ (Math.Abs(v3.X) + Math.Abs(v3.Z));
    }
    public void MoveRight(float value)
    {
        Vector3 f = Modified;
        float multiplier = Multiplier(value, f);
        Position += new Vector3(f.Z * multiplier, 0, f.X * multiplier);
    }
    public void MoveLeft(float value)
    {
        Vector3 f = Modified;
        float multiplier = Multiplier(value, f);
        Position += new Vector3(-f.Z * multiplier, 0, -f.X * multiplier);
    }
    public void MoveBackward(float value)
    {
        Vector3 f = Modified;
        float multiplier = Multiplier(value, f);
        Position += new Vector3(-f.X * multiplier, 0, f.Z * multiplier);
    }
    public void MoveForward(float value)
    {
        Vector3 f = Modified;
        float multiplier = Multiplier(value, f);
        Position += new Vector3(f.X * multiplier, 0, -f.Z * multiplier);
    }
    
    public Matrix World;
    public Matrix View;
    public Matrix Proj;
    
    public Vector3 Position { get; private set; }
    public Vector3 Focus;
    
    public void Update()
    {
        World = Matrix.CreateTranslation(-Position - Focus) *
            Matrix.Invert(Matrix.CreateRotationY(worldRotation)) *
            Matrix.CreateScale(Zoom) *
            Matrix.CreateTranslation(Position + Focus);
        View = Matrix.CreateLookAt(Position, Position + Focus, Vector3.Up);
    }
    
    于 2012-06-03T09:31:47.880 回答