Advertisement
Guest User

3D camera for C#/XNA by Superbest

a guest
Feb 6th, 2012
2,671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2.  
  3. namespace SeaBaseAlpha.Movables
  4. {
  5.     /// <summary>
  6.     /// Just another 3D camera. The controls use a spaceship flying in space
  7.     /// analogy.
  8.     ///
  9.     /// By Superbest, http://seabasealpha.wordpress.com/
  10.     /// </summary>
  11.     class Camera3D
  12.     {
  13.         public Vector3 Position { get; private set; }
  14.         public Vector3 Up { get; private set ; }
  15.         public Vector3 Forward { get; private set; }
  16.  
  17.         /// <summary>
  18.         /// Construct a view matrix corresponding to this camera.
  19.         /// </summary>
  20.         public Matrix ViewMatrix
  21.         {
  22.             get
  23.             {
  24.                 return Matrix.CreateLookAt(Position, Forward+Position, Up);
  25.             }
  26.         }
  27.  
  28.         /// <summary>
  29.         ///
  30.         /// </summary>
  31.         /// <param name="position">Position of the camera</param>
  32.         /// <param name="forward">Direction which the camera looks in, equal to target - position</param>
  33.         /// <param name="up">Up direction of the camera</param>
  34.         public Camera3D(Vector3 position, Vector3 forward, Vector3 up)
  35.         {
  36.             Position = position;
  37.             Forward = forward;
  38.             Up = up;
  39.         }
  40.  
  41.         /// <summary>
  42.         /// Move forward with respect to camera
  43.         /// </summary>
  44.         /// <param name="amount"></param>
  45.         public void Thrust(float amount)
  46.         {
  47.             Forward.Normalize();
  48.             Position += Forward*amount;
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Strafe left with respect to camera
  53.         /// </summary>
  54.         /// <param name="amount"></param>
  55.         public void StrafeHorz(float amount)
  56.         {
  57.             var left = Vector3.Cross(Up, Forward);
  58.             left.Normalize();
  59.             Position += left*amount;
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Strafe up with respect to camera
  64.         /// </summary>
  65.         /// <param name="amount"></param>
  66.         public void StrafeVert(float amount)
  67.         {
  68.             Up.Normalize();
  69.             Position += Up*amount;
  70.         }
  71.  
  72.         /// <summary>
  73.         /// Roll (around forward axis) clockwise with respect to camera
  74.         /// TODO: Is it CW or CCW?
  75.         /// </summary>
  76.         /// <param name="amount">Angle in degrees</param>
  77.         public void Roll(float amount)
  78.         {
  79.             Up.Normalize();
  80.             var left = Vector3.Cross(Up, Forward);
  81.             left.Normalize();
  82.  
  83.             Up = Vector3.Transform(Up, Matrix.CreateFromAxisAngle(Forward, MathHelper.ToRadians(amount)));
  84.         }
  85.        
  86.         /// <summary>
  87.         /// Yaw (turn/steer around up vector) to the left
  88.         /// </summary>
  89.         /// <param name="amount">Angle in degrees</param>
  90.         public void Yaw(float amount)
  91.         {
  92.             Forward.Normalize();
  93.  
  94.             Forward = Vector3.Transform(Forward, Matrix.CreateFromAxisAngle(Up, MathHelper.ToRadians(amount)));
  95.         }
  96.  
  97.         /// <summary>
  98.         /// Pitch (around leftward axis) upward
  99.         /// </summary>
  100.         /// <param name="amount"></param>
  101.         public void Pitch(float amount)
  102.         {
  103.             Forward.Normalize();
  104.             var left = Vector3.Cross(Up, Forward);
  105.             left.Normalize();
  106.  
  107.             Forward = Vector3.Transform(Forward, Matrix.CreateFromAxisAngle(left, MathHelper.ToRadians(amount)));
  108.             Up = Vector3.Transform(Up, Matrix.CreateFromAxisAngle(left, MathHelper.ToRadians(amount)));
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement