-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
34 lines (30 loc) · 1023 Bytes
/
utils.js
File metadata and controls
34 lines (30 loc) · 1023 Bytes
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
const Utils = {
// Calculate distance between two points
distance: (x1, y1, x2, y2) => {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
},
// Calculate angle between two points
angle: (x1, y1, x2, y2) => {
return Math.atan2(y2 - y1, x2 - x1);
},
// Random number between min and max
random: (min, max) => {
return Math.random() * (max - min) + min;
},
// Check collision between two rectangles
checkCollision: (rect1, rect2) => {
return rect1.x < rect2.x + rect2.width &&
rect1.x + rect1.width > rect2.x &&
rect1.y < rect2.y + rect2.height &&
rect1.y + rect1.height > rect2.y;
},
// Load image helper
loadImage: (src) => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = reject;
img.src = src;
});
}
};