-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.cpp
146 lines (115 loc) · 2.68 KB
/
player.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "player.h"
// TODO
#define MOVEMENT_DIVIDER 2.000f
Player::Player(const std::string& name, const Vector3f& size, float weightKg, float eyeHeight) : m_name(name), m_size(size), m_weight(weightKg), m_eyeHeight(eyeHeight), m_position(0, 0, 0), m_rotation(0, 0, 0)
{
}
Player::~Player()
{
}
void Player::SetName(const std::string& name)
{
m_name = name;
}
const std::string& Player::GetName() const
{
return m_name;
}
void Player::SetSize(const Vector3f& size)
{
m_size = size;
}
const Vector3f& Player::GetSize() const
{
return m_size;
}
void Player::SetWeight(float weight)
{
m_weight = weight;
}
float Player::GetWeight() const
{
return m_weight;
}
void Player::SetEyeHeight(float eyeHeight)
{
m_eyeHeight = eyeHeight;
}
float Player::GetEyeHeight() const
{
return m_eyeHeight;
}
Vector3f Player::SimulateMove(bool front, bool back, bool left, bool right, bool run, bool fly, float elapsedTime)
{
Vector3f delta(0, 0, 0);
float divider = MOVEMENT_DIVIDER;
if(run)
divider *= 4;
if(fly)
divider *= 20;
float yrotrad = DEGTORAD(m_rotation.y);
if(front)
{
delta.x += float(sin(yrotrad)) * divider * elapsedTime;
delta.z -= float(cos(yrotrad)) * divider * elapsedTime;
}
if(left)
{
delta.x -= float(cos(yrotrad)) * divider * elapsedTime;
delta.z -= float(sin(yrotrad)) * divider * elapsedTime;
}
if(back)
{
delta.x -= float(sin(yrotrad)) * divider * elapsedTime;
delta.z += float(cos(yrotrad)) * divider * elapsedTime;
}
if(right)
{
delta.x += float(cos(yrotrad)) * divider * elapsedTime;
delta.z += float(sin(yrotrad)) * divider * elapsedTime;
}
return delta;
}
void Player::SetPosition(const Vector3f& position)
{
m_position = position;
}
Vector3f Player::GetPosition() const
{
return m_position;
}
Vector3f Player::GetEyePosition() const
{
Vector3f eyePos = GetPosition();
eyePos.y += GetEyeHeight() - (m_size.y / 2.f);
return eyePos;
}
void Player::Rotate(const Vector3f& deltaRotation)
{
m_rotation += deltaRotation;
ClampRotation();
}
void Player::SetRotation(const Vector3f& rotation)
{
m_rotation = rotation;
ClampRotation();
}
const Vector3f& Player::GetRotation() const
{
return m_rotation;
}
Vector3f Player::GetEyeVector() const
{
float rx = DEGTORAD(m_rotation.x);
float ry = DEGTORAD(m_rotation.y);
Vector3f result(sin(ry) * cos(rx), -sin(rx), -cos(ry) * cos(rx));
result.Normalize();
return result;
}
void Player::ClampRotation()
{
if(m_rotation.x >= 85.f)
m_rotation.x = 85.f;
if(m_rotation.x <= -85.f)
m_rotation.x = -85.f;
}