-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSolid.js
47 lines (36 loc) · 1.33 KB
/
Solid.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
class Solid {
constructor(topL,botR){
var lineWidth = 1;
this.pos = createVector(topL.pixelPos.x-lineWidth, topL.pixelPos.y-lineWidth);
this.w = botR.pixelPos.x - this.pos.x + lineWidth;
this.h = botR.pixelPos.y - this.pos.y + lineWidth;
this.bottomRight = createVector(this.pos.x + this.w, this.pos.y + this.h);
}
restrictMovement(tl, br, movement) {//player dimensions
//add the x first
var x = movement.x;
var y = movement.y;
//
// movement.x = round(movement.x);
// movement.y = round(movement.y);
var ptl = createVector(tl.x+movement.x, tl.y);
var pbr = createVector(br.x+movement.x, br.y);
if ((ptl.x <this.bottomRight.x && pbr.x > this.pos.x) &&( ptl.y < this.bottomRight.y && pbr.y > this.pos.y)) {
x=0;
}
//check the y movement
ptl = createVector(tl.x, tl.y +movement.y);
pbr = createVector(br.x, br.y + movement.y);
if ((ptl.x <this.bottomRight.x && pbr.x > this.pos.x) &&( ptl.y < this.bottomRight.y && pbr.y > this.pos.y)) {
y=0;
}
return createVector(x, y);
}
collision(ptl, pbr) {//player dimensions
//add the x first
if ((ptl.x <this.bottomRight.x && pbr.x > this.pos.x) &&( ptl.y < this.bottomRight.y && pbr.y > this.pos.y)) {
return true;
}
return false;
}
}