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 Model named cube to which I load in LoadContent(): cube = Content.Load<Model>("untitled");.

In the Draw Method I call DrawModel:

    private void DrawModel(Model m, Matrix world)
    {
        foreach (ModelMesh mesh in m.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();
                effect.View = camera.View;
                effect.Projection = camera.Projection;
                effect.World = world;
            }
            mesh.Draw();
        }
    }

camera is of the Camera type, a class I've setup. Right now it is instantiated in the initialization section with the graphics aspect ratio and the translation (world) vector of the model, and the Draw loop calls the camera.UpdateCamera(); before drawing the models.

class Camera
{
    #region Fields
    private Matrix view; // View Matrix for Camera
    private Matrix projection; // Projection Matrix for Camera
    private Vector3 position; // Position of Camera
    private Vector3 target; // Point camera is "aimed" at
    private float aspectRatio; //Aspect Ratio for projection
    private float speed; //Speed of camera
    private Vector3 camup = Vector3.Up;
    #endregion

    #region Accessors
    /// <summary>
    /// View Matrix of the Camera -- Read Only
    /// </summary>
    public Matrix View
    {
        get { return view; }
    }
    /// <summary>
    /// Projection Matrix of the Camera -- Read Only
    /// </summary>
    public Matrix Projection
    {
        get { return projection; }
    }
    #endregion

    /// <summary>
    /// Creates a new Camera.
    /// </summary>
    /// <param name="AspectRatio">Aspect Ratio to use for the projection.</param>
    /// <param name="Position">Target coord to aim camera at.</param>
    public Camera(float AspectRatio, Vector3 Target)
    {
        target = Target;
        aspectRatio = AspectRatio;
        ResetCamera();
    }

    private void Rotate(Vector3 Axis, float Amount)
    {
        position = Vector3.Transform(position - target, Matrix.CreateFromAxisAngle(Axis, Amount)) + target;
    }

    /// <summary>
    /// Resets Default Values of the Camera
    /// </summary>
    private void ResetCamera()
    {
        speed = 0.05f;
        position = target + new Vector3(0f, 20f, 20f);
        projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, 0.5f, 100f);
        CalculateViewMatrix();
    }

    /// <summary>
    /// Updates the Camera.  Should be first thing done in Draw loop
    /// </summary>
    public void UpdateCamera()
    {
        Rotate(Vector3.Right, speed);
        CalculateViewMatrix();
    }

    /// <summary>
    /// Calculates the View Matrix for the camera
    /// </summary>
    private void CalculateViewMatrix()
    {
        view = Matrix.CreateLookAt(position,target, camup);
    }

I'm trying to create the camera so that it can orbit the center of the model. For a test I am calling Rotate(Vector3.Right, speed); but it rotates almost right but gets to a point where it "flips." If I rotate along a different axis Rotate(Vector3.Up, speed); everything seems OK in that direction.

So I guess, can someone tell me what I'm not accounting for in the above code I wrote? Or point me to an example of an orbiting camera that can be fixed on an arbitrary point?

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

It appears you might be rotating around Vector3.Right to give your camera the freedom to change pitch.

As you pitch(orbit) around your object, getting to the very top, if you then pitch some more, your camera would be upside down as it follows the orbit path. But since you have Vector3.Up (camup) as the 3rd param in your createlookat method, it is being told to always stay right side up relative to Vector3.Up. So instead of allowing the camera to proceed upside down, it flips it over as it continues the orbit path.

Most games limit the max pitch to somewhere between +89 & -89 degrees to keep the flip from happening.

share|improve this answer
    
Yea, I see what you mean now. At first it was hard for me to get a grasp on it. I decided to clamp on the pitch in the interval [-80,80]. Seemed to make the most sense. –  user1169578 Jul 10 '12 at 15:08
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.