Advertisement
Guest User

Sandbox - TrackingCamera

a guest
Mar 5th, 2012
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.97 KB | None | 0 0
  1. namespace Sandbox.Graphics
  2. {
  3.     using Microsoft.Xna.Framework;
  4.     using Microsoft.Xna.Framework.Graphics;
  5.     using System;
  6.  
  7.     /// <summary>
  8.     /// Represents a camera in a three dimensional world that tracks a <see cref="Sandbox.Graphics.GameModel"/>.
  9.     /// </summary>
  10.     public class TrackingCamera : GameComponent, ICamera
  11.     {
  12.         private const float DEFAULT_DISTANCE_FROM_TARGET = 100.0f;
  13.         private const float DEFAULT_NEAR_PLANE_DISTANCE = 0.1f;
  14.         private const float DEFAULT_FAR_PLANE_DISTANCE = 300.0f;
  15.  
  16.         private GameModel mTarget;
  17.  
  18.         private Matrix mWorldMatrix;
  19.         private Matrix mViewMatrix;
  20.         private Matrix mProjectionMatrix;
  21.  
  22.         private Vector3 mPosition = Vector3.Zero;
  23.         private Vector3 mLookAt = Vector3.Zero;
  24.  
  25.         private float mDistanceBehindTarget = DEFAULT_DISTANCE_FROM_TARGET;
  26.         private float mDistanceAboveTarget = DEFAULT_DISTANCE_FROM_TARGET;
  27.         private float mNearClippingPlaneDistance = DEFAULT_NEAR_PLANE_DISTANCE;
  28.         private float mFarClippingPlaneDistance = DEFAULT_FAR_PLANE_DISTANCE;
  29.  
  30.         public TrackingCamera(Game game)
  31.             : base(game)
  32.         {
  33.             // Perform initial matrix calculations
  34.             CalculateMatrices();
  35.         }
  36.  
  37.         #region Properties
  38.  
  39.         /// <summary>
  40.         /// Gets or sets the distance the camera should be positioned behind the target.
  41.         /// </summary>
  42.         public float DistanceBehindTarget
  43.         {
  44.             get { return mDistanceBehindTarget; }
  45.             set { mDistanceBehindTarget = value; }
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Gets or sets the distance the camera should be positioned above the target.
  50.         /// </summary>
  51.         public float DistanceAboveTarget
  52.         {
  53.             get { return mDistanceAboveTarget; }
  54.             set { mDistanceAboveTarget = value; }
  55.         }
  56.  
  57.         #endregion
  58.  
  59.         #region ICamera Implementation
  60.  
  61.         /// <inherit />
  62.         public Matrix World
  63.         {
  64.             get { return mWorldMatrix; }
  65.         }
  66.  
  67.         /// <inherit />
  68.         public Matrix View
  69.         {
  70.             get { return mViewMatrix; }
  71.         }
  72.  
  73.         /// <inherit />
  74.         public Matrix Projection
  75.         {
  76.             get { return mProjectionMatrix; }
  77.         }
  78.  
  79.         /// <inherit />
  80.         public float NearClippingPlaneDistance
  81.         {
  82.             get { return mNearClippingPlaneDistance; }
  83.             set
  84.             {
  85.                 // Make sure the value has changed
  86.                 if (mNearClippingPlaneDistance != value)
  87.                 {
  88.                     mNearClippingPlaneDistance = value;
  89.  
  90.                     // Re-calculate the projection matrix
  91.                     CalculateProjectionMatrix();
  92.                 }
  93.             }
  94.         }
  95.  
  96.         /// <inherit />
  97.         public float FarClippingPlaneDistance
  98.         {
  99.             get { return mFarClippingPlaneDistance; }
  100.             set
  101.             {
  102.                 // Make sure the value has changed
  103.                 if (mFarClippingPlaneDistance != value)
  104.                 {
  105.                     mFarClippingPlaneDistance = value;
  106.  
  107.                     // Re-calculate the projection matrix
  108.                     CalculateProjectionMatrix();
  109.                 }
  110.             }
  111.         }
  112.  
  113.         #endregion
  114.  
  115.         #region GameComponent Overrides
  116.  
  117.         public override void Update(GameTime gameTime)
  118.         {
  119.             if (mTarget != null)
  120.             {
  121.                 UpdateTrackingInformation();
  122.                 CalculateViewMatrix();
  123.             }
  124.  
  125.             base.Update(gameTime);
  126.         }
  127.  
  128.         #endregion
  129.  
  130.         #region Tracking
  131.  
  132.         /// <summary>
  133.         /// Specifies the <see cref="Sandbox.Graphics.GameModel"/> to track.
  134.         /// </summary>
  135.         /// <param name="target">The <see cref="Sandbox.Graphics.GameModel"/> to track.</param>
  136.         public void Track(GameModel target)
  137.         {
  138.             mTarget = target;
  139.         }
  140.  
  141.         #endregion
  142.  
  143.         #region Calculations
  144.  
  145.         private void CalculateMatrices()
  146.         {
  147.             CalculateWorldMatrix();
  148.             CalculateViewMatrix();
  149.             CalculateProjectionMatrix();
  150.         }
  151.  
  152.         private void CalculateWorldMatrix()
  153.         {
  154.             mWorldMatrix = Matrix.Identity;
  155.         }
  156.  
  157.         private void CalculateViewMatrix()
  158.         {
  159.             mViewMatrix = Matrix.CreateLookAt(mPosition, mLookAt, Vector3.Up);
  160.         }
  161.  
  162.         private void CalculateProjectionMatrix()
  163.         {
  164.             Viewport viewport = this.Game.GraphicsDevice.Viewport;
  165.  
  166.             // Calculate the projection matrix parameters
  167.             float fieldOfView = MathHelper.PiOver4; // 45 degrees in radians
  168.             float aspectRatio = viewport.Width / viewport.Height;
  169.             float nearClippingPlaneDistance = this.NearClippingPlaneDistance;
  170.             float farClippingPlaneDistance = this.FarClippingPlaneDistance;
  171.  
  172.             // The projection matrix represents the focal projection from a camera lense
  173.             mProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(fieldOfView, aspectRatio, nearClippingPlaneDistance, farClippingPlaneDistance);
  174.         }
  175.  
  176.         private void UpdateTrackingInformation()
  177.         {
  178.             // Calculate the offset vector
  179.             Vector3 offset = new Vector3(0.0f, this.DistanceAboveTarget, -this.DistanceBehindTarget);
  180.  
  181.             // Transform the offset by the target's rotation
  182.             float targetRotation = mTarget.Rotation.Y;
  183.             Matrix rotationMatrix = Matrix.CreateRotationY(targetRotation);
  184.  
  185.             Vector3 transformedOffset = Vector3.Transform(offset, rotationMatrix);
  186.  
  187.             // Calculate the new position and look at vectors
  188.             Vector3 targetPosition = mTarget.Position;
  189.             mLookAt = targetPosition;
  190.  
  191.             mPosition = targetPosition + transformedOffset;
  192.         }
  193.  
  194.         #endregion
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement