xxxxxxxxxx
31
class Square {
constructor(x, w, m, v) {
this.x = x
this.w = w
this.m = m
this.v = v
}
hitWall() {
return this.x <= 0;
}
reverse() {
this.v *= -1;
}
show() {
rectMode(CORNER)
rect(this.x, 200 - this.w, this.w)
}
update() {
this.x += this.v
}
collide(other) {
return !(this.x + this.w < other.x || this.x > other.x + other.w);
}
bounce(other) {
let sumM = this.m + other.m;
let newV = ((this.m - other.m) / sumM) * this.v;
newV += ((2 * other.m) / sumM) * other.v;
return newV;
}
}