WORKING!
Over the past 2 days I have been relentlessly googling and trawling old forums trying to get this collision detection working!
I finally found this version, and implemented the piece of code proposed by Quark:
for (int i = 0; i < blobs.size() - 1; i++) {
for (int j = i + 1; j <blobs.size(); j++) {
if (dist(blobs.get(i).minx, blobs.get(i).miny, blobs.get(j).minx, blobs.get(j).miny) <= 100) {
fill (255,0,0);
} else {
fill (255);
}
}
}
It only works when I put it inside the class Blob, but, when it detects collision, ALL the blobs turn red?? I only want the blobs that have collided to turn red… Any ideas?
I also tried to turn it into a boolean so I could allocate colour/behaviour in the main sketch to future-proof the concept when I eventually want to load different images or behaviours…
boolean checkCollision() {
for (int i = 0; i < blobs.size() - 1; i++) {
for (int j = i + 1; j <blobs.size(); j++) {
if (dist(blobs.get(i).minx, blobs.get(i).miny, blobs.get(j).minx, blobs.get(j).miny) <= 100) {
return true;
} else {
return false;
}
}
}
}
but it came up with the error “This method must return a result of type boolean”