Orbit Camera

Language: C#

This class implements a camera orbiting around a fixed point in space. The camera position is defined by its spherical coordinates: the radius and the polar and azimuth angles.

The OrbitCameraControler class allows movement around the spherical orbit and on the radial vector.

Code sample

/// <summary>
/// A camera orbiting around its point of interest.
/// </summary>
class OrbitCamera : Camera
{
    /// <summary>
    /// A controller for the camera
    /// </summary>
    class OrbitCameraControler : CameraControler
    {
        OrbitCamera m_camera;
        /// <summary>
        /// A controller for the camera
        /// <param name="camera">The controlled camera</param>
        /// </summary>
        public OrbitCameraControler(OrbitCamera camera)
        {
            m_camera = camera;
        }
 
        protected override void MoveForward(float amount)
        {
            m_camera.Radius -= amount;
        }
 
        protected override void MoveUp(float amount)
        {
            m_camera.Polar += (float)MathHelper.ToRadians(amount);
        }
 
        protected override void MoveRight(float amount)
        {
            m_camera.Azimuth += (float)MathHelper.ToRadians(amount);
        }
    }
 
    float m_polar = 0.0f;
    /// <summary>
    /// Gets the polar angle of the camera
    /// </summary>
    public float Polar
    {
        get { return m_polar; }
        set { m_polar = value; EyeFromParams(); }
    }
 
    float m_azimuth = 0.0f;
    /// <summary>
    /// Gets the azimuth angle of the camera
    /// </summary>
    public float Azimuth
    {
        get { return m_azimuth; }
        set { m_azimuth = value; EyeFromParams(); }
    }
 
    float m_radius = 400.0f;
    /// <summary>
    /// Gets the radial position of the camera
    /// </summary>
    public float Radius
    {
        get { return m_radius; }
        set { m_radius = value; EyeFromParams(); }
    }
 
    public OrbitCamera()
    {
        Eye = Vector3.UnitX * -1;
        Target = Vector3.Zero;
        Up = Vector3.UnitZ;
    }
 
    /// <summary>
    /// Set the camera point of interest
    /// This is the center of the camera's orbit
    /// </summary>
    public override void SetPointOfInterest(Vector3 point)    
    {
        m_target = point;
        EyeFromParams();
    }
 
    /// <summary>
    /// Compute the Eye vector from the spherical coordinates.
    /// </summary>
    protected void EyeFromParams()
    {
        //We set a maximum and minimum polar angle,
        //to avoid movement over the top and bottom poles.
        if (m_polar > Math.PI / 2 - 0.001f)
        {
            m_polar = (float)Math.PI / 2 - 0.001f;
        }
        else if (m_polar < -Math.PI / 2 + 0.001f)
        {
            m_polar = -(float)Math.PI / 2 + 0.001f;
        }
 
        //We block negative radius
        if(m_radius < 0f)
        {
            m_radius = 0f;
        }
 
        Eye = new Vector3(
            Target.X + m_radius * (float)Math.Cos(m_azimuth) * (float)Math.Cos(m_polar),
            Target.Y + m_radius * (float)Math.Sin(m_azimuth) * (float)Math.Cos(m_polar),
            Target.Z + m_radius * (float)Math.Sin(m_polar));
    }
 
    /// <summary>
    /// Create a OrbitCameraControler for the camera
    /// </summary>
    protected override CameraControler CreateControler()
    {
        return new OrbitCameraControler(this);
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License