I was fooling around with some maths and realized that I could use a parabola to represent a bouncing ball. So, naturally, I decided to make it in code.
var Ball = function(r, v, a, y, x) {
this.r = r;
this.v = v;
this.a = a;
this.y = y;
this.x = x;
};
Ball.prototype.draw = function() {
this.y = this.v*this.r*this.r + this.a*this.r;
var NewBallY = 380 - this.y;
ellipse(this.x, NewBallY, 20, 20);
if(this.y < 0) {
this.r = 0;}
this.r += 2;
};
var Ball1 = new Ball(0, random(-0.05, -0.02), random(2.0, 6.0), 40, random(20, 380));
var Ball2 = new Ball(0, random(-0.05, -0.02), random(2.0, 6.0), 40, random(20, 380));
var Ball3 = new Ball(0, random(-0.05, -0.02), random(2.0, 6.0), 40, random(20, 380));
var draw = function() {
background(255, 255, 255);
Ball1.draw();
Ball2.draw();
Ball3.draw();
};
The equation I used to calculate the parabola representing a ball's \$y\$ position is here, where \$y\$ is the y position of the ball, \$V\$ is the velocity, \$A\$ is the acceleration, and \$r\$ is the amount of time passed since the start of the bounce:
$$y=Vr^{2}+Ar$$