xxxxxxxxxx
55
class Ball{
constructor(r,m,pos,v) {
this.r = r;
this.m = m;
this.pos = pos;
this.v = v;
this.a = createVector(0,0);
}
show() {
fill(255);
ellipse(this.pos.x, this.pos.y, 2*this.r, 2*this.r);
noFill();
}
applyForce(F) {
this.a.add(F.div(this.m));
}
step() {
this.pos.add(this.v)
this.pos.add(this.a.div(2));
this.v.add(this.a);
this.a = createVector(0,0);
}
}
function setup() {
createCanvas(400, 400);
p = createVector(200, 300);
v = createVector(4, 0);
b = new Ball(8,1,p,v);
nail = createVector(200,100);
l = 200;
k = 0.03;
}
function draw() {
background(0);
fill(255);
ellipse (nail.x,nail.y,10,10);
F = p5.Vector.sub(nail,b.pos);
springForce = k*(F.mag()-l);
F.setMag(springForce);
g = createVector(0,1);
b.applyForce(F);
b.applyForce(g);
b.show();
b.step();
}