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.

Currently, my camera rotates with my model's Y-Axis (yaw) perfectly. What I'm having trouble with is rotating the X-Axis (pitch) along with it. I've tried the same method for cameraYaw() in the form of cameraPitch(), while adjusting the axis to Vector.Right, but the camera wouldn't pitch at all in accordance to the Y-Axes of the controller.

Is there a way similar to this to get the same effect for pitching the camera around the model?

// Rotates model on its own Y-axis
    public void modelRotMovement(GamePadState pController)
    {
        Yaw = pController.ThumbSticks.Right.X * MathHelper.ToRadians(speedAngleMAX);

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

    // Orbit (yaw) Camera around model (camTarget is the model's position)
    public void cameraYaw(Vector3 axis, float yaw)
    {

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

    public void updateCamera()
    {
        cameraYaw(Vector3.Up, Yaw);
    }
share|improve this question
add comment

1 Answer

It looks to me like all you would need to do to pitch around an object is call the cameraYaw method with as first argument Vector3.Backward. This way you will rotate around the Z axis (pitching) instead of around the Y axis (yawing).

If you want to both pitch and roll at the same time you will need to create an arcball camera. I've written a code snippet for XNA that gives you a very simple arcball camera. Just set the look at to where your model is, use zoom to zoom out from your model and start manipulating the pitch and yaw variables http://roy-t.nl/index.php/2010/02/21/xna-simple-arcballcamera/

To not leave you with just a link here is the most important part of the code.

            //Calculate the relative position of the camera                        
            position = Vector3.Transform(Vector3.Backward, Matrix.CreateFromYawPitchRoll(yaw, pitch, 0));
            //Convert the relative position to the absolute position
            position *= zoom;
            position += lookAt;

            //Calculate a new viewmatrix
            viewMatrix = Matrix.CreateLookAt(position, lookAt, Vector3.Up);

As you can see I first transform a vector that points to (0,0,1), that is towards the player sitting behind the monitor, given a yaw, pitch and roll. After that I still have a normalized vector but it's pointing to where I want the camera to be. I then scale the vector so that the camera is the distance from the model that. After that I move the vector so that it orbits the model instead of (0,0,0). Finally I create the viewmatrix that you can pass to your effect.

share|improve this answer
add comment

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.