This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cs
More file actions
118 lines (98 loc) · 3.2 KB
/
Copy pathCamera.cs
File metadata and controls
118 lines (98 loc) · 3.2 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
using Microsoft.Xna.Framework.Input;
using System;
namespace Minesharp;
public class Camera
{
public bool Freezed { get; set; }
public Vector3 Position;
public float Speed;
public float FOV;
public float Sensitivity;
private Vector3 _up = Vector3.UnitY;
private Vector3 _front = Vector3.UnitZ;
private Vector3 _right = -Vector3.UnitX;
private float _pitch;
private float _yaw = -90f;
private bool _firstFrame = true;
private GraphicsDevice graphicsDevice;
public Camera(Vector3 position, float speed, float fov, float sensitivity, GraphicsDevice graphicsDevice)
{
Position = position;
Speed = speed;
FOV = fov;
Sensitivity = sensitivity;
this.graphicsDevice = graphicsDevice;
}
public void Update()
{
if (!Freezed)
{
InputHandler();
UpdateAxes();
}
}
public Matrix GetViewMatrix()
{
return Matrix.CreateLookAt(Position, Position + _front, _up);
}
public Matrix GetProjectionMatrix()
{
return Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(FOV), graphicsDevice.Viewport.AspectRatio, 0.1f, 1000f);
}
private void InputHandler()
{
KeyboardState kState = Keyboard.GetState();
MouseState mouse = Mouse.GetState();
if (kState.IsKeyDown(Keys.W))
{
Position += _front * Speed * Global.DeltaTime;
}
if (kState.IsKeyDown(Keys.A))
{
Position -= _right * Speed * Global.DeltaTime;
}
if (kState.IsKeyDown(Keys.S))
{
Position -= _front * Speed * Global.DeltaTime;
}
if (kState.IsKeyDown(Keys.D))
{
Position += _right * Speed * Global.DeltaTime;
}
if (kState.IsKeyDown(Keys.Space))
{
Position.Y += Global.DeltaTime * Speed;
}
if (kState.IsKeyDown(Keys.LeftAlt))
{
Position.Y -= Global.DeltaTime * Speed;
}
int screenCenterX = graphicsDevice.Viewport.Width / 2;
int screenCenterY = graphicsDevice.Viewport.Height / 2;
if (_firstFrame)
{
Mouse.SetPosition(screenCenterX, screenCenterY);
_firstFrame = false;
}
else
{
float deltaX = mouse.X - screenCenterX;
float deltaY = mouse.Y - screenCenterY;
_yaw += deltaX * Sensitivity * Global.DeltaTime;
_pitch -= deltaY * Sensitivity * Global.DeltaTime;
// Clamp pitch value
_pitch = Math.Clamp(_pitch, -89f, 89f);
// Reset mouse position to the center of the screen
Mouse.SetPosition(screenCenterX, screenCenterY);
}
}
private void UpdateAxes()
{
_front.X = MathF.Cos(MathHelper.ToRadians(_pitch)) * MathF.Cos(MathHelper.ToRadians(_yaw));
_front.Y = MathF.Sin(MathHelper.ToRadians(_pitch));
_front.Z = MathF.Cos(MathHelper.ToRadians(_pitch)) * MathF.Sin(MathHelper.ToRadians(_yaw));
_front = Vector3.Normalize(_front);
_right = Vector3.Normalize(Vector3.Cross(_front, Vector3.UnitY));
_up = Vector3.Normalize(Vector3.Cross(_right, _front));
}
}