-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairplanemovement.cpp
116 lines (94 loc) · 2.73 KB
/
airplanemovement.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
/*
* File: airplane_movement_t.cpp
* Author: gg
*
* Created on 4 de dezembro de 2019, 23:31
*/
#include "airplanemovement.h"
#include <cmath>
#include <cstdio>
airplane_movement_t::airplane_movement_t(const point3f& p) : position(p) {
}
void airplane_movement_t::clipZ(float height, float radius) {
if (position.z < radius) {
position.z = radius;
} else if (position.z > height - radius) {
position.z = height - radius;
}
}
void airplane_movement_t::keyPress(unsigned char key) {
switch (key) {
case '+':
printf("Increasing velocity\n");
magnitude *= 1 + velocityAccelerationFactor;
break;
case '-':
printf("Decreasing velocity\n");
magnitude *= 1 - velocityAccelerationFactor;
break;
default:
updateKey(key, 1);
break;
}
}
void airplane_movement_t::keyReleased(unsigned char key) {
updateKey(key, 0);
}
void airplane_movement_t::updateKey(unsigned char key, float intensity) {
switch (key) {
case 'a':
left = -intensity;
break;
case 'd':
right = -intensity;
break;
case 's':
down = intensity;
break;
case 'w':
up = intensity;
break;
default:
return;
}
setInputAxis(right - left, up - down);
}
void airplane_movement_t::move(float dt) {
horizontal += horizontalVelocity * dt;
vertical += verticalVelocity * dt;
updateVelocity();
position += velocity * dt;
}
void airplane_movement_t::updateVelocity() {
float r, z;
// compute the sine and cosine
sincosf(vertical, &z, &r);
float x, y;
sincosf(horizontal, &y, &x);
x *= r;
y *= r;
// this velocity is normalized, its length is 1.
velocity.x = x;
velocity.y = y;
velocity.z = z;
velocity *= magnitude;
}
void airplane_movement_t::setInputAxis(float dx, float dy) {
horizontalVelocity = dx * magnitude * horizontalFactor;
verticalVelocity = dy * magnitude * verticalFactor;
}
void airplane_movement_t::setInitialMagnitude(float magnitude) {
velocity *= magnitude / this->magnitude;
this->magnitude = magnitude;
initialMagnitude = magnitude;
}
void airplane_movement_t::setInitialConditions(const point3f& p, const vector3f& v) {
position = p;
velocity = v;
magnitude = velocity.length();
// The angle of the vector <x, y> is the horizontal angle.
horizontal = atan2f(velocity.y, velocity.x);
// This projects the vector to the xy plane and uses the z component
// as opposite cathetus while the length of xy is the adjacent cathetus.
vertical = asinf(velocity.z / magnitude);
}