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

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

    // 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...

UPDATE I understand the problem Im having fully. While Im facing world-North or world-South, the camera pitches around the model like normal. But lets say I'm facing world-East. The camera will still point to the model regardless of the rotation, but when I pitch the camera, it will rise and fall in a clock-like fashion around the back of the model. So the camera itself is still pitching as to world's North and South. Not the model's North and South.

share|improve this question

1 Answer 1

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

Usually, this is solved by pitching first, then yawing.. Like this:

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

The short explanation is that the axis you need to pitch around changes as you yaw, but the axis you yaw around does not change as you pitch.

here's the long explanation: http://www.screencast.com/users/sh8zen/folders/Xna/media/929e0a9a-16d1-498a-b777-8b3d85fd8a00

share|improve this answer
    
Thanks for the answer, but it didn't change anything. It's a strange behavior, because I can move in the cardinal directions (while facing the initial direction) and pitch the camera perfectly, but as soon as the model rotates, it's out of wack... Maybe something needs to updated? –  ChocoMan Oct 11 '12 at 0:42

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.