【问题标题】:Camera pitch in worldspace and not localspace世界空间而不是局部空间中的相机间距
【发布时间】:2012-10-11 06:32:26
【问题描述】:

如何在本地空间中投放相机?

我有一个模型,无论它在世界空间中的哪个位置,它都会在其轴上旋转。我遇到的问题是让相机俯仰相同,而不管模型的其他旋转(偏航)。目前,如果模型在世界空间中面向北方或南方,则相机会相应地倾斜。但是当模型面对任何其他基本方向时,在尝试俯仰相机后,相机在模型后面以圆周运动上下移动(因为它只识别世界空间而不是模型面对的方向)。

如何让俯仰与模型一起旋转,以便无论模型面向哪个方向?当我尝试俯仰时,相机会在模型上方移动吗?

// Rotates model and pitches camera on its own axis
    public void modelRotMovement(GamePadState pController)
    {
        /* For rotating the model left or right.
         * Camera maintains distance from model
         * throughout rotation and if model moves 
         * to a new position.
         */

        Yaw = pController.ThumbSticks.Right.X * MathHelper.ToRadians(speedAngleMAX);

        AddRotation = Quaternion.CreateFromYawPitchRoll(Yaw, 0, 0);
        ModelLoad.MRotation *= AddRotation;
        MOrientation = Matrix.CreateFromQuaternion(ModelLoad.MRotation);

        /* Camera pitches vertically around the
         * model. Problem is that the pitch is
         * in worldspace and doesn't take into
         * account if the model is rotated.
         */
        Pitch = pController.ThumbSticks.Right.Y * MathHelper.ToRadians(speedAngleMAX);
    }

    // Orbit (yaw) Camera around model
    public void cameraYaw(Vector3 axisYaw, float yaw)
    {
        ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
            Matrix.CreateFromAxisAngle(axisYaw, yaw)) + ModelLoad.camTarget;
    }

    // Pitch Camera around model
    public void cameraPitch(Vector3 axisPitch, float pitch)
    {
        ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
            Matrix.CreateFromAxisAngle(axisPitch, pitch)) + ModelLoad.camTarget; 
    }

    public void updateCamera()
    {
        cameraPitch(Vector3.Right, Pitch);
        cameraYaw(Vector3.Up, Yaw);
    }

【问题讨论】:

    标签: c# xna camera pitch


    【解决方案1】:
    >    public void cameraPitch(Vector3 axisPitch, float pitch)
    >     {
    >         ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
    >             Matrix.CreateFromAxisAngle(axisPitch, pitch)) + ModelLoad.camTarget; 
    >     }
    

    我应该在您的上一个问题中看到这一点。对不起。

    axisPitch 确实是一个全局空间向量,它需要在局部空间中。这应该可以。

       public void cameraPitch(float pitch)
        {
            Vector3 f = ModelLoad.camTarget - ModelLoad.CameraPos;
            Vector3 axisPitch = Vector3.Cross(Vector3.Up, f);
            axisPitch.Normalize();
            ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
                Matrix.CreateFromAxisAngle(axisPitch, pitch)) + ModelLoad.camTarget; 
        }
    

    【讨论】:

      猜你喜欢
      • 2016-01-07
      • 2021-12-14
      • 1970-01-01
      • 2013-04-22
      • 2021-04-07
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多