xxxxxxxxxx
59
var mass = this.r *2.3
var gravity = 9.8/30
let balls=[];
let numBalls=50;
function setup() {
createCanvas(500, 500);
for (let i = 0; i < numBalls; i++) {
balls[i] = new PopcornBall (
random(6,150),
(10)
);
}
}
function draw() {
background(155);
for (let i = 0; i < balls.length; i++) {
balls[i].display(700, 200);
balls[i].bounce();
balls[i].move();
frameRate(600)
}
}
class PopcornBall {
constructor(x, y) { // we could add radius or r to the constructor
this.x = x;
this.y = y;
this.xspeed = gravity
this.speed = random(3, 2);
this.r = random(30);
this.mass = 1.5 * this.r
this.gravity = (9.8 * this.mass)/30
}
move() {
//your code here to move with gravity
if (this.x > width){
this.xspeed = this.xspeed * -1}
if (this.x < 6){
this.xspeed = this.xspeed * -1}
this.y = this.y + this.speed
this.x = this.x + this.xspeed
}
bounce() {
if (this.y > 480){
this.speed *= -.781
}
if (this.y < 20){
this.speed *= -.781
}
if (this.y > 490){
this.y = 490
}
this.speed += gravity
this.x > 3
}
display() {
fill(200, 34, 123);
ellipse(this.x, this.y, this.r, this.r);
}
}