# Questions about BouncyBubbles example

**URL:** <https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883>\
**Category:** Coding Questions\
**Created:** [November 24, 2018, 12:56pm UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883 "2018-11-24T12:56:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 24, 2018, 12:56pm UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/1 "2018-11-24T12:56:30Z")

</div>

Hello,

I am trying to fully understand the Java [BouncyBubbles](https://processing.org/examples/bouncybubbles.html) example.

Here’s the code:

```auto
int numBalls = 12;
float spring = 0.05;
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];

void setup() {
  size(640, 360);
  for (int i = 0; i < numBalls; i++) {
    balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls);
  }
  noStroke();
  fill(255, 204);
}

void draw() {
  background(0);
  for (Ball ball : balls) {
    ball.collide();
    ball.move();
    ball.display();  
  }
}

class Ball {
  
  float x, y;
  float diameter;
  float vx = 0;
  float vy = 0;
  int id;
  Ball[] others;
 
  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
  } 
  
  void collide() {
    for (int i = id + 1; i < numBalls; i++) {
      float dx = others[i].x - x;
      float dy = others[i].y - y;
      float distance = sqrt(dx*dx + dy*dy);
      float minDist = others[i].diameter/2 + diameter/2;
      if (distance < minDist) { 
        float angle = atan2(dy, dx);
        float targetX = x + cos(angle) * minDist;
        float targetY = y + sin(angle) * minDist;
        float ax = (targetX - others[i].x) * spring;
        float ay = (targetY - others[i].y) * spring;
        vx -= ax;
        vy -= ay;
        others[i].vx += ax;
        others[i].vy += ay;
      }
    }   
  }
  
  void move() {
    vy += gravity;
    x += vx;
    y += vy;
    if (x + diameter/2 > width) {
      x = width - diameter/2;
      vx *= friction; 
    }
    else if (x - diameter/2 < 0) {
      x = diameter/2;
      vx *= friction;
    }
    if (y + diameter/2 > height) {
      y = height - diameter/2;
      vy *= friction; 
    } 
    else if (y - diameter/2 < 0) {
      y = diameter/2;
      vy *= friction;
    }
  }
  
  void display() {
    ellipse(x, y, diameter, diameter);
  }
}

```

I don’t understand why in setup the creation of balls seem to refer to itself with the last parameter:  
`balls[i] = new Ball(random(width), random(height), random(30, 70), i, **balls** );`

Also, the link between others,oin and Ball is not clear as well:

```auto
  Ball[] others;
 
  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
  }

```

Then, this _for_ routine is strange: `for (Ball ball : balls)`. Why neither semicolons nor comparison but a colon?

Thank you for helping.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 24, 2018, 1:30pm UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/2 "2018-11-24T13:30:33Z")

</div>

> [@amundsen](#):
>
> Then, this _for_ routine is strange: `for (Ball ball : balls)` . Why neither semicolons nor comparison but a colon?

```auto
// balls is a list of objects of class Ball
// for (Ball ball : balls)
// means loop through all objects in list balls and give me each as :
// ball of class Ball

// more easy example:
String[] words = new String[] {"abc","def","ghi"};

for ( String word : words ) println(word);

/*
abc
def
ghi
*/

```

“:”==“in”  
pls. speak:

> for each word in words

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 24, 2018, 5:37pm UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/3 "2018-11-24T17:37:27Z")

</div>

You can have a look here, we were speaking about it: [Help a newbie with polymorphic particles](https://discourse.processing.org/t/help-a-newbie-with-polymorphic-particles/5068)

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 24, 2018, 7:33pm UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/4 "2018-11-24T19:33:43Z")

</div>

Got it!

What about the other question?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 25, 2018, 1:30am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/5 "2018-11-25T01:30:39Z")

</div>

i also just read it and, what can i say:  
you and me need another year of hard work before we would be able  
to come up with that kind of idea / structured coding,  
so let us forget how that is be done by the “genius” and start at the beginning:  
WHY?

a class is a powerful thing what has grown over the decades

