xxxxxxxxxx
34
let anchor,
bob,
velocity,
springForceAmount,
airFrictionAmount,
gravityConstant;
function setup() {
createCanvas(720, 405);
anchor = createVector(width / 2, height / 2);
bob = createVector(0, 0);
velocity = createVector(0, 0);
springForceAmount = 0.02;
airFrictionAmount = 1.01;
gravityConstant = 1;
}
function draw() {
background(22);
strokeWeight(32);
stroke(127);
point(anchor);
strokeWeight(5);
line(anchor.x, anchor.y, bob.x, bob.y);
strokeWeight(64);
stroke(233);
point(bob);
velocity.add(p5.Vector.mult(p5.Vector.sub(bob, anchor), -springForceAmount));
velocity.add(createVector(0, gravityConstant));
velocity.div(airFrictionAmount);
if (velocity.mag() > 1)
bob.add(velocity);
}