-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cs
More file actions
114 lines (83 loc) · 3.27 KB
/
Camera.cs
File metadata and controls
114 lines (83 loc) · 3.27 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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace HLMapFileLoader.Example
{
class Camera : GameComponent
{
private Vector3 cameraLookAt;
public Vector3 Position { get; private set; }
public Vector3 Rotation { get; private set; }
public Matrix Projection { get; protected set; }
public Matrix View { get { return Matrix.CreateLookAt(Position, cameraLookAt, Vector3.Up); } }
public Camera(Game game) : base(game)
{
this.cameraLookAt = Vector3.Zero;
this.Position = Vector3.Zero;
this.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4, Game.GraphicsDevice.Viewport.AspectRatio,
0.05f, 10000.0f);
SetPosition(new Vector3(-331.68f, 103.5f, 214.47f));
SetRotation(new Vector3(0.39f, 2.1f, 0));
}
public override void Update(GameTime gameTime)
{
KeyboardState ks = Keyboard.GetState();
Vector3 moveVector = Vector3.Zero;
if (ks.IsKeyDown(Keys.W))
moveVector.Z = 1;
if (ks.IsKeyDown(Keys.S))
moveVector.Z = -1;
if (ks.IsKeyDown(Keys.A))
moveVector.X = 1;
if (ks.IsKeyDown(Keys.D))
moveVector.X = -1;
if (ks.IsKeyDown(Keys.Space))
moveVector.Y = 1;
if (ks.IsKeyDown(Keys.LeftShift))
moveVector.Y = -1;
if (ks.IsKeyDown(Keys.Up))
SetRotation(new Vector3(Rotation.X - 0.05f, Rotation.Y, Rotation.Z));
if (ks.IsKeyDown(Keys.Down))
SetRotation(new Vector3(Rotation.X + 0.05f, Rotation.Y, Rotation.Z));
if (ks.IsKeyDown(Keys.Right))
SetRotation(new Vector3(Rotation.X, Rotation.Y - 0.05f, Rotation.Z));
if (ks.IsKeyDown(Keys.Left))
SetRotation(new Vector3(Rotation.X, Rotation.Y + 0.05f, Rotation.Z));
if (moveVector != Vector3.Zero)
{
moveVector.Normalize();
moveVector *= (float)gameTime.ElapsedGameTime.TotalSeconds * 225f;
Vector3 location = PreviewMove(moveVector);
Move(location);
}
base.Update(gameTime);
}
private void UpdateLookAt()
{
Matrix rotationMatrix = Matrix.CreateRotationX(Rotation.X) * Matrix.CreateRotationY(Rotation.Y);
Vector3 lookAtOffset = Vector3.Transform(Vector3.UnitZ, rotationMatrix);
cameraLookAt = Position + lookAtOffset;
}
public Vector3 PreviewMove(Vector3 movement)
{
Matrix rotate = Matrix.CreateRotationY(Rotation.Y);
movement = Vector3.Transform(movement, rotate);
return Position + movement;
}
public void Move(Vector3 position)
{
Position = position;
UpdateLookAt();
}
public void SetPosition(Vector3 position)
{
Vector3 cameraView = PreviewMove(position);
Move(cameraView);
}
public void SetRotation(Vector3 rotation)
{
this.Rotation = rotation;
UpdateLookAt();
}
}
}