function do_aabb_collision(ax, ay, Ax, Ay, bx, by, Bx, By) {
return ! ((Ax < bx) || (Bx < ax) || (Ay < by) || (By < ay));
function setNewLayout() {
for (var i = 0; i < collisions.length; i++) {
collisions[i].target.x = random(width/2.4, width-width/2.4);
collisions[i].target.y = random(height/2, height);
collisions[i].targetMass = random(cMinMass, cMaxMass);
createCanvas(windowWidth, windowHeight);
waterfallMin = width/2.4;
waterfallMax = width-width/2.4;
spawnSlider = new SliderLayout("Spawn count", 1, 10, 5, 1, 50, 100);
splitSlider = new SliderLayout("Split count", 1, 5, 1, 1, 50, 170);
frictionSlider = new SliderLayout("Bounce", 0.1, 1, 0.3, 0.1, 50, 240);
layoutButton = createButton("New layout");
layoutButton.position(50, 290);
layoutButton.mousePressed(setNewLayout);
for (var i = 0; i < 10; i++) {
var x = random(waterfallMin, waterfallMax);
var y = random(height/2, height);
var mass = random(cMinMass, cMaxMass);
collisions[collisions.length] = new Collision(x, y, mass);
var spawnCount = spawnSlider.slider.value();
for (var num = 0; num < spawnCount; num++) {
var x = random(waterfallMin, waterfallMax);
var mass = random(pMinMass, pMaxMass);
if (particles.length % 5 == 0) {
var displayColor = color(255);
var displayColor = color(random(180, 210), 255, 255);
var newParticle = new Particle(x, 0, mass, displayColor);
particles[particles.length] = newParticle;
for (var i = particles.length-1; i > -1; i--) {
var has_collision = particles[i].resolveCollisions();
if (particles[i].pos.y > height) {
} else if (has_collision && particles[i].vel.mag() < 0.1) {
for (var i = 0; i < collisions.length; i++) {
if (frameCount % 10 == 0) {
fps = frameRate().toFixed(2);
text("FPS " + fps, 50, height-50);
frictionSlider.display();