Advertisement
Guest User

Camera.cs

a guest
Mar 26th, 2019
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using System;
  3.  
  4. namespace Game1
  5. {
  6.     internal class Camera
  7.     {
  8.         internal Transform transform;
  9.         float nearPlaneDistance = 0.1f;
  10.         internal float NearPlaneDistance
  11.         {
  12.             get
  13.             {
  14.                 return nearPlaneDistance;
  15.             }
  16.             set
  17.             {
  18.                 nearPlaneDistance = value;
  19.                 CreateProjection();
  20.             }
  21.         }
  22.         float farPlaneDistance = 1000000000.0f;
  23.         internal float FarPlaneDistance
  24.         {
  25.             get
  26.             {
  27.                 return farPlaneDistance;
  28.             }
  29.             set
  30.             {
  31.                 farPlaneDistance = value;
  32.                 CreateProjection();
  33.             }
  34.         }
  35.         internal Matrix View
  36.         {
  37.             get { return transform.Matrix; }
  38.         }
  39.         Matrix projection;
  40.         internal Matrix Projection
  41.         {
  42.             get { return projection; }
  43.         }
  44.         float fieldOfView = MathHelper.ToRadians(60f);
  45.         internal float FieldOfView
  46.         {
  47.             get
  48.             {
  49.                 return MathHelper.ToDegrees(fieldOfView);
  50.             }
  51.             set
  52.             {
  53.                 fieldOfView = MathHelper.ToRadians(value);
  54.                 CreateProjection();
  55.             }
  56.         }
  57.         internal Vector3 destination;
  58.         internal float speed;
  59.  
  60.         internal Camera()
  61.         {
  62.             Game1.graphics.GraphicsDevice.DeviceReset += (e, s) => CreateProjection();
  63.             Game1.window.ClientSizeChanged += (e, s) => CreateProjection();
  64.             transform = new Transform();
  65.             destination = new Vector3(0, 500, 0);
  66.             transform.Position = new Vector3(0, 500, 0);
  67.             speed = 1;
  68.             CreateProjection();
  69.         }
  70.         void CreateProjection()
  71.         {
  72.             projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, Game1.graphics.GraphicsDevice.Viewport.AspectRatio, nearPlaneDistance, farPlaneDistance);
  73.             Console.WriteLine("Changed Projection Matrix");
  74.         }
  75.         internal void Update()
  76.         {
  77.             SmoothMotion();
  78.         }
  79.         void SmoothMotion()
  80.         {
  81.             // Smoothly transition from current position to destination.
  82.             if (Vector3.Distance(transform.Position, destination) > 1/300f)
  83.             {
  84.                 transform.Position = Vector3.Lerp(transform.Position, destination, (float)Game1.gameTime.ElapsedGameTime.TotalSeconds * speed);
  85.             }
  86.             // Snap camera if withithin 1/300 of a unit.            
  87.             else
  88.             {
  89.                 if (transform.Position != destination)
  90.                 {
  91.                     transform.Position = destination;
  92.                 }
  93.             }            
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement