P5.js communicating bubbles

I am rewriting my question on P5.js. The same I had asked on this video[https://youtu.be/5Q9cA0REztY] and Dan suggested me to post here.

I tried to do the same example myself. I was able to achieve many things except one … bubble turning on and Off. In your example, when you say b.intersects(other) and then you set color of intersecting bubble with b.changeColor(255). My question is how both intersecting bubbles change their color. In my case only one of the intersecting bubble changes its color.

Another question:
I am using for i = 0 loop syntax and this does not work same file for…of loop. My code below.

for (let i = 0; i < bubbles.length; i++) {
    bubbles[i].show();
    bubbles[i].move();
    let overlapping = false;
    for (let j = 0; j <= i; j++) {
      if(i != j && bubbles[i].intersect(bubbles[j])){
    overlapping = true;
  } 
 }
   if(overlapping) {
    bubbles[i].changeColor(255);
   } else {
    bubbles[i].changeColor(0);
   }
  }

same code with for…of loop works. Dont understand how

for (let b of bubbles) {
  b.show();
  b.move();
   let overlapping = false;
  for (let other of bubbles) {
    if (b != other && b.intersect(other)) {
      overlapping = true;
    }

}
if(overlapping) {
    b.changeColor(255);
   } 
   else {
    b.changeColor(0);
}
}

Please format your code by using </> in the message editor and don’t forget to press Ctrl + T in the Processing IDE to correctly indent your code.

Thanks. Good to know.

1 Like

Got this sketch, in Java Mode, which links bubbles using a red line() if they’re intersecting each other: :smiley_cat:
http://Studio.ProcessingTogether.com/sp/pad/export/ro.989GaZC5t7EkE

I can convert it to p5js if you don’t understand Java syntax. :coffee:

P.S.: This article might help as well: :innocent:
http://JeffreyThompson.org/collision-detection/circle-circle.php

1 Like

The Java mode does not answer my question. I have 2 different questions in my post.

I’ve clearly stated at my previous post: :face_with_raised_eyebrow:

I do understand the syntax and I got what the java program is doing. My example is where two bubbles communicate on their own whereas the JAVA program has one bubble and the other is mouse controlled. The mouse controlled bubble example is clear and work fine with me in P5.js

Ru really describing the same sketch I’ve linked to at my 1st post? :dizzy_face:
http://Studio.ProcessingTogether.com/sp/pad/export/ro.989GaZC5t7EkE

Correct this one. I was not able to open this link earlier. Now I can see that this replicates my problem.