Guest User

Camera.cs - WindowsPhoneDemoGame

a guest
Apr 12th, 2010
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Input.Touch;
  11. using Microsoft.Xna.Framework.Media;
  12.  
  13. namespace WindowsPhoneGameDemoObjects
  14. {
  15.     public class Camera : DrawableGameComponent
  16.     {
  17.         private float movementSpeed = 250f;
  18.         private float turnSpeed = 0.4f;
  19.  
  20.         public bool DenyVerticalMovement = false;
  21.  
  22.         private const float maxPitch = MathHelper.PiOver2 - 0.3f;
  23.  
  24.         public Matrix Projection { get; protected set; }
  25.         public Matrix View { get; protected set; }
  26.         public float NearPlane { get { return nearPlane; } set { nearPlane = value; CreateProjection(); } }
  27.         public float FarPlane { get { return farPlane; } set { farPlane = value; CreateProjection(); } }
  28.  
  29.         protected float nearPlane = 1;
  30.         protected float farPlane = 10000;
  31.  
  32.         public BoundingFrustum Frustrum { get; protected set; }
  33.  
  34.         public Vector3 Position;
  35.         public Vector3 Angle;
  36.  
  37.         public Camera(Game game)
  38.             : base(game)
  39.         {
  40.             CreateProjection();
  41.         }
  42.  
  43.  
  44.  
  45.         public override void Initialize()
  46.         {
  47.             base.Initialize();
  48.         }
  49.  
  50.         private void CreateProjection()
  51.         {
  52.             Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.Pi / 4, 1.66f, nearPlane, farPlane);
  53.         }
  54.  
  55.         protected override void LoadContent()
  56.         {
  57.             base.LoadContent();
  58.         }
  59.  
  60.         GameTime touchStartTime;
  61.         private void UpdateTouch(GameTime gameTime)
  62.         {
  63.             TouchCollection tc = TouchPanel.GetState();
  64.             if (tc.Count > 0)
  65.             {
  66.                 TouchLocation current = tc[0];
  67.                 TouchLocation prev;
  68.                 if (current.TryGetPreviousLocation(out prev))
  69.                 {
  70.                     Angle.X -= MathHelper.ToRadians((current.Position.X - prev.Position.X) * turnSpeed); // pitch
  71.                     Angle.Y += MathHelper.ToRadians((current.Position.Y - prev.Position.Y) * turnSpeed); // yaw
  72.  
  73.                     while (Angle.Y > MathHelper.Pi * 2)
  74.                         Angle.Y -= MathHelper.Pi * 2;
  75.                     while (Angle.Y < -MathHelper.Pi * 2)
  76.                         Angle.Y += MathHelper.Pi * 2;
  77.  
  78.                     if (Angle.X > maxPitch)
  79.                         Angle.X = maxPitch;
  80.  
  81.                     if (Angle.X < -maxPitch)
  82.                         Angle.X = -maxPitch;
  83.  
  84.                     float time = (float)gameTime.ElapsedGameTime.TotalSeconds;
  85.                     Vector3 forward = -Vector3.Normalize(new Vector3((float)Math.Sin(-Angle.Y),
  86.                         (float)Math.Sin(Angle.X),
  87.                         (float)Math.Cos(-Angle.Y)));
  88.                     if (DenyVerticalMovement)
  89.                     {
  90.                         forward = new Vector3(forward.X, 0, forward.Z);
  91.                         forward.Normalize();
  92.                     }
  93.                     Position += forward * movementSpeed * time;
  94.                 }
  95.                 else
  96.                     touchStartTime = gameTime;
  97.             }
  98.         }
  99.  
  100.         public override void Update(GameTime gameTime)
  101.         {
  102.  
  103.             UpdateTouch(gameTime);
  104.             RecalcView();
  105.  
  106.             base.Update(gameTime);
  107.         }
  108.  
  109.         protected void RecalcView()
  110.         {
  111.             View = Matrix.Identity;
  112.             View *= Matrix.CreateTranslation(-Position);
  113.             View *= Matrix.CreateRotationZ(Angle.Z);
  114.             View *= Matrix.CreateRotationY(Angle.Y);
  115.             View *= Matrix.CreateRotationX(Angle.X);
  116.  
  117.             Frustrum = new BoundingFrustum(Matrix.Multiply(View, Projection));
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment