Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Input.Touch;
- using Microsoft.Xna.Framework.Media;
- namespace WindowsPhoneGameDemoObjects
- {
- public class Camera : DrawableGameComponent
- {
- private float movementSpeed = 250f;
- private float turnSpeed = 0.4f;
- public bool DenyVerticalMovement = false;
- private const float maxPitch = MathHelper.PiOver2 - 0.3f;
- public Matrix Projection { get; protected set; }
- public Matrix View { get; protected set; }
- public float NearPlane { get { return nearPlane; } set { nearPlane = value; CreateProjection(); } }
- public float FarPlane { get { return farPlane; } set { farPlane = value; CreateProjection(); } }
- protected float nearPlane = 1;
- protected float farPlane = 10000;
- public BoundingFrustum Frustrum { get; protected set; }
- public Vector3 Position;
- public Vector3 Angle;
- public Camera(Game game)
- : base(game)
- {
- CreateProjection();
- }
- public override void Initialize()
- {
- base.Initialize();
- }
- private void CreateProjection()
- {
- Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.Pi / 4, 1.66f, nearPlane, farPlane);
- }
- protected override void LoadContent()
- {
- base.LoadContent();
- }
- GameTime touchStartTime;
- private void UpdateTouch(GameTime gameTime)
- {
- TouchCollection tc = TouchPanel.GetState();
- if (tc.Count > 0)
- {
- TouchLocation current = tc[0];
- TouchLocation prev;
- if (current.TryGetPreviousLocation(out prev))
- {
- Angle.X -= MathHelper.ToRadians((current.Position.X - prev.Position.X) * turnSpeed); // pitch
- Angle.Y += MathHelper.ToRadians((current.Position.Y - prev.Position.Y) * turnSpeed); // yaw
- while (Angle.Y > MathHelper.Pi * 2)
- Angle.Y -= MathHelper.Pi * 2;
- while (Angle.Y < -MathHelper.Pi * 2)
- Angle.Y += MathHelper.Pi * 2;
- if (Angle.X > maxPitch)
- Angle.X = maxPitch;
- if (Angle.X < -maxPitch)
- Angle.X = -maxPitch;
- float time = (float)gameTime.ElapsedGameTime.TotalSeconds;
- Vector3 forward = -Vector3.Normalize(new Vector3((float)Math.Sin(-Angle.Y),
- (float)Math.Sin(Angle.X),
- (float)Math.Cos(-Angle.Y)));
- if (DenyVerticalMovement)
- {
- forward = new Vector3(forward.X, 0, forward.Z);
- forward.Normalize();
- }
- Position += forward * movementSpeed * time;
- }
- else
- touchStartTime = gameTime;
- }
- }
- public override void Update(GameTime gameTime)
- {
- UpdateTouch(gameTime);
- RecalcView();
- base.Update(gameTime);
- }
- protected void RecalcView()
- {
- View = Matrix.Identity;
- View *= Matrix.CreateTranslation(-Position);
- View *= Matrix.CreateRotationZ(Angle.Z);
- View *= Matrix.CreateRotationY(Angle.Y);
- View *= Matrix.CreateRotationX(Angle.X);
- Frustrum = new BoundingFrustum(Matrix.Multiply(View, Projection));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment