xxxxxxxxxx
136
let polys = [];
let vel;
let allPolys = [];
let walls = [];
let interval = 20;
const root2 = Math.pow(2, 0.5);
let gravity;
let Walls;
function setup() {
frameRate(40);
createCanvas(800, 800);
c1 = color(226, 328, 246);
c2 = color(180, 180, 180);
//////walls
//top
walls[0] = new SAT.Polygon(width / 2, height, 5, 4);
walls[0].verts = [
createVector(-width / 2, 0),
createVector(-width / 2, 5),
createVector(width / 2, 5),
createVector(width / 2, 0),
];
walls[0].isStatic = true;
//bottom
walls[1] = new SAT.Polygon(width / 2, 0, 5, 4);
walls[1].verts = [
createVector(-width / 2, 0),
createVector(-width / 2, 5),
createVector(width / 2, 5),
createVector(width / 2, 0),
];
walls[1].isStatic = true;
//left
walls[2] = new SAT.Polygon(0, width / 2, 5, 4);
walls[2].verts = [
createVector(-width / 2, 0),
createVector(-width / 2, 5),
createVector(width / 2, 5),
createVector(width / 2, 0),
];
//right
walls[2].angle = PI / 2;
walls[2].isStatic = true;
walls[3] = new SAT.Polygon(width, width / 2, 5, 4);
walls[3].verts = [
createVector(-width / 2, 0),
createVector(-width / 2, 5),
createVector(width / 2, 5),
createVector(width / 2, 0),
];
walls[3].angle = PI / 2;
walls[3].isStatic = true;
//////
gravity = createVector(0, 0.1);
}
function draw() {
setGradient(c1, c2);
if (frameCount % (interval * 20) == 0 && frameCount < 5000) {
allPolys.push(
new SAT.Polygon(random(40, width - 40), random(40, height / 2), 40, 80)
);
}
if (frameCount % (interval * 10) == 0 && frameCount < 5000) {
allPolys.push(
new SAT.Polygon(random(40, width - 40), random(40, height / 2), 50, 4)
);
}
for (let poly of allPolys) {
poly.show();
}
for (let i = 0; i < allPolys.length; i++) {
for (let j = 0; j < allPolys.length; j++) {
if (i === j) continue;
const poly = allPolys[i];
const poly2 = allPolys[j];
let mtv = SAT.intersectsPolyPoly(poly2, poly);
if (mtv) {
poly.vel.add(mtv.copy());
}
}
}
for (const wall of walls) {
for (const poly of allPolys) {
let mtv = SAT.intersectsPolyPoly(wall, poly);
if (mtv) {
poly.vel.add(mtv.copy());
poly.vel.add(poly.acc);
poly.vel.mult(0.95);
}
}
}
for (const poly of allPolys) {
if (poly.isStatic) continue;
poly.acc.add(gravity);
poly.pos.add(poly.vel);
poly.vel.add(poly.acc);
poly.vel.mult(0.95);
poly.vel.limit(1);
poly.acc.set(createVector());
poly.angle = poly.vel
.copy()
.rotate(-PI / poly.verts.length)
.heading();
}
}
function setGradient(c1, c2) {
noFill();
for (var y = 0; y < height; y++) {
var inter = map(y, 0, height, 0, 1);
var c = lerpColor(c1, c2, inter);
stroke(c);
line(0, y, width, y);
}
}