Pushy universe
A O(n²) simulation of repulsive forces. Click to add new points.
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 30; i ++) {
points[i] = [random(400), random(400), 0, 0]
}
}
let points = []
function draw() {
background(220);
let s = 0
for (let i = 0; i < points.length; i ++) {
for (let j = 1; j < points.length; j ++) {
let k = (i + j + points.length) % points.length
let minDiff = [(points[i][0] - points[k][0] + 600) % 400 - 200, (points[i][1] - points[k][1] + 600) % 400 - 200]
let minD = minDiff[0]**2 + minDiff[1]**2
let minDrt = sqrt(minD)
s += minDrt
if (minD > 1) {
points[i][2] += minDiff[0] / minD
points[i][3] += minDiff[1] / minD
}
}
}
for (let i = 0; i < points.length; i ++) {
points[i][2] *= 0.99
points[i][3] *= 0.99
points[i][0] += points[i][2]
points[i][1] += points[i][3]
points[i][0] = (points[i][0] % 400 + 400) % 400
points[i][1] = (points[i][1] % 400 + 400) % 400
circle(points[i][0], points[i][1], 10)
circle(points[i][0]-400, points[i][1], 10)
circle(points[i][0], points[i][1]+400, 10)
circle(points[i][0]+400, points[i][1], 10)
circle(points[i][0], points[i][1]-400, 10)
}
//text(s.toFixed(3), 0, 10)
}
function mousePressed() {
points.push([mouseX, mouseY, 0, 0])
}(Originally seen at https://editor.p5js.org/bojidar-bg/sketches/lD2-zkf3f)
Browse more articles?
← Teleportation effect Experiments tagged p5 (34/85) Scroll-to-rotate, 2D →
← Teleportation effect Experiments tagged interactive (12/26) Scroll-to-rotate, 2D →
← Teleportation effect Experiments on this site (34/85) Scroll-to-rotate, 2D →