//--------------------------------------------------------------------------------------------------------------------------------------------------- // // 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(700, 1400, 700); private Vector3 firstPersonOffset = new Vector3(0, 0, 0); private Vector3 thirdPersonOffset = new Vector3(700, 1400, 700); /// /// Constructor /// public Camera() { viewport = new Viewport(); } 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 Vector3 cameraPosition = currentPosition - (offset * normalVector); // Calculate new lookAtPosition in the opposite direction of cameraPosition relative to current position Vector3 lookAtPosition = currentPosition + normalVector + (offset * normalVector); // Use just the height value or camera goes wobbly sideways Vector3 upVec = upVector + new Vector3(0, offset.Y, 0); // Make sure camera doesn't go below player position (clipping) float buffer = 50; if(cameraPosition.Y < currentPosition.Y + buffer) cameraPosition.Y = currentPosition.Y + buffer; // 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 ); } } }