# How to make balls in an array bounce of each other?

**URL:** <https://discourse.processing.org/t/how-to-make-balls-in-an-array-bounce-of-each-other/583>\
**Category:** Coding Questions\
**Created:** [May 31, 2018, 10:53pm UTC](https://discourse.processing.org/t/how-to-make-balls-in-an-array-bounce-of-each-other/583 "2018-05-31T22:53:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eshad](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@Eshad](https://discourse.processing.org/u/Eshad)\
**Post date:** [May 31, 2018, 10:53pm UTC](https://discourse.processing.org/t/how-to-make-balls-in-an-array-bounce-of-each-other/583/1 "2018-05-31T22:53:45Z")

</div>

I just started coding and I’m sorry if i made your eyes bleed

this my collison detection

```auto
class Collisions {
  float dir;
  Collisions () {
  }
  void BallCollide(float x, float y, float d) {
    for (int i = 0; i < Ball.length; i++) {
      if (dist(x, y, Ball[i].x, Ball[i].y) >0 && dist(x, y, Ball[i].x, Ball[i].y) < Ball[i].radius *2) {
        dir = -d;
      } else {
      dir = d;
      }
    }
  }
  float getDir() {
  return dir;
  }

```

---

<div class="post-metadata">

**Author:** ![dan850](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dan850](https://discourse.processing.org/u/dan850)\
**Post date:** [June 1, 2018, 5:19pm UTC](https://discourse.processing.org/t/how-to-make-balls-in-an-array-bounce-of-each-other/583/2 "2018-06-01T17:19:08Z")

</div>

Hi @Eshad can you explain what’s your code is doing so far? I would recommend looking into this tutorial as you need a little bit more physics to pull this off [https://channel9.msdn.com/Series/Sketchbooktutorial/Simple-Collision-Detection-and-Response](https://channel9.msdn.com/Series/Sketchbooktutorial/Simple-Collision-Detection-and-Response) hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 4, 2018, 12:57am UTC](https://discourse.processing.org/t/how-to-make-balls-in-an-array-bounce-of-each-other/583/3 "2018-06-04T00:57:11Z")

</div>

There are several great collision detection tutorials in Processing.

There is a single extensively annotated example, CircleCollision, which deals with the balls array issue (although it isn’t a class-based solution):

- [https://processing.org/examples/circlecollision.html](https://processing.org/examples/circlecollision.html)

And there are two full walkthrough tutorials:

1. Collision Detection (Jeff Thompson) [http://www.jeffreythompson.org/collision-detection/](http://www.jeffreythompson.org/collision-detection/)

2. Collision Detection (Kevin Workman) [http://happycoding.io/tutorials/processing/collision-detection](http://happycoding.io/tutorials/processing/collision-detection)
