Advertisement
Guest User

XNA Camera

a guest
Feb 4th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8.  
  9. namespace gphysics
  10. {
  11.     class Camera : Particle
  12.     {
  13.         private float mMoveSpeed;
  14.         private float mSprintSpeed;
  15.         private float mCurrentSpeed;
  16.         private float mRotateSpeed;
  17.         private float mMouseSpeed;
  18.  
  19.         private Vector3 mTarget;
  20.  
  21.         private Matrix mViewMatrix;
  22.         private Matrix mProjectionMatrix;
  23.         private Matrix mCameraRotation;
  24.  
  25.         public Matrix GetViewMatrix()
  26.         {
  27.             return mViewMatrix;
  28.         }
  29.  
  30.         public Matrix GetProjectionMatrix()
  31.         {
  32.             return mProjectionMatrix;
  33.         }
  34.  
  35.         public Matrix GetRotationMatrix()
  36.         {
  37.             return mCameraRotation;
  38.         }
  39.  
  40.         public Camera()
  41.         {
  42.             ResetCamera();
  43.         }
  44.  
  45.         public void ResetCamera()
  46.         {
  47.             mPosition = new Vector3(0.0f, 25.0f, 0.0f);
  48.             mRotation = new Vector3(0.0f, -45.0f, 0.0f);
  49.  
  50.             mMoveSpeed = 15.0f;
  51.             mSprintSpeed = 150.0f;
  52.             mCurrentSpeed = mMoveSpeed;
  53.             mRotateSpeed = 1.0f;
  54.             mMouseSpeed = 0.001f;
  55.  
  56.             mTarget = Vector3.Zero;
  57.  
  58.             mViewMatrix = Matrix.Identity;
  59.             mProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(65.0f), Game1.Instance.GetAspectRatio(), 1.0f, 10000000.0f);
  60.             mCameraRotation = Matrix.Identity;
  61.         }
  62.  
  63.         public void Update(GameTime gameTime)
  64.         {
  65.             DoInput(gameTime);
  66.             UpdateViewMatrix();
  67.         }
  68.  
  69.         private void UpdateViewMatrix()
  70.         {
  71.             mCameraRotation.Forward.Normalize();
  72.             mCameraRotation.Up.Normalize();
  73.             mCameraRotation.Right.Normalize();
  74.  
  75.             mCameraRotation *= Matrix.CreateFromAxisAngle(mCameraRotation.Right, mRotation.Y);
  76.             mCameraRotation *= Matrix.CreateFromAxisAngle(Vector3.Up, mRotation.X);
  77.             mCameraRotation *= Matrix.CreateFromAxisAngle(mCameraRotation.Forward, mRotation.Z);
  78.  
  79.             mRotation = Vector3.Zero;
  80.  
  81.             mTarget = mPosition + mCameraRotation.Forward;
  82.  
  83.             mViewMatrix = Matrix.CreateLookAt(mPosition, mTarget, mCameraRotation.Up);
  84.         }
  85.  
  86.         //This should probably be handled somewhere else but for now I don't really care enough;
  87.         private void DoInput(GameTime gameTime)
  88.         {
  89.             float elapsedTime = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
  90.  
  91.             KeyboardState keyboardState = Keyboard.GetState();
  92.             MouseState mouseState = Mouse.GetState();
  93.             Vector2 baseMousePosition = new Vector2((Game1.Instance.graphics.GraphicsDevice.Viewport.Width / 2.0f), (Game1.Instance.graphics.GraphicsDevice.Viewport.Height / 2.0f));
  94.             Vector2 mouseChange = new Vector2(baseMousePosition.X - mouseState.X, baseMousePosition.Y - mouseState.Y);
  95.  
  96.             //Camera Rotation
  97.             //Mouse Rotation
  98.             mRotation.X += mouseChange.X * mMouseSpeed;
  99.             mRotation.Y += -mouseChange.Y * mMouseSpeed;
  100.             //Set the mouse position after we're done here.
  101.             Mouse.SetPosition((int)(Game1.Instance.graphics.GraphicsDevice.Viewport.Width / 2.0f), (int)(Game1.Instance.graphics.GraphicsDevice.Viewport.Height / 2.0f));
  102.  
  103.             //Keyboard Rotation Keys
  104.             if (keyboardState.IsKeyDown(Keys.J))
  105.             {
  106.                 mRotation.X += mRotateSpeed * elapsedTime;
  107.             }
  108.             if (keyboardState.IsKeyDown(Keys.L))
  109.             {
  110.                 mRotation.X += -mRotateSpeed * elapsedTime;
  111.             }
  112.             if (keyboardState.IsKeyDown(Keys.I))
  113.             {
  114.                 mRotation.Y += mRotateSpeed * elapsedTime;
  115.             }
  116.             if (keyboardState.IsKeyDown(Keys.K))
  117.             {
  118.                 mRotation.Y += -mRotateSpeed * elapsedTime;
  119.             }
  120.             //Disabled roll for the moment because it is normally not there anyway.
  121.             //if (keyboardState.IsKeyDown(Keys.U))
  122.             //{
  123.             //    mRotation.Z += -mRotateSpeed * elapsedTime;
  124.             //}
  125.             //if (keyboardState.IsKeyDown(Keys.O))
  126.             //{
  127.             //    mRotation.Z += mRotateSpeed * elapsedTime;
  128.             //}
  129.  
  130.             //Fast Movement
  131.             if (keyboardState.IsKeyDown(Keys.LeftShift))
  132.                 mCurrentSpeed = mSprintSpeed;
  133.             else
  134.                 mCurrentSpeed = mMoveSpeed;
  135.  
  136.             //Camera Movement
  137.             if (keyboardState.IsKeyDown(Keys.W))
  138.             {
  139.                 Move(mCameraRotation.Forward * mCurrentSpeed * elapsedTime);
  140.             }
  141.             if (keyboardState.IsKeyDown(Keys.S))
  142.             {
  143.                 Move(-mCameraRotation.Forward * mCurrentSpeed * elapsedTime);
  144.             }
  145.             if (keyboardState.IsKeyDown(Keys.A))
  146.             {
  147.                 Move(-mCameraRotation.Right * mCurrentSpeed * elapsedTime);
  148.             }
  149.             if (keyboardState.IsKeyDown(Keys.D))
  150.             {
  151.                 Move(mCameraRotation.Right * mCurrentSpeed * elapsedTime);
  152.             }
  153.             //Make up and down absolute so we don't get confused.
  154.             //Actually don't do that.
  155.            
  156.             if (keyboardState.IsKeyDown(Keys.Q))
  157.             {
  158.                 Move(mCameraRotation.Up * mCurrentSpeed * elapsedTime);
  159.             }
  160.             if (keyboardState.IsKeyDown(Keys.E))
  161.             {
  162.                 Move(-mCameraRotation.Up * mCurrentSpeed * elapsedTime);
  163.             }
  164.  
  165.             //Reset camera button
  166.             if (keyboardState.IsKeyDown(Keys.K))
  167.             {
  168.                 ResetCamera();
  169.             }
  170.            
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement