Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace gphysics
- {
- class Camera : Particle
- {
- private float mMoveSpeed;
- private float mSprintSpeed;
- private float mCurrentSpeed;
- private float mRotateSpeed;
- private float mMouseSpeed;
- private Vector3 mTarget;
- private Matrix mViewMatrix;
- private Matrix mProjectionMatrix;
- private Matrix mCameraRotation;
- public Matrix GetViewMatrix()
- {
- return mViewMatrix;
- }
- public Matrix GetProjectionMatrix()
- {
- return mProjectionMatrix;
- }
- public Matrix GetRotationMatrix()
- {
- return mCameraRotation;
- }
- public Camera()
- {
- ResetCamera();
- }
- public void ResetCamera()
- {
- mPosition = new Vector3(0.0f, 25.0f, 0.0f);
- mRotation = new Vector3(0.0f, -45.0f, 0.0f);
- mMoveSpeed = 15.0f;
- mSprintSpeed = 150.0f;
- mCurrentSpeed = mMoveSpeed;
- mRotateSpeed = 1.0f;
- mMouseSpeed = 0.001f;
- mTarget = Vector3.Zero;
- mViewMatrix = Matrix.Identity;
- mProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(65.0f), Game1.Instance.GetAspectRatio(), 1.0f, 10000000.0f);
- mCameraRotation = Matrix.Identity;
- }
- public void Update(GameTime gameTime)
- {
- DoInput(gameTime);
- UpdateViewMatrix();
- }
- private void UpdateViewMatrix()
- {
- mCameraRotation.Forward.Normalize();
- mCameraRotation.Up.Normalize();
- mCameraRotation.Right.Normalize();
- mCameraRotation *= Matrix.CreateFromAxisAngle(mCameraRotation.Right, mRotation.Y);
- mCameraRotation *= Matrix.CreateFromAxisAngle(Vector3.Up, mRotation.X);
- mCameraRotation *= Matrix.CreateFromAxisAngle(mCameraRotation.Forward, mRotation.Z);
- mRotation = Vector3.Zero;
- mTarget = mPosition + mCameraRotation.Forward;
- mViewMatrix = Matrix.CreateLookAt(mPosition, mTarget, mCameraRotation.Up);
- }
- //This should probably be handled somewhere else but for now I don't really care enough;
- private void DoInput(GameTime gameTime)
- {
- float elapsedTime = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
- KeyboardState keyboardState = Keyboard.GetState();
- MouseState mouseState = Mouse.GetState();
- Vector2 baseMousePosition = new Vector2((Game1.Instance.graphics.GraphicsDevice.Viewport.Width / 2.0f), (Game1.Instance.graphics.GraphicsDevice.Viewport.Height / 2.0f));
- Vector2 mouseChange = new Vector2(baseMousePosition.X - mouseState.X, baseMousePosition.Y - mouseState.Y);
- //Camera Rotation
- //Mouse Rotation
- mRotation.X += mouseChange.X * mMouseSpeed;
- mRotation.Y += -mouseChange.Y * mMouseSpeed;
- //Set the mouse position after we're done here.
- Mouse.SetPosition((int)(Game1.Instance.graphics.GraphicsDevice.Viewport.Width / 2.0f), (int)(Game1.Instance.graphics.GraphicsDevice.Viewport.Height / 2.0f));
- //Keyboard Rotation Keys
- if (keyboardState.IsKeyDown(Keys.J))
- {
- mRotation.X += mRotateSpeed * elapsedTime;
- }
- if (keyboardState.IsKeyDown(Keys.L))
- {
- mRotation.X += -mRotateSpeed * elapsedTime;
- }
- if (keyboardState.IsKeyDown(Keys.I))
- {
- mRotation.Y += mRotateSpeed * elapsedTime;
- }
- if (keyboardState.IsKeyDown(Keys.K))
- {
- mRotation.Y += -mRotateSpeed * elapsedTime;
- }
- //Disabled roll for the moment because it is normally not there anyway.
- //if (keyboardState.IsKeyDown(Keys.U))
- //{
- // mRotation.Z += -mRotateSpeed * elapsedTime;
- //}
- //if (keyboardState.IsKeyDown(Keys.O))
- //{
- // mRotation.Z += mRotateSpeed * elapsedTime;
- //}
- //Fast Movement
- if (keyboardState.IsKeyDown(Keys.LeftShift))
- mCurrentSpeed = mSprintSpeed;
- else
- mCurrentSpeed = mMoveSpeed;
- //Camera Movement
- if (keyboardState.IsKeyDown(Keys.W))
- {
- Move(mCameraRotation.Forward * mCurrentSpeed * elapsedTime);
- }
- if (keyboardState.IsKeyDown(Keys.S))
- {
- Move(-mCameraRotation.Forward * mCurrentSpeed * elapsedTime);
- }
- if (keyboardState.IsKeyDown(Keys.A))
- {
- Move(-mCameraRotation.Right * mCurrentSpeed * elapsedTime);
- }
- if (keyboardState.IsKeyDown(Keys.D))
- {
- Move(mCameraRotation.Right * mCurrentSpeed * elapsedTime);
- }
- //Make up and down absolute so we don't get confused.
- //Actually don't do that.
- if (keyboardState.IsKeyDown(Keys.Q))
- {
- Move(mCameraRotation.Up * mCurrentSpeed * elapsedTime);
- }
- if (keyboardState.IsKeyDown(Keys.E))
- {
- Move(-mCameraRotation.Up * mCurrentSpeed * elapsedTime);
- }
- //Reset camera button
- if (keyboardState.IsKeyDown(Keys.K))
- {
- ResetCamera();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement