Camera wont stay behind model after pitch, then rotation

Posted by ChocoMan on Game Development See other posts from Game Development or by ChocoMan
Published on 2012-10-10T09:48:33Z Indexed on 2012/10/10 9:53 UTC
Read the original article Hit count: 275

Filed under:
|
|

I have a camera position behind a model. Currently, if I push the left thumbstick making my model move forward, backward, or strafe, the camera stays with the model. If I push the right thumbstick left or right, the model rotates in those directions fine along with the camera rotating while maintaining its position relatively behind the model.

But when I pitch the model up or down, then rotate the model afterwards, the camera moves slightly rotates in a clock-like fashion behind the model. If I do a few rotations of the model and try to pitch the camera, the camera will eventually be looking at the side, then eventually the front of the model while also rotating in a clock-like fashion.

My question is, how do I keep the camera to pitch up and down behind the model no matter how much the model has rotated? Here is what I got:

// Rotates model and pitches camera on its own axis
    public void modelRotMovement(GamePadState pController)
    {
        // Rotates Camera with model
        Yaw = pController.ThumbSticks.Right.X * MathHelper.ToRadians(angularSpeed);

        // Pitches Camera around model
        Pitch = pController.ThumbSticks.Right.Y * MathHelper.ToRadians(angularSpeed);


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

    // Orbit (yaw) Camera around with model (only seeing back of model)
    public void cameraYaw(Vector3 axisYaw, float yaw)
    {
        ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
            Matrix.CreateFromAxisAngle(axisYaw, yaw)) + ModelLoad.camTarget;
    }
    // Raise camera above or below model's shoulders
    public void cameraPitch(Vector3 axisPitch, float pitch)
    {
        ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
            Matrix.CreateFromAxisAngle(axisPitch, pitch)) + ModelLoad.camTarget; 
    }

    // Call in update method
    public void updateCamera()
    {
        cameraYaw(Vector3.Up, Yaw);
        cameraPitch(Vector3.Right, Pitch);
    } 

NOTE: I tried to use addPitch just like addRotation but it didn't work...

© Game Development or respective owner

Related posts about XNA

Related posts about camera