Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a camera behind a model (3rd Person) and I'm having problems KEEPING it behind the model. When I first start my game, you see the back of the model. If the model moves forward, backward or strafe left or right, the camera moves along accordingly. When the model rotates (stationary), the camera rotates accordingly with the model still pointing at the model's back. So far, so good.

The problem comes when the player is BOTH moving and rotating at the same time. Take for example a model moving in a circular pattern like running around a track. As the model moves in this motion, the model rotates slightly more with each complete rotation. Eventually, instead of looking at the model's back, eventually you will see the model in a profile view and before you know it, the model's front is facing the camera. And when you stop moving the model, the model stays in that position.

So, as long as my model is stationary and rotating in one place, the camera rotates correctly. But as soon as there is any sort movement while rotating, the model is offset by a mysterious increasing amount. How can I keep the camera maintaining the same view no matter how I move AND rotate at the same time?

 // 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.CreateFromAxisAngle(Vector3.Up, yaw);
        //AddRotation = Quaternion.CreateFromYawPitchRoll(Yaw, 0, 0);
        ModelLoad.MRotation *= AddRotation;
        MOrientation = Matrix.CreateFromQuaternion(ModelLoad.MRotation);

        Pitch = pController.ThumbSticks.Right.Y * MathHelper.ToRadians(speedAngleMAX);
        AddPitch = Quaternion.CreateFromAxisAngle(Vector3.Up, pitch);
        ModelLoad.CRotation *= AddPitch;
        COrientation = Matrix.CreateFromQuaternion(ModelLoad.CRotation);
    }


// Orbit (yaw) Camera around model
    public void cameraYaw(float yaw)
    {
        Vector3 yawAngle = ModelLoad.CameraPos - ModelLoad.camTarget;
        Vector3 axisYaw = Vector3.Up;

        ModelLoad.CameraPos = Vector3.Transform(yawAngle,
            Matrix.CreateFromAxisAngle(axisYaw, yaw)) + ModelLoad.camTarget;
    }

    public void updateCamera()
    {
        cameraYaw(Yaw);
        cameraPitch(Pitch); 
    }
share|improve this question
    
when are you calling cameraYaw? Also It's not entirely clear if you camera is rotating too much, or the model is rotating too much. Can you clarify. –  Ken Oct 12 '12 at 9:03
    
Just out of curiosity (and a possibility it affects your issue) why are you pitching around the Yaw axis. Both your AddRotation and AddPitch are causing rotations around the Vector3.Up axis. That seems correct for yaw, but should't AddPitch use COrientation.Right instead? –  Steve H Oct 12 '12 at 13:59
    
possible duplicate of Camera wont stay behind model after pitch, then rotation –  Byte56 Oct 12 '12 at 14:32
    
@SteveH Turns out that AddPitch and COrientation has no affect. I removed it and I still recieved the same results. –  ChocoMan Oct 12 '12 at 19:02
    
@Ken I had to move the updateCamera() above my rotation and movement methods. That fixed my rotation with movement problem (partially). Now the only problem I have with the while traveling in a circle, the camera sways towards wider then closer smoothly. Instead of the model traveling in a circle with the camera traveling in a bigger circle, the camera looks like its making a shape of an egg every 360 degrees. –  ChocoMan Oct 12 '12 at 21:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.