相机间距在世界空间而不是本地空间

时间:2012-10-11 06:32:27

标签: c# xna camera pitch

如何在当地空间投放相机?

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

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

// 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);
    }

1 个答案:

答案 0 :(得分:1)

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

我应该在你的上一个问题中看到这一点。 SRY。

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; 
    }