- -a- you have basics like int float…
- -b- you have array of that basics or even , but these are all same type
- -c- to come over that limitation there has been  
something like struct / records, a definition of a mix of variables of different type  
( much like a data line in a excel table / the header line explains what they mean )  
what now is the first step of a class
- -d- more, add to the public “record” you can have local variables
- -e- and that makes the today class: IT can have methods

but over all a class describes ONE object ( with public and private properties ++)  
that is the

> class Ball

and one object would be a “ball of type Ball”

> Ball ball

or you can make a list / array of items of that type Ball

> Ball balls

and that would be all independent from each other,  
and the class Ball don’t even know that there are others  
unless you make ( from the outside ) a loop over all “balls”  
to check on something, like the distance of each / to each other.

But if you now not want do that check loop from the outside,  
you might try to do that inside, as a method of the class,  
what is impossible unless the class has a link to the others.  
( and by the way, that is insofar a easy example as it refs to “its” array of this class,  
it might be that you want to check on objects of a other class “bricks of Brick” )

and that link to the array is what you see in the Ball definition

> Ball oin

used in the

> Ball.collide()

method.

so, sorry, i am good with it, just to know something like this can be done.  
i go back to make my own easy/beginner examples

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 9:58am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/6 "2018-11-25T09:58:27Z")

</div>

Things became a bit less complex when I understood that _ball_ was just the name of an iterator such as _i_ in usual for loops and it has nothing to do with _balls_ apart from the purpose of the sketch.

The loop could has been coded like this:

```auto
  for (Ball dadido : balls) {
    dadido.collide();
    dadido.move();
    dadido.display();  
  }

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 25, 2018, 10:05am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/7 "2018-11-25T10:05:33Z")

</div>

yes, that’s why you write

```auto
for ( int i

```

right, a loop local variable

but regarding your:

> [@amundsen](#):
>
> fully understand

sorry: homework? try

```auto
int numBalls = 3;
//.. and after
      float minDist = others[i].diameter/2 + diameter/2;
      println("i:"+i+" id: "+id+" minDist: "+minDist);

```

is that looping clear?

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 10:07am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/8 "2018-11-25T10:07:06Z")

</div>

> [@kll](#):
>
> But if you now not want do that check loop from the outside,  
> you might try to do that inside, as a method of the class,  
> what is impossible unless the class has a link to the others.  
> ( and by the way, that is insofar a easy example as it refs to “its” array of this class,  
> it might be that you want to check on objects of a other class “bricks of Brick” )
> 
> and that link to the array is what you see in the Ball definition
> 
> > Ball oin

This is now clear but still it’s strange to have this part of code below, because it seems one needs to provide a Ball object to define a Ball object. So it seems there’s a self-reference.

```auto
  Ball(float xin, float yin, float din, int idin, Ball[] oin) {
    x = xin;
    y = yin;
    diameter = din;
    id = idin;
    others = oin;
  }

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 25, 2018, 10:11am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/9 "2018-11-25T10:11:01Z")

</div>

no, not NEED, just WANT to use the data of a “external” array  
funny that it is a array of its own class type  
again, a class Ball does not know if/that you made a object ball or a array balls  
of its or any kind.

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 10:16am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/10 "2018-11-25T10:16:29Z")

</div>

> [@kll](#):
>
> no, not NEED, just WANT to use the data of a “external” array  
> funny that it is a array of its own class type  
> again, a class Ball does not know if/that you made a object ball or a array balls  
> of its or any kind.

Does it mean that the class Ball and the object Ball are not the same thing despite the same name?

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 10:18am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/11 "2018-11-25T10:18:09Z")

</div>

> [@kll](#):
>
> sorry: homework? try
> 
> ```auto
> int numBalls = 3;
> //.. and after
> float minDist = others[i].diameter/2 + diameter/2;
> println("i:"+i+" id: "+id+" minDist: "+minDist);
> 
> ```
> 
> is that looping clear?

Yes, it’s a way to avoid checking twice if a collision occurs between the two same balls. When you check the collision between the first ball and the others, there is a maximum number (2) of collisions to check, but the more you progress into the array of balls the number of collisions to check is reduced because actually these collisions have already been checked but from the opposite point of view, the other ball possibly involved.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 25, 2018, 10:18am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/12 "2018-11-25T10:18:20Z")

</div>

> [@amundsen](#):
>
> class Ball and the object Ball

try it and you will get a ERROR  
a class is a description  
and a object is a (higher) variable of a type ( int, float, class Ball … )

add there is a naming convention for class Big letter, variable small letter

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 10:19am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/13 "2018-11-25T10:19:07Z")

</div>

> [@kll](#):
>
> try it and you will get a ERROR

Try what? I don’t get it.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 25, 2018, 10:21am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/14 "2018-11-25T10:21:08Z")

</div>

“Ball” can not be used 2 times, as name of a class and as name of a variable/object  
so use class “Ball” and object “ball” or “balls”

but possibly the whole naming thing is bad?

```auto
String string;
String[] strings;

```

who would use that?  
possibly better:

```auto
class Ball;
Ball my_ball;
Ball[] my_array_of_balls;

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 25, 2018, 10:26am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/15 "2018-11-25T10:26:48Z")

</div>

> [@amundsen](#):
>
> This is now clear but still it’s strange to have this part of code below, because it seems one needs to provide a Ball object to define a Ball object. So it seems there’s a self-reference.
> 
> ```auto
> Ball(float xin, float yin, float din, int idin, Ball[] oin) {
> x = xin;
> y = yin;
> diameter = din;
> id = idin;
> others = oin;
> }
> 
> ```

~~The idea is that every `Ball` object you are creating get the list of all the other `Ball` objects that you have created before: that is the oin variable so it is not a self-reference. 

In the code, every time you create a `Ball` object, you add it the the `balls` array and you also give that list to the constructor of the `Ball` object.

 

The first time, the `balls` array is empty and you create the first `Ball` object: let’s call it Ball01. Ball01 is now created and its `others` array is empty (since the `balls` one was).

 

Now let’s create the second one. The `balls` array contain 1 `Ball` object: Ball01. Ball02 is now created and its `others` array contain Ball01.

 

And so on and so on. This way the `others` array of Ball03 will have Ball01 and Ball02 inside.

 

The idea is that after, when you want to collide you can just loop through all your `Ball` in the `balls` array without having to check for double collisions. You ca simply call the collide method that will check collision only with the `Ball` in `others` so only with the Ball created before.

~~

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 10:29am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/16 "2018-11-25T10:29:15Z")

</div>

> [@kll](#):
>
> “Ball” can not be used 2 times, as name of a class and as name of a variable/object  
> so use class “Ball” and object “ball” or “balls”

That was my basic understanding. However if the instance’s (?) name is different, the class/objet is the same and seems to refer to itself. The class includes an array with objects ot its own kind and which have not been defined yet?

```auto
class Ball {
  ...
  Ball[] others;

```

And how can the description of the object Ball include an array of Ball???  
`Ball(float xin, float yin, float din, int idin, Ball[] oin)`

I must be missing somehing, huh?

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 10:36am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/17 "2018-11-25T10:36:25Z")

</div>

[jb4x](https://discourse.processing.org/u/jb4x) your explanation arrived while I was typing my last message. It’s still not crystal clear but it helps a lot. Thank you.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 25, 2018, 10:36am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/18 "2018-11-25T10:36:55Z")

</div>

> [@amundsen](#):
>
> The class includes an array with objects ot its own kind and which have not been defined yet?

yes, imagine how smart the compiler must be

---

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [November 25, 2018, 10:37am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/19 "2018-11-25T10:37:47Z")

</div>

> [@kll](#):
>
> yes, imagine how smart the compiler must be

Yes, that’s a bit beyond common sense.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 25, 2018, 10:38am UTC](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883/20 "2018-11-25T10:38:03Z")

</div>

I’m writing a piece of code to illustrate. Hope it will make it crystal clear 🙂

[Next page](https://discourse.processing.org/t/questions-about-bouncybubbles-example/5883.md?page=2)
