xna orbit camera troubles

Posted by user17753 on Game Development See other posts from Game Development or by user17753
Published on 2012-07-09T15:36:28Z Indexed on 2012/07/09 21:24 UTC
Read the original article Hit count: 405

Filed under:
|
|

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?

© Game Development or respective owner

Related posts about c#

Related posts about xna-4.0