HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Bouncing ball simulation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
ballbouncingsimulation

Problem

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.vthis.rthis.r + this.a*this.r;
var NewBallY = 380 - this.y;
ellipse(this.x, NewBallY, 20, 20);
if(this.y

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$$

Solution

Using this.r to represent elapsed time is highly unconventional. I would have guessed that r stood for "radius". Why not use this.t like a sane programmer?

The draw() function ends with this.r += 2;, which is odd. Why should the drawing routine manage the passage of time? I would expect the time to be managed either in reference to the wall clock or to be passed in as a parameter by whatever code calls draw().

These don't look like parabolas to me. At best, they would be degenerate ones, since this.x never changes.

The placement of the closing brace here:

if(this.y < 0) {
    this.r = 0;}


is weird. Conventional formatting would be:

if (this.y < 0) {
    this.r = 0;
}

Code Snippets

if(this.y < 0) {
    this.r = 0;}
if (this.y < 0) {
    this.r = 0;
}

Context

StackExchange Code Review Q#67026, answer score: 6

Revisions (0)

No revisions yet.