-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCamera.cs
More file actions
120 lines (92 loc) · 3.99 KB
/
Camera.cs
File metadata and controls
120 lines (92 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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.Media;
/*
* W.A.Prasad Jayashanka
* prasadjayashanka@gmail.com
* http://www.prasad-notes.blogspot.com
*
* Sri Lanka
*
* Please rename below namespace name according to your class name..
*
* Happy coding :D
*
*/
namespace YOUR_CLASS_HERE
{
/// <summary>
/// This is a game component that implements IUpdateable.
/// </summary>
public class Camera : Microsoft.Xna.Framework.GameComponent
{
public Matrix view {get; protected set;}
public Matrix projection { get; protected set; }
public Vector3 cameraPosition { get; protected set; }
Vector3 cameraDirection;
Vector3 cameraUp;
//defines speed of camera movement
float speed = 0.5F;
MouseState prevMouseState;
public Camera(Game game, Vector3 pos, Vector3 target, Vector3 up)
: base(game)
{
// TODO: Construct any child components here
// Build camera view matrix
cameraPosition = pos;
cameraDirection = target - pos;
cameraDirection.Normalize();
cameraUp = up;
CreateLookAt();
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)Game.Window.ClientBounds.Width / (float)Game.Window.ClientBounds.Height, 1, 100);
}
/// <summary>
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
/// </summary>
public override void Initialize()
{
// TODO: Add your initialization code here
// Set mouse position and do initial get state
Mouse.SetPosition(Game.Window.ClientBounds.Width / 2, Game.Window.ClientBounds.Height / 2);
prevMouseState = Mouse.GetState();
base.Initialize();
}
/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
// Move forward and backward
if (Keyboard.GetState( ).IsKeyDown(Keys.W))
cameraPosition += cameraDirection * speed;
if (Keyboard.GetState( ).IsKeyDown(Keys.S))
cameraPosition -= cameraDirection * speed;
if (Keyboard.GetState( ).IsKeyDown(Keys.A))
cameraPosition += Vector3.Cross(cameraUp, cameraDirection) * speed;
if (Keyboard.GetState( ).IsKeyDown(Keys.D))
cameraPosition -= Vector3.Cross(cameraUp, cameraDirection) * speed;
// Rotation in the world
cameraDirection = Vector3.Transform(cameraDirection, Matrix.CreateFromAxisAngle(cameraUp, (-MathHelper.PiOver4 / 150) * (Mouse.GetState( ).X - prevMouseState.X)));
cameraDirection = Vector3.Transform(cameraDirection, Matrix.CreateFromAxisAngle(Vector3.Cross(cameraUp, cameraDirection), (MathHelper.PiOver4 / 100) * (Mouse.GetState( ).Y - prevMouseState.Y)));
cameraUp = Vector3.Transform(cameraUp, Matrix.CreateFromAxisAngle(Vector3.Cross(cameraUp, cameraDirection), (MathHelper.PiOver4 / 100) * (Mouse.GetState( ).Y - prevMouseState.Y)));
// Reset prevMouseState
prevMouseState = Mouse.GetState( );
CreateLookAt();
base.Update(gameTime);
}
private void CreateLookAt()
{
view = Matrix.CreateLookAt(cameraPosition, cameraPosition + cameraDirection, cameraUp);
}
}
}