-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcameracontrol.cpp
More file actions
108 lines (91 loc) · 2.61 KB
/
cameracontrol.cpp
File metadata and controls
108 lines (91 loc) · 2.61 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
#include "cameracontrol.hpp"
#include <Urho3D/Input/Input.h>
#include <Urho3D/Math/MathDefs.h>
namespace UrhoExtras
{
CameraControl::CameraControl(Urho3D::Context* context) :
Urho3D::Object(context),
key_forward(Urho3D::KEY_W),
key_backward(Urho3D::KEY_S),
key_left(Urho3D::KEY_A),
key_right(Urho3D::KEY_D),
key_jump(Urho3D::KEY_SPACE),
key_crouch(Urho3D::KEY_LCTRL),
yaw_sensitivity(0.1),
pitch_sensitivity(0.1),
pitch(0),
yaw(0),
buttons(0)
{
}
void CameraControl::update(bool update_rotation)
{
Urho3D::Input* input = GetSubsystem<Urho3D::Input>();
buttons = 0;
if (input->GetKeyDown(key_forward)) buttons |= BUTTON_FORWARD;
if (input->GetKeyDown(key_backward)) buttons |= BUTTON_BACKWARD;
if (input->GetKeyDown(key_left)) buttons |= BUTTON_LEFT;
if (input->GetKeyDown(key_right)) buttons |= BUTTON_RIGHT;
if (input->GetKeyDown(key_jump)) buttons |= BUTTON_JUMP;
if (input->GetKeyDown(key_crouch)) buttons |= BUTTON_CROUCH;
if (update_rotation) {
yaw += float(input->GetMouseMoveX()) * yaw_sensitivity;
pitch = Urho3D::Clamp(pitch + float(input->GetMouseMoveY()) * pitch_sensitivity, -90.0f, 90.0f);
}
}
void CameraControl::getRotation(Urho3D::Quaternion& result) const
{
result = Urho3D::Quaternion(pitch, yaw, 0);
}
Urho3D::Quaternion CameraControl::getRotation() const
{
return Urho3D::Quaternion(pitch, yaw, 0);
}
void CameraControl::getFlyingMovement(Urho3D::Vector3& result) const
{
result = Urho3D::Vector3::ZERO;
bool zero = true;
// Forward and backward
if ((buttons & BUTTON_FORWARD) && !(buttons & BUTTON_BACKWARD)) {
result.x_ = Urho3D::Sin(yaw) * Urho3D::Cos(pitch);
result.z_ = Urho3D::Cos(yaw) * Urho3D::Cos(pitch);
result.y_ = -Urho3D::Sin(pitch);
zero = false;
}
else if (!(buttons & BUTTON_FORWARD) && (buttons & BUTTON_BACKWARD)) {
result.x_ = -Urho3D::Sin(yaw) * Urho3D::Cos(pitch);
result.z_ = -Urho3D::Cos(yaw) * Urho3D::Cos(pitch);
result.y_ = Urho3D::Sin(pitch);
zero = false;
}
// Left and right
if ((buttons & BUTTON_LEFT) && !(buttons & BUTTON_RIGHT)) {
result.x_ -= Urho3D::Cos(yaw);
result.z_ += Urho3D::Sin(yaw);
zero = false;
}
else if (!(buttons & BUTTON_LEFT) && (buttons & BUTTON_RIGHT)) {
result.x_ += Urho3D::Cos(yaw);
result.z_ -= Urho3D::Sin(yaw);
zero = false;
}
// Jump and crouch
if ((buttons & BUTTON_JUMP) && !(buttons & BUTTON_CROUCH)) {
result.y_ += 1;
zero = false;
}
else if (!(buttons & BUTTON_JUMP) && (buttons & BUTTON_CROUCH)) {
result.y_ -= 1;
zero = false;
}
if (!zero) {
result.Normalize();
}
}
Urho3D::Vector3 CameraControl::getFlyingMovement() const
{
Urho3D::Vector3 result;
getFlyingMovement(result);
return result;
}
}