-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
386 lines (330 loc) · 10.2 KB
/
script.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
var angleSlider = document.getElementById("angleSlider");
var velocitySlider = document.getElementById("velocitySlider");
var gravitySlider = document.getElementById("gravitySlider");
var restitutionCoefSlider = document.getElementById("restitutionCoefSlider");
var initialHeightSlider = document.getElementById("initialHeightSlider");
let gravity = 9.8; // Standard gravity = 9.80665 m/s^2 (rounded down to avoid 2 decimals)
let elasticity_index = 0.6;
let initial_x = 0, initial_y = 10;
angle = getAngleSlider();
velocity = getVelocitySlider();
angleSlider.oninput = function () {
angle = this.value;
document.getElementById("angleOut").innerHTML = this.value;
previewLine();
};
velocitySlider.oninput = function () {
velocity = this.value / 10;
document.getElementById("velocityOut").innerHTML = this.value / 10;
previewLine();
};
gravitySlider.oninput = function () {
gravity = this.value / 10;
document.getElementById("gravityOut").innerHTML = this.value / 10;
console.log(gravity);
previewLine();
};
restitutionCoefSlider.oninput = function () {
elasticity_index = this.value / 100;
document.getElementById("restitutionCoefOut").innerHTML = this.value / 100;
console.log(gravity);
previewLine();
};
initialHeightSlider.oninput = function () {
initial_y = this.value / 10;
document.getElementById("initialHeightOut").innerHTML = this.value / 10;
console.log(initial_y);
previewLine();
};
let canvas = document.getElementById("drawcanvas");
let ctx = canvas.getContext("2d");
let ball_color = "#7A0222", ball_radius = 14;
let line_color = "#1DEFD2";
line_color = "#46a9bf";
let FPS = 60;
/* if (window.matchMedia) {
// Check if the dark-mode Media-Query matches
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
line_color = "#C2CACA";
}
} else {
// Default (when Media-Queries are not supported)
} */
let cx = ctx.canvas.width,
cy = ctx.canvas.height;
let bx = ball_radius,
by = cy - ball_radius;
let default_velocity = 10,
default_angle = 45; // 10 m/s and 45 deg
let increment = 15;
let lang_index = 1;
let Vx, Vy;
let language_strings = [
[
"Angle",
"Velocity",
"Gravity",
"Restitution coefficient",
"Initial height",
"Time taken",
"Final velocity",
"Final position",
"Maximum height",
],
[
"Angle",
"Velocitat",
"Gravetat",
"Coeficient de restitució",
"Alçada inicial",
"Temps trigat",
"Velocitat final",
"Posició final",
"Altura màxima",
],
[
"Angle",
"Vitesse",
"Gravité",
"Coefficient de restitution",
"Hauteur initiale",
"Temps pris",
"Vitesse finale",
"Position finale",
"Hauteur maximale",
],
[
"Ángulo",
"Velocidad",
"Gravedad",
"Coeficiente de restitución",
"Altura inicial",
"Tiempo tardado",
"Velocidad final",
"Posición final",
"Altura máxima",
],
];
let sliderNames = [
"angle",
"velocity",
"gravity",
"restitutionCoef",
"initialHeight",
"timetaken",
"finalvelocity",
"finalposition",
"maximumheight",
];
let langNames = ["en", "ca", "fr", "es"];
function setCookie(cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
let expires = "Expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";Path=/;SameSite=Lax";
}
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i <ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return null;
}
function drawBall(x, y, radius, color) {
ctx.beginPath();
ctx.strokeStyle = color;
ctx.fillStyle = color;
ctx.arc(x, y, radius, 0, 2 * Math.PI); // Full circle
ctx.fill();
ctx.stroke();
ctx.closePath();
}
function setLanguage(index) {
let sliderDisplay = new Array();
for (let i = 0; i < sliderNames.length; i++) {
console.log(sliderNames[i] + "Disp");
console.log(sliderNames.length);
document.getElementById(sliderNames[i] + "Disp").innerHTML =
language_strings[lang_index][i];
}
setCookie("default_language", langNames[index], 365);
for(let i = 0; i < langNames.length; i++)
{
document.getElementById(langNames[i] + "lang").style.color = (i == index) ? "#08e4d3" : "#11b78b";
}
}
function langIndex(index) {
lang_index = index;
setLanguage(index);
}
function initCanvas() {
let cookieLanguage = getCookie("default_language");
if(getCookie == null)
{
lang_index = 0;
setCookie("default_language", langNames[lang_index], 365);
} else {
for(let i = 0; i < langNames.length; i++)
{
if(langNames[i] == cookieLanguage)
{
lang_index = i;
break;
}
}
}
setLanguage(lang_index);
angleSlider.value = default_angle;
velocitySlider.value = default_velocity * 10;
gravitySlider.value = gravity * 10;
restitutionCoefSlider.value = elasticity_index * 100;
initialHeightSlider.value = initial_y*10;
startGame();
previewLine();
}
const resizeObserver = new ResizeObserver(() => {
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
});
resizeObserver.observe(canvas);
function round(num, dec) {
num = Math.round(num + "e" + dec);
return Number(num + "e-" + dec);
}
function displayInformation(xf, vf, max_y, time_taken) {
document.getElementById("timetaken").innerHTML = round(time_taken, 2);
document.getElementById("finalvelocity").innerHTML = round(vf, 2);
document.getElementById("finalposition").innerHTML = round(xf, 2);
document.getElementById("maximumheight").innerHTML = round(max_y, 2);
}
function previewLine() {
ctx.clearRect(0, 0, cx, cy);
ctx.strokeStyle = "#000000";
ctx.save();
ctx.moveTo(bx, by);
//ctx.lineTo(bx+10*Vx, by-10*Vy);
ctx.stroke();
let resol = 15 + Math.round(velocity) + Math.round(4 / gravity);
launchBall(
angle,
velocity,
ball_radius / increment + initial_x,
initial_y,
resol,
increment,
false,
0
);
ctx.restore();
drawBall(ball_radius, cy - ball_radius-initial_y*increment, ball_radius, ball_color);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let sizeMultiplier = 0;
function launchBall(angle_launch, vel, x0, y0, steps, inc, showBall, bounceCount) {
steps = Math.round(steps); // If steps are decimal, it will cause displaying issues
let x = x0, y = y0;
angleRad = (angle_launch / 180) * Math.PI;
(Vx = vel * Math.cos(angleRad)), (Vy = vel * Math.sin(angleRad));
ctx.beginPath();
//console.log("Vx: " + Vx + " Vy: " + Vy + " Delta=" + (Math.pow(Vy, 2) - 2*gravity*y0));
if (((vel > 0 && Vy > 0) || (angle_launch == 0 && y0 > 0)) && x0 * inc < cx) {
ctx.strokeStyle = line_color;
let endtime =
(Vy + Math.sqrt(Math.pow(Vy, 2) + 2 * gravity * y0)) / gravity; // Calculate the end time
/*
We get the equation y = y0 + v0y*t + 1/2*g*t^2
After setting y to 0 we get a nice second degree equation, 1/2*g*t^2 + v0y*t + y0 = 0
Solving for x by using the quadratic formula and simplyfing gives that expression
*/
let delta_time = endtime / steps; // After isolating from the equation
let count = 0;
//console.log('Launching ball with angle ' + angle_launch + 'º, velocity ' + vel + ' m/s, gravity ' + gravity + ' m/s^2 and y0 ' + y0 + 'm');
let xf = x0 + Vx * endtime;
let vf = Math.sqrt(Math.pow(Vy - gravity * endtime, 2) + Math.pow(Vx, 2));
let time = 0;
let max_y = y0 + Math.pow(Vy, 2) / (2 * gravity);
ctx.moveTo(inc * x0, cy - inc * y0);
for (let k = 0; k < endtime; k += delta_time) {
/*if((inc*x > cx) && inc*y > cy && count > 1)
{
break; // Don't process things that aren't rendered
}*/
ctx.moveTo(inc * x, cy - inc * y);
time += delta_time;
x = x0 + Vx * time;
y = y0 + Vy * time - (Math.pow(time, 2) * gravity) / 2;
//if(inc*(y+1) <= cy || count < 2)
{
ctx.lineTo(inc * x, cy - inc * y);
ctx.stroke();
}
count++;
if(showBall)
{
sizeMultiplier = (xf-x0)/(Vx*duration/1000+Math.pow(duration/1000, 2));
}
if (Math.abs(endtime - k) < 1e-8 || Math.abs(y) < 1e-8) {
break;
}
}
if (bounceCount < 3 || (vf * elasticity_index) / gravity > 0.2) {
if (showBall) {
drawBall(xf * inc, cy - ball_radius-initial_y*increment, ball_radius, ball_color);
} else {
if (xf * inc < cx && vf > 1) {
if(bounceCount == 0)
{
console.log("Sense rectificació: " + angle_launch);
angle_launch = 180*Math.abs(Math.atan2(Vy-gravity*endtime, Vx))/Math.PI;
console.log("Amb rectificació: " + angle_launch);
}
launchBall(
angle_launch,
vf * elasticity_index,
x,
0,
steps * elasticity_index,
inc,
showBall,
++bounceCount
); // y0 = 0 because it bounces with the ground (y = 0)
}
}
}
displayInformation(xf, vf, max_y, endtime);
}
ctx.closePath();
}
function animateBall(time, speed, x0, xf, y0, yf, x_acc, y_acc)
{
}
function startGame() {
previewLine();
drawBall(ball_radius, cy - ball_radius-initial_y*increment, ball_radius, ball_color);
document.addEventListener("keypress", function (event) {
if (
event.key == " " ||
event.key == "Enter" ||
event.keyCode == 32 ||
event.keyCode == 13
) {
launchBall(angle, velocity, ball_radius / increment, 0, 200, 15, true, 0);
}
});
}
function getAngleSlider() {
return document.getElementById("angleOut").innerHTML;
}
function getVelocitySlider() {
return document.getElementById("velocityOut").innerHTML;
}