Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using System;
- namespace Game1
- {
- internal class Camera
- {
- internal Transform transform;
- float nearPlaneDistance = 0.1f;
- internal float NearPlaneDistance
- {
- get
- {
- return nearPlaneDistance;
- }
- set
- {
- nearPlaneDistance = value;
- CreateProjection();
- }
- }
- float farPlaneDistance = 1000000000.0f;
- internal float FarPlaneDistance
- {
- get
- {
- return farPlaneDistance;
- }
- set
- {
- farPlaneDistance = value;
- CreateProjection();
- }
- }
- internal Matrix View
- {
- get { return transform.Matrix; }
- }
- Matrix projection;
- internal Matrix Projection
- {
- get { return projection; }
- }
- float fieldOfView = MathHelper.ToRadians(60f);
- internal float FieldOfView
- {
- get
- {
- return MathHelper.ToDegrees(fieldOfView);
- }
- set
- {
- fieldOfView = MathHelper.ToRadians(value);
- CreateProjection();
- }
- }
- internal Vector3 destination;
- internal float speed;
- internal Camera()
- {
- Game1.graphics.GraphicsDevice.DeviceReset += (e, s) => CreateProjection();
- Game1.window.ClientSizeChanged += (e, s) => CreateProjection();
- transform = new Transform();
- destination = new Vector3(0, 500, 0);
- transform.Position = new Vector3(0, 500, 0);
- speed = 1;
- CreateProjection();
- }
- void CreateProjection()
- {
- projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, Game1.graphics.GraphicsDevice.Viewport.AspectRatio, nearPlaneDistance, farPlaneDistance);
- Console.WriteLine("Changed Projection Matrix");
- }
- internal void Update()
- {
- SmoothMotion();
- }
- void SmoothMotion()
- {
- // Smoothly transition from current position to destination.
- if (Vector3.Distance(transform.Position, destination) > 1/300f)
- {
- transform.Position = Vector3.Lerp(transform.Position, destination, (float)Game1.gameTime.ElapsedGameTime.TotalSeconds * speed);
- }
- // Snap camera if withithin 1/300 of a unit.
- else
- {
- if (transform.Position != destination)
- {
- transform.Position = destination;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement