-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollider.js
More file actions
74 lines (57 loc) · 1.91 KB
/
collider.js
File metadata and controls
74 lines (57 loc) · 1.91 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
class RectangleCollider {
constructor(pos, size) {
this.pos = pos;
this.size = size;
// Restore everything to default settings
this.clearLastCollisionFace();
}
hitTopFace() {
return this.hit && !this.horizontal && !this.left;
}
hitUnderFace() {
return this.hit && !this.horizontal && this.left;
}
hitLeftFace() {
return this.hit && this.horizontal && this.left;
}
hitRightFace() {
return this.hit && this.horizontal && !this.left;
}
clearLastCollisionFace() {
this.hit = false;
this.horizontal = false;
this.left = false;
}
collideCalc(collider, collide) {
this.clearLastCollisionFace();
// Return false if not colliding
for (let i = 0; i < 2; i++)
if (this.pos[i] >= collider.pos[i] + collider.size[i] ||
collider.pos[i] >= this.pos[i] + this.size[i])
return false;
this.hit = true;
// Generate centers
let cents = [
[undefined, undefined],
[undefined, undefined]
];
for (let i = 0; i < 2; i++) {
cents[0][i] = this.pos[i] + (this.size[i] / 2);
cents[1][i] = collider.pos[i] + (collider.size[i] / 2);
}
let centDif = [undefined, undefined];
for (let i = 0; i < 2; i++)
centDif[i] = Math.abs(cents[0][i] - cents[1][i]);
this.horizontal = centDif[0] > centDif[1];
const inHorizontal = Number(!this.horizontal);
this.left = cents[0][inHorizontal] < cents[1][inHorizontal];
if (collide)
collider.pos[inHorizontal] =
this.pos[inHorizontal] + (this.left ? this.size[1] : -collider.size[1]);
return true;
}
p5DebugDraw() {
rect(this.pos[0], this.pos[1],
this.size[0], this.size[1]);
}
}