Current File : /home/jeshor13/11bsouth.com/Class2/mover.js |
function Mover(m,x,y) {
this.mass = m;
this.position = createVector(x,y);
this.velocity = createVector(0,0);
this.acceleration = createVector(0,0);
// Newton's 2nd law
this.applyForce = function(force) {
var f = p5.Vector.div(force,this.mass);
this.acceleration.add(f);
};
this.update = function() {
// Velocity changes according to acceleration
this.velocity.add(this.acceleration);
// position changes by velocity
this.position.add(this.velocity);
// We must clear acceleration each frame
this.acceleration.mult(0);
};
this.display = function() {
stroke(0);
strokeWeight(2);
fill(50);
ellipse(this.position.x,this.position.y,this.mass*16,this.mass*16);
};
// Bounce off bottom of window
this.checkEdges = function() {
if (this.position.y > height) {
this.velocity.y *= -0.9; // A little dampening when hitting the bottom
this.position.y = height;
};
if (this.position.y <0 ) {
this.velocity.y *= -0.9; // A little dampening when hitting the bottom
this.position.y = 0;
};
if (this.position.x > width) {
this.velocity.x *= -0.9; // A little dampening when hitting the bottom
this.position.x = width;
};
if (this.position.x < 0) {
this.velocity.x *= -0.9; // A little dampening when hitting the bottom
this.position.x = 0;
};
if (this.velocity > 10) {
this.velocity = 10
};
};
}