I have an agent called cube (cube its just the name, it is a square in reality). Every cube falls and stops on the ground, and I want them to detect the other cubes and stack up.

I am having problems with this method for detecting collision: https://www.youtube.com/watch?v=GY-c2HO2liA&list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA

I made 2 for loops and all but it doesn't work for some reason.

this is the main script:

var cubes = []; var nb = 10; //number of cubes var gravity = .1; var sz = 10; //cube size function setup() { createCanvas(400, 400); for (i = 0; i < nb; i++) { cubes.push(new Cube()); } } function draw() { background(51); for (var i = 0; i < cubes.length; i++) { cubes[i].show(); for (var j = 0; j < cubes.length; j++) { if (i != j && !cubes[i].collide(cubes[j]) && !(cubes[i].pos.y + sz > height)) { cubes[i].pos.y += gravity; } } } }

this is the cube function linked with p5.js in an HTML file:

function Cube() { this.rx = (round((random(0, width - sz)) / sz) * sz); this.ry = (round((random(sz, height - sz)) / sz) * sz); this.pos = createVector(this.rx, this.ry); this.show = function() { fill(220); noStroke(); rect(this.pos.x, this.pos.y, sz, sz); } this.collide = function(other) { if (this.pos.y + sz == other.pos.y && this.pos.x == other.pos.x) { return true; } else { return false; } } }

I want the cubes to stack up when they hit the ground but they only pass through each other completely ignoring the collision I've set up.