-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.js
43 lines (37 loc) · 950 Bytes
/
block.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
class Block {
constructor(x, w, m, v, xc){
this.x = x;
this.y = height - w;
this.w = w;
this.v = v;
this.m = m;
this.xConstraint = xc
}
hitWall(){
return (this.x <=0)
}
reverse(){
this.v *= -1;
}
collide(other) {
return !(this.x + this.w < other.x || this.x > other.x + other.w)
// print( 'not collide');
// } else {
// print ('collide');
// }
}
bounce(other) {
let sumM = this.m + other.m;
// let newV = (((this.m-other.m)/sumM)*this.v)+(((2*other.m)/sumM)*other.v);
let newV = (this.m - other.m) / sumM * this.v;
newV += (2 * other.m / sumM) * other.v;
return newV
}
update() {
this.x += this.v;
}
show(){
const x = constrain(this.x, this.xConstraint, windowWidth)
image(blockImg, x, this.y, this.w, this.w);
}
}