xxxxxxxxxx
123
let { VerletParticle2D, VerletPhysics2D, VerletSpring2D } = toxi.physics2d;
let { Vec2D, Rect } = toxi.geom;
let { GravityBehavior } = toxi.physics2d.behaviors;
let { AttractionBehavior } = toxi.physics2d.behaviors;
let particles = [];
let springs = [];
let particles2 = [];
let springs2 = [];
let physics;
let dir;
let flail;
let cluster;
let bball;
let tennis;
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
//make a world
physics = new VerletPhysics2D();
let gravity = new GravityBehavior(new Vec2D(0, 0.1));
physics.addBehavior(gravity);
let boundry = new Rect(0, 0, width, height);
physics.setWorldBounds(boundry);
angleMode(RADIANS);
//make a particle
let y = height/4;
let x = 200;
let strength = 1;
flail = new Flail(x, y, 20, 300);
//createBallChain(particles, springs, 1, bball, x, y, "b");
// createBallChain(particles2, springs2, strength, tennis, x, y, 2);
//create basketball chain
for (i = 0; i < 13; i++) {
particles[i] = new Particle(x, y, 2);
y += 20;
}
for (i = 0; i < particles.length - 1; i++) {
springs[i] = new Spring(particles[i], particles[i + 1], 20, strength);
}
let bx = particles[particles.length - 1].x;
let by = particles[particles.length - 1].y;
bball = new Ball(bx, by, 70, "b");
//create tennis ball chain
for (i = 0; i < 15; i++) {
particles2[i] = new Particle(x, y, 2);
y += 20;
}
for (i = 0; i < particles2.length - 1; i++) {
springs2[i] = new Spring(particles2[i], particles2[i + 1], 20, strength);
}
tennis = new Ball(bx, by, 30, "t");
}
function draw() {
background(100);
physics.update();
flail.show();
flail.swing();
attachBallChain(particles, springs, bball);
attachBallChain(particles2, springs2, tennis);
// cluster.show();
}
function createBallChain(particles, springs, strength, ball, x, y, type){
for (i = 0; i < 15; i++) {
particles[i] = new Particle(x, y, 2);
y += 20;
}
for (i = 0; i < particles.length - 1; i++) {
springs[i] = new Spring(particles[i], particles[i + 1], 20, strength);
}
let bx = particles[particles.length - 1].x;
let by = particles[particles.length - 1].y;
ball = new Ball(bx, by, 70, type);
}
function attachBallChain(particles, springs, ball) {
//particles[0].lock();
particles[0].x = flail.x + flail.w / 2;
particles[0].y = flail.y - flail.l * 0.04;
particles[0].lock();
for (let i = 0; i < springs.length; i++) {
springs[i].show();
}
if (ball.t == "b") {
ball.showB();
} else if (ball.t == "t") {
ball.showT();
}
ball.update(particles[particles.length - 1]);
}