-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
275 lines (214 loc) · 6.73 KB
/
sketch.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
let width = 840;
let height = 480;
let radius = 40;
let dronBoatHeight = 20;
let dronBoatVelocity = 0.01;
let v1;
let mass = 100;
let cfr = 0.01;
let gravity;
let thrust;
let velocity;
let acceleration;
let orientation;
let tmpVector;
let touchDown = false;
function setup() {
createCanvas(840, 480);
initializeGameState();
initializeUI();
}
function initializeGameState() {
v1 = createVector(40, 50);
thrust = createVector(0, 0);
gravity = createVector(0, 0.04);
acceleration = createVector(0, 0);
velocity = createVector(2, 0);
orientation = createVector(0, -1);
tmpVector = createVector(0, 0);
dronBoat = createVector(width / 2, height - dronBoatHeight);
dVel = createVector(0, 0);
dAcc = createVector(0.01, 0);
}
function initializeUI() {
slider = createSlider(1, 60, 55, 1);
slider.position(10, 10);
slider.style('width', '80px');
}
function resetAcceleration() {
acceleration.mult(0);
}
function draw() {
frameRate(slider.value());
resetAcceleration();
handleInput();
handleCollisions();
applyTouchDown();
applyEnvironmentalForces();
updateDronBoat();
updateRocketVector();
///////////// VISUALISATIONS //////////////
background(120);
drawOrientationVisualization();
drawRocket();
drawDronBoat();
drawTelemetry();
}
function updateRocketVector() {
velocity.add(acceleration);
velocity.limit(12);
v1.add(velocity);
}
function handleInput() {
const ROTATION_ANGLE = 1; // угол вращения в градусах
const THRUST_FORCE = 6.5; // сила тяги
if (keyIsPressed) {
if (keyCode === LEFT_ARROW) {
orientation = rotateNew(orientation.x, orientation.y, -ROTATION_ANGLE);
} else if (keyCode === RIGHT_ARROW) {
orientation = rotateNew(orientation.x, orientation.y, ROTATION_ANGLE);
}
if (keyCode === 32 || key === "x" || key === "X") { // пробел или X
thrust = createVector(orientation.x, orientation.y);
thrust.normalize();
thrust.mult(THRUST_FORCE);
applyForce(thrust);
}
}
}
function handleCollisions() {
// Handle collisions on the X axis
if ((v1.x + velocity.x) > (width - radius)) {
v1.x = 0;
} else if ((v1.x + velocity.x) < 0) {
v1.x = width - radius;
}
// Handle collisions with the top boundary
if ((v1.y + velocity.y) <= 0) {
v1.y = 0; // Set position at the top boundary
if (velocity.y < 0) { // If moving upwards
velocity.y *= -0.5; // Lose energy upon collision
}
}
// Handle collisions with the ground
if ((v1.y + velocity.y) >= (height - radius)) {
v1.y = height - radius; // Set position at ground level
if (velocity.y > 0) { // If falling
velocity.y *= -0.5; // Lose energy upon collision
}
// Stop completely if speed is very low and no thrust is applied
if (Math.abs(velocity.y) < 0.1 && thrust.mag() === 0) {
velocity.y = 0;
}
// Apply friction on the ground
velocity.x *= 0.96;
}
// Eliminate jitter at low horizontal speeds
if (Math.abs(velocity.x) < 0.01) {
velocity.x = 0;
}
}
function rotateNew(x, y, degree) {
tmpX = x;
// convert degrees to radians needed
x = x * cos(degree * 3.14 / 180) - y * sin(degree * 3.14 / 180);
y = tmpX * sin(degree * 3.14 / 180) + y * cos(degree * 3.14 / 180);
return createVector(x, y);
}
function applyForce(force) {
let f = p5.Vector.div(force, mass);
acceleration.add(f);
}
function drawOrientationVisualization() {
let tmp = createVector(orientation.x, orientation.y);
tmp.rotate(90 * 3.14 / 180);
tmp.normalize();
fill(244, 200, 200);
// Small circle
circle(width / 2 + tmp.x, height / 2 + tmp.y, 4);
text(`x-> ${nf(tmp.x, 0, 4)} y-> ${nf(tmp.y, 0, 4)}`, width / 2, 180);
// Scaled up vector
tmp.mult(5);
circle(width / 2 + tmp.x, height / 2 + tmp.y, 4);
text(`x-> ${nf(tmp.x, 0, 4)} y-> ${nf(tmp.y, 0, 4)}`, width / 2, 200);
// Half vector in the opposite direction
tmp.mult(-0.5);
circle(width / 2 + tmp.x, height / 2 + tmp.y, 4);
text(`x-> ${nf(tmp.x, 0, 4)} y-> ${nf(tmp.y, 0, 4)}`, width / 2, 220);
}
function applyEnvironmentalForces() {
// Apply gravity
let gravityForce = createVector(gravity.x, gravity.y);
gravityForce.mult(mass);
applyForce(gravityForce);
// Apply drag (air resistance)
let dragForce = createVector(velocity.x, velocity.y);
dragForce.normalize();
dragForce.mult(-1);
let speed = velocity.mag();
dragForce.mult(cfr * speed * speed); // Quadratic drag
applyForce(dragForce);
}
function updateDronBoat() {
const MAX_SPEED = 0.1;
const MOVEMENT_RANGE = 3 * dronBoatHeight;
// Update acceleration and velocity
dAcc.x += dronBoatVelocity;
dVel.add(dAcc);
dVel.limit(MAX_SPEED);
// Reverse direction at edges
if (dronBoat.x > width / 2 + MOVEMENT_RANGE || dronBoat.x < width / 2 - MOVEMENT_RANGE) {
dVel.mult(-1);
dronBoatVelocity *= -1;
}
// Update position
dronBoat.add(dVel);
// Reset acceleration
dAcc.mult(0);
}
function applyTouchDown() {
touchDown =
(v1.y + velocity.y) >= (height - radius - dronBoatHeight - 2) &&
(v1.y + velocity.y) <= (height - radius - dronBoatHeight) &&
(v1.x + velocity.x >= dronBoat.x - radius / 2) &&
((v1.x + velocity.x + radius) <= (dronBoat.x + dronBoatHeight * 3 * 1.5));
if (touchDown && thrust.x === 0) {
velocity.x = dronBoatVelocity;
}
}
function drawDronBoat() {
fill(200, 80, 180);
if (touchDown) {
fill(0, 222, 0);
}
rect(dronBoat.x, dronBoat.y, dronBoatHeight * 3, dronBoatHeight); // dron boat
circle(dronBoat.x + dronBoatHeight * 3, height - dronBoatHeight, 4);
}
function drawRocket() {
fill(100, 180, 180);
circle(v1.x, v1.y, 4);
circle(v1.x + radius, v1.y, 4);
rect(v1.x, v1.y, radius, radius); // rocket
tmpVector = createVector(velocity.x, velocity.y);
tmpVector.setMag(20 * tmpVector.mag());
tmpVector.sub(v1);
tmpVector.x = sqrt(tmpVector.x * tmpVector.x);
tmpVector.y = sqrt(tmpVector.y * tmpVector.y);
line(v1.x, v1.y, tmpVector.x, tmpVector.y);
line(v1.x, v1.y, v1.x + 20 * orientation.x, v1.y + 20 * orientation.y);
}
function drawTelemetry() {
fill(240);
textSize(14);
text("grv > " + nf(gravity.x, 0, 3) + " : " + nf(gravity.y, 0, 3), 10, 50);
text("acc > " + nf(acceleration.x, 0, 3) + " : " + nf(acceleration.y, 0, 3), 10, 65);
text("vel > " + nf(velocity.x, 0, 3) + " : " + nf(velocity.y, 0, 3), 10, 80);
text("pos > " + nf(v1.x, 0, 2) + " : " + nf(v1.y, 0, 2), 10, 95);
text("mag > " + nf(velocity.mag(), 0, 5), 10, 110);
text("mass > " + nf(mass, 0, 3), 10, 125);
text("cfr > " + nf(cfr, 0, 4), 10, 140);
text("ori > " + nf(orientation.x, 0, 2) + " : " + nf(orientation.y, 0, 2), 10, 155);
if (touchDown || (keyIsPressed && keyCode === 32)) {
text("thr > " + nf(thrust.x, 0, 4) + " : " + nf(thrust.y, 0, 2), 10, 170);
}
}