Frage

How do I pitch a camera in localspace?

I have a model that rotates on its its on axis regardless of where it is in worldspace. The problem I'm having is to get the camera to pitch the same regardless other rotation (yaw) of the model. Currently, if the model is facing north or south in worldspace, the camera pitches accordingly. But when the model is facing any other cardinal direction, after trying to pitch the camera, the camera moves up and down in a circular motion behind the model (because it is only recognizing worldspace and not which direction the model is facing).

How do I get the pitch to rotate with the model so that no matter which direction the model is facing? when I try to pitch the camera will move over the model?

// 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);
    }
War es hilfreich?

Lösung

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

I should have seen this in your last question. sry.

axisPitch is indeed a global space vector and it needs to be in local space. This should work.

   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; 
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top