xxxxxxxxxx
57
class Ball extends LimitedParticle {
constructor(ix, iy, ivx, ivy, ir) {
super(int(ix), int(iy), ivx, ivy, ir);
this.ballColor = color(255, 255, 0);
let baseHue = random(255);
this.rotation = random(radians(360));
let baseVel = this.vel.mag();
this.angularV = (random(baseVel / 2, baseVel)) / 50.0;
// Create a graphics object 'sprite' for drawing the balls.
this.sprite = createGraphics(int(this.radius * 2 + 10), int(this.radius * 2 + 10));
this.sprite.colorMode(HSB);
this.sprite.noStroke();
this.sprite.translate(this.sprite.width / 2, this.sprite.height / 2);
for (let i = 0; i < 3; i++) {
this.sprite.rotate(radians(120));
this.sprite.fill((baseHue + (i * (255 / 3.0))) % 255, 200, 255);
this.sprite.arc(0, 0, this.radius * 2, this.radius * 2, 0, radians(120));
}
this.sprite.fill(255);
this.sprite.ellipse(0, 0, this.radius * 0.8, this.radius * 0.8);
}
update() {
let vMag = this.vel.mag();
// Update the spin on the ball depending on how fast
// it is travelling.
if(vMag > 0.1){
if(vMag > 1.0){
this.rotation += this.angularV;
}
else {
this.rotation += this.angularV * vMag;
}
}
super.update();
}
display() {
push();
translate(this.loc.x, this.loc.y);
rotate(this.rotation);
image(this.sprite, -this.sprite.width / 2, -this.sprite.height / 2);
pop();
}
limit() {
super.limit();
if (this.loc.y > height - 20 - this.radius) {
this.loc.y = height - 20 - this.radius;
this.vel.y = -this.vel.y;
}
}
}