xxxxxxxxxx
55
function sign(n) {
if (n > 0) return 1
if (n < 0) return -1
return 0
}
function distSq(v1, v2) {
return pow(v1.x - v2.x, 2) + pow(v1.y - v2.y, 2)
}
class Hand {
constructor(x, y, c) {
this.pos = createVector(x, y);
this.vel = createVector();
this.acc = createVector();
this.chasing = c;
}
chase() {
let ball = balls[this.chasing]
let deltaX = ball.pos.x - this.pos.x;
if (abs(deltaX) <= 10) {
this.vel.set(0, 0);
if (distSq(this.pos, ball.pos) <= 1024) {
ball.applyForce(p5.Vector.mult(ball.vel, -1));
ball.applyForce(antigravity);
}
}
this.applyForce(createVector(sign(deltaX), 0));
}
applyForce(force) {
this.acc.add(force);
}
update() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.set(0, 0);
}
display() {
fill(255, 0, 0);
rect(this.pos.x, this.pos.y, 34, 14);
}
throw() {
let ball = balls[this.chasing]
this.chasing = (this.chasing + 1) % balls.length
let direction = p5.Vector.sub(createVector(200, -100), ball.pos).setMag(21.5);
ball.applyForce(direction);
}
}