xxxxxxxxxx
73
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
// Mover object
let particles = [];
let springs = [];
function setup() {
createCanvas(700, 360);
// Create objects at starting position
// Note third argument in Spring constructor is "rest length"
let offset =width/2;
let total = width / offset;
let prev = null;
for (let i = 0; i < total + 1; i++) {
particles[i] = new Particle( i*offset, height / 2);
if (prev) {
let s = new Spring(prev, particles[i], offset*0.1, 6);
springs.push(s);
}
prev = particles[i];
}
particles[0].lock();
particles[particles.length - 1].lock();
// Constrain spring distance between min and max
// spring.constrainLength(bob, 30, 200);
}
function draw() {
background(0);
// Apply a gravity force to the bob
// var gravity = createVector(0, 2);
//bob.applyForce(gravity);
//anchor.applyForce(gravity);
for (let p of particles) {
p.update();
if (mouseIsPressed) {
p.display();
}
p.handleDrag(mouseX, mouseY);
}
for (let s of springs) {
s.update();
s.display();
//s.constrainLength2(1);
}
}
function mousePressed() {
for (let p of particles) {
p.handleClick(mouseX, mouseY);
}
}
function mouseReleased() {
for (let p of particles) {
p.stopDragging();
}
}