//--------------------------------------------------------------------------------------------------------------------------------------------------- // // Copyright (C)2007 DarkWynter Studios. All rights reserved. // //--------------------------------------------------------------------------------------------------------------------------------------------------- // {Contact : darkwynter.com for licensing information //--------------------------------------------------------------------------------------------------------------------------------------------------- namespace DarkWynter.Stream { #region Using Statements using System; using System.Collections; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; #endregion /// /// Camera class /// public class Camera { /// /// Viewport associated with this Camera /// public Viewport viewport; /// /// Allows user to move camera around relative the Mass's currentPosition /// public static Vector3 offset = new Vector3(0); public Vector3 firstPersonOffset = new Vector3(0, 0, 0); public Vector3 thirdPersonOffset = new Vector3(400, 900, 400); public Vector3 fixedPosition = Vector3.One; public Vector3 fixedPosition2 = new Vector3(50, 50000, 50); /// /// Camera Modes: /// Free - No Collision; mainly for development. /// FirstPerson - See through the eyes of the character. /// ThirdPerson - Follow character from a fixed distance. /// Fixed - Camera position remains constant. /// Spline - Camera moves from one point to another over time. /// public enum CameraMode { FirstPerson, ThirdPerson, FixedPosition, FixedPosition2} public CameraMode mode = CameraMode.FirstPerson; public Vector3 cameraPosition; public Vector3 lookAtPosition; public Vector3 upVec; /// /// Constructor /// public Camera() { viewport = new Viewport(); // G2L override offset = thirdPersonOffset; } private void GetOffset(bool fpvEnabled) { // First person view if (fpvEnabled) { if (offset.Length() > firstPersonOffset.Length()) { offset -= thirdPersonOffset / 100; } } // Third person view else { if (offset.Length() < thirdPersonOffset.Length()) { offset += thirdPersonOffset / 100; } } } /// /// Set the 3rd person camera max distance, /// /// public static void SetCameraLocation(Vector3 cameraLocation) { offset = cameraLocation; } /// /// Generates the View Matrix based on the mass's position and the camera offset /// /// Player's current position /// Player's normal Vector /// Player's up vector /// First Person View enabled/disabled /// View Matrix public Matrix GetViewMatrix(Vector3 currentPosition, Vector3 normalVector, Vector3 upVector, bool fpvEnabled) { GetOffset(fpvEnabled); // Calculate new camera position relative to current position cameraPosition = currentPosition - (offset * normalVector); // Calculate new lookAtPosition in the opposite direction of cameraPosition relative to current position lookAtPosition = currentPosition + normalVector + (offset * normalVector); // Use just the height value or camera goes wobbly sideways upVec = upVector + new Vector3(0, offset.Y, 0); // Make sure camera doesn't go below player position (clipping) if(cameraPosition.Y < currentPosition.Y) cameraPosition.Y = currentPosition.Y; // return final view matrix return Matrix.CreateLookAt( cameraPosition, lookAtPosition, upVec ); } /// /// Generates the projection matrix /// /// Projection matrix public Matrix GetProjectionMatrix() { return Matrix.CreatePerspectiveFieldOfView( (float)Math.PI / 4, viewport.Width / viewport.Height, 0.3f, 100000f ); } } }