Created
March 14, 2016 23:21
-
-
Save oolong32/ed8b7969036d29ee42fd to your computer and use it in GitHub Desktop.
p5.js code for the bouncing ball example in Dan Shiffman’s video ‘PVector class (The Nature of Code)’ <https://vimeo.com/58943395>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var b; | |
function Ball() { | |
this.location = createVector(width/2, height/2); | |
this.velocity = createVector(2.5, -2); | |
this.move = function() { | |
this.location = this.location.add(this.velocity); | |
}; | |
this.bounce = function() { | |
if (this.location.x > width || this.location.x < 0) { | |
this.velocity.x = this.velocity.x * -1; | |
} | |
if (this.location.y > height || this.location.y < 0) { | |
this.velocity.y = this.velocity.y * -1; | |
} | |
}; | |
this.display = function() { | |
background(0); | |
noStroke(); | |
fill(255); | |
ellipse(this.location.x, this.location.y, 32, 32) | |
}; | |
} | |
function setup() { | |
createCanvas(400, 300); | |
b = new Ball(); | |
} | |
function draw() { | |
b.move(); | |
b.bounce(); | |
b.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment