const forceMultiplier = 0.25;
const node = function (x, y, pinned) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.force = createVector(0, 0);
const acc = p5.Vector.mult(this.force,forceMultiplier);
this.vel.limit(speedLimit);
const link = function (node1, node2) {
const difference = node2.pos.copy().sub(node1.pos);
this.node1.pinned || this.node1.force.add(difference);
this.node2.pinned || this.node2.force.sub(difference);
const m = min(windowWidth, windowHeight);
speedLimit = (width / gridCount) * 1.5;
nodeArray = createNodes();
linkArray = createLinks(nodeArray);
for (let j = 0; j < gridCount; j++) {
for (let i = 0; i < gridCount; i++) {
i == 0 || j == 0 || i == gridCount - 1 || j == gridCount - 1
const x = map(i, 0, gridCount - 1, 0, width - 1);
const y = map(j, 0, gridCount - 1, 0, height - 1);
nodes.push(new node(x, y, pinned));
function createLinks(nodes) {
for (let i = 0; i < nodes.length; i++) {
const current = nodes[i];
const rest = nodes.slice(i + 1);
const neighbors = rest.filter(
(target) => current.pos.dist(target.pos) <= width / (gridCount - 1)
neighbors.forEach((target) => {
if (current.pinned == false || target.pinned == false)
links.push(new link(current, target));
mouseIsPressed && grabNodesNearMouse();
linkArray.forEach((link) => link.update());
nodeArray.forEach((node) => node.update());
linkArray.forEach((link) => link.show());
function grabNodesNearMouse() {
const mouse = createVector(mouseX, mouseY);
const nodesNearMouse = nodeArray.filter(
(node) => node.pinned == false && mouse.dist(node.pos) < mouseRadius
nodesNearMouse.forEach((node) => (node.pos = mouse.copy()));