-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·325 lines (289 loc) · 9.39 KB
/
main.js
File metadata and controls
executable file
·325 lines (289 loc) · 9.39 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
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
console.log("Web Physic Engine by Linux_Hat dev-0.1");
var canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
var Timer;
var FpsTimer;
var objects;
var collisions;
var meterSize = 100;
var speed = 1;
var gravityX = 0 * meterSize;
var gravityY = 0 * meterSize;
var cameraPos = [0, 0];
var cameraFollow = false;
function toFixed(num, fixed) {
var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?');
return num.toString().match(re)[0];
}
async function Init() {
function get_param(param) {
var vars = {};
window.location.href
.replace(location.hash, "")
.replace(/[?&]+([^=&]+)=?([^&]*)?/gi, function (m, key, value) {
vars[key] = value !== undefined ? value : "";
});
if (param) {
return vars[param] ? vars[param] : null;
}
return vars;
}
var config_file = get_param("config");
if (!config_file) {
config_file = "default.json";
}
try {
const reponse = await fetch(config_file);
if (!reponse.ok) {
throw new Error(`Error loading JSON file: ${reponse.status}`);
}
const data = await reponse.json();
objects = data.objects;
meterSize = data.meterSize;
speed = data.speed;
gravityX = data.gravityX * meterSize;
gravityY = data.gravityY * meterSize;
cameraFollow = data.cameraFollow;
} catch (erreur) {
console.error("Error while loading JSON file: ", erreur.message);
}
Timer = new Date().getTime();
FpsTimer = new Date().getTime();
collisions = 0;
}
function drawObjects(objects) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var key in objects) {
var object = objects[key];
if (object.type === "box") {
ctx.beginPath();
ctx.rect(
object.x - object.width / 2 - cameraPos[0],
object.y - object.height / 2 - cameraPos[1],
object.width,
object.height
);
ctx.fillStyle = object.color;
ctx.fill();
ctx.closePath();
}
if (object.type === "ball") {
ctx.beginPath();
ctx.arc(
object.x - cameraPos[0],
object.y - cameraPos[1],
object.radius,
0,
Math.PI * 2
);
ctx.fillStyle = object.color;
ctx.fill();
ctx.closePath();
}
}
}
function updateObjectsPosition(objects) {
for (var key in objects) {
var object = objects[key];
var t = ((new Date().getTime() - Timer) / 1000) * speed;
if (!object.freeze) {
object.x += object.vx * t;
object.y += object.vy * t;
}
}
}
function updateObjectsVelocity(objects) {
for (var key in objects) {
var object = objects[key];
if (!object.freeze) {
var t = ((new Date().getTime() - Timer) / 1000) * speed;
object.vx += gravityX * t;
object.vy += gravityY * t;
} else {
object.vx = 0;
object.vy = 0;
}
}
}
function collideObjects(objects) {
var pairs = new getPairs(objects);
for (let i = 0; i < pairs.length; i++) {
var pair = pairs[i];
var object = objects[pair[0]];
var object_ = objects[pair[1]];
if (
!object.collision ||
!object_.collision ||
(object.freeze && object_.freeze)
) {
continue;
} else if (object.type == object_.type && object.type == "ball") {
collideBalls(object, object_);
} else if (object.type == "ball" && object_.type == "box") {
collideBallBox(object, object_);
} else if (object.type == "box" && object_.type == "ball") {
collideBallBox(object_, object);
}
}
}
function collideBalls(ball, ball_) {
const deltaX = ball_.x - ball.x;
const deltaY = ball_.y - ball.y;
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
const overlap = ball.radius + ball_.radius - distance;
if (overlap > 0) {
collisions += 1;
a = Math.atan2(ball_.y - ball.y, ball_.x - ball.x);
if (ball.freeze) {
ball_.x += overlap * Math.cos(a);
ball_.y += overlap * Math.sin(a);
} else if (ball_.freeze) {
ball.x -= overlap * Math.cos(a);
ball.y -= overlap * Math.sin(a);
} else {
const correctionRatio = overlap / distance;
ball.x -= deltaX * correctionRatio;
ball.y -= deltaY * correctionRatio;
ball_.x += deltaX * correctionRatio;
ball_.y += deltaY * correctionRatio;
}
const relativeVelocityX = ball_.vx - ball.vx;
const relativeVelocityY = ball_.vy - ball.vy;
const restitution_coef = Math.max(
ball.restitution_coef,
ball_.restitution_coef
);
const dotProduct = deltaX * relativeVelocityX + deltaY * relativeVelocityY;
if (ball.freeze) {
const impulse = (2 * dotProduct) / (distance * (ball_.mass + ball_.mass));
const bounceX =
(impulse * ball_.mass * deltaX) / distance +
restitution_coef * ((impulse * ball_.mass * deltaX) / distance);
const bounceY =
(impulse * ball_.mass * deltaY) / distance +
restitution_coef * ((impulse * ball_.mass * deltaY) / distance);
ball_.vx -= bounceX;
ball_.vy -= bounceY;
} else if (ball_.freeze) {
const impulse = (2 * dotProduct) / (distance * (ball.mass + ball.mass));
const bounceX =
(impulse * ball.mass * deltaX) / distance +
restitution_coef * ((impulse * ball.mass * deltaX) / distance);
const bounceY =
(impulse * ball.mass * deltaY) / distance +
restitution_coef * ((impulse * ball.mass * deltaY) / distance);
ball.vx += bounceX;
ball.vy += bounceY;
} else {
const impulse = (2 * dotProduct) / (distance * (ball.mass + ball_.mass));
ball.vx += (impulse * ball_.mass * deltaX * restitution_coef) / distance;
ball.vy += (impulse * ball_.mass * deltaY * restitution_coef) / distance;
ball_.vx -= (impulse * ball.mass * deltaX * restitution_coef) / distance;
ball_.vy -= (impulse * ball.mass * deltaY * restitution_coef) / distance;
}
}
}
function collideBallBox(ball, box) {
// Calculate the distances between the ball center and the box edges
const distX = Math.abs(ball.x - box.x) - box.width / 2 - ball.radius;
const distY = Math.abs(ball.y - box.y) - box.height / 2 - ball.radius;
let overlap = 0;
// Check if the ball is inside the box
if (distX < 0 && distY < 0) {
const dx = Math.min(distX, 0);
const dy = Math.min(distY, 0);
overlap = Math.sqrt(dx * dx + dy * dy);
// Move the ball out of the box
ball.x -= dx * (overlap / Math.abs(distX));
ball.y -= dy * (overlap / Math.abs(distY));
}
if (overlap > 0) {
collisions += 1;
// Calculate the relative velocity
const relativeVelocityX = ball.vx - box.vx;
const relativeVelocityY = ball.vy - box.vy;
// Calculate the restitution coefficient
const restitution_coef = Math.max(
ball.restitution_coef,
box.restitution_coef
);
// Calculate the normal vector and the tangential vector
const normalX = (ball.x - box.x) / box.width;
const normalY = (ball.y - box.y) / box.height;
const tangentialX = -normalY;
const tangentialY = normalX;
// Calculate the impulse
const dotProduct =
relativeVelocityX * normalX + relativeVelocityY * normalY;
const invMassSum = 1 / ball.mass + 1 / box.mass;
const j = (-(1 + restitution_coef) * dotProduct) / invMassSum;
const impulseX = j * normalX;
const impulseY = j * normalY;
// Apply the impulse
ball.vx += impulseX;
ball.vy += impulseY;
box.vx -= normalX * (impulseX / box.mass);
box.vy -= normalY * (impulseY / box.mass);
}
}
function gravityBetweenObjects(objects) {
var pairs = new getPairs(objects);
var text = "Forces :\n";
for (let i = 0; i < pairs.length; i++) {
var pair = pairs[i];
var object = objects[pair[0]];
var object_ = objects[pair[1]];
const deltaX = object_.x - object.x;
const deltaY = object_.y - object.y;
const d = Math.sqrt(deltaX * deltaX + deltaY * deltaY) / meterSize;
var G = 6.674e-11;
var F = ((G * (object.mass * object_.mass)) / (d * d));
var a = Math.atan2(deltaY, deltaX);
var t = ((new Date().getTime() - Timer) / 1000) * speed;
object.vx += F * t * Math.cos(a);
object.vy += F * t * Math.sin(a);
object_.vx -= F * t * Math.cos(a);
object_.vy -= F * t * Math.sin(a);
text += `Force between ${pair[0]} and ${pair[1]} is ${toFixed(F,3)} N.\n`;
}
document.getElementById("forces").innerText = text;
}
function getPairs(objects) {
var objects_list = [];
for (var key in objects) {
objects_list.push(key);
}
let pairs = [];
for (let i = 0; i < objects_list.length; i++) {
for (let j = i + 1; j < objects_list.length; j++) {
pairs.push([objects_list[i], objects_list[j]]);
}
}
return pairs;
}
Init();
function mainLoop() {
var tps = 1000 / (new Date().getTime() - Timer);
document.getElementById("tps").innerText = tps.toFixed(0);
updateObjectsPosition(objects);
gravityBetweenObjects(objects);
updateObjectsVelocity(objects);
collideObjects(objects);
Timer = new Date().getTime();
}
function renderLoop() {
var fps = 1000 / (new Date().getTime() - FpsTimer);
document.getElementById("fps").innerText = fps.toFixed(2);
document.getElementById("collisions").innerText = collisions;
if (objects && cameraFollow) {
cameraPos = [
objects[Object.keys(objects)[0]].x - canvas.width / 2,
objects[Object.keys(objects)[0]].y - canvas.height / 2,
];
}
drawObjects(objects);
requestAnimationFrame(renderLoop);
FpsTimer = new Date().getTime();
}
setInterval(mainLoop);
renderLoop();