Collision check multiple balls

I am trying to make a simple ball dodging game. But i 'm having a small problem with the collision check using dist() function.
Here is the section of the code:
Ball[] balls;

boolean [] keys;
float w;
float e;
float d;
void setup(){
keys = new boolean[128];
w = width/2;
e = height/2;
d = dist(w,e,balls.w,balls.e);
balls = new Ball[35];
for (int i = 0; i < balls.length; i++){
balls[i] = new Ball();
The problem is processing is telling me that w and e are not global variables when using on balls.e and balls.w
What am i doing wrong?

I think the biggest problem you have is that you are trying to work out the distances before you have created the Balls!

The second biggest problem you have is that you need to loop over each Ball in balls. Each Ball has a position - it doesn’t make any sense to try to get the position for a whole array of them at once! That is, balls is many Balls. You need to pick a specific one to work with to find a distance to it.

Also, please format your code, and post enough code so that we can copy, paste, and run your sketch. As it stands now, we are missing your Ball class and your draw() function.

1 Like
void draw() {
    ellipse(w, e, 20, 20);

    for (int i = 0; i < balls.length; i++) {
  if ( w > width) {
    w = 0;
  if (w < 0) {
    w = 600;
  if ( e > height) {
    e = 0;
  if (e < 0) {
    e = 600;
void moveUB() {

  if ( keys  ['a']) 
    w = w - 4;

  if (keys  ['d']) 
    w = w + 4;

  if (keys  ['w']) 
    e = e -4;

  if (keys  ['s']) 
    e = e + 4;
void keyPressed() {
  keys[key] = true;
void keyReleased() {
  keys[key] = false;

class Ball {
  float x;
  float y;
  float e;
  float t;

  Ball() {

      x = 20;
      y = 30;
      t = random(-3, 4);
      e = random(-3, 4);
  void moveBalls() {
    x= x + e;
    y = y +t;
    if (x > width  || x < 10) {
      e = -e;
    if (y > height  || y < 10) {
      t = -t;
  void drawBalls() {
    ellipse(x, y, 30, 30);

Hi tatsuya98,

You could have included your setup() function as well in your previous post.

@TfGuy44 laready pointed out the problem tough.

d = dist(w, e, balls.w, balls.e);
balls = new Ball[35];
for (int i = 0; i < balls.length; i++) {
  balls[i] = new Ball();

On the first line, you are doing balls.w and balls.e: you can’t do that. balls refers to an array of Ball, 35 of them to be precise. This is what your are saying with balls = new Ball[35];. So what does it means to say give me the e value of an array of Ball? Nothing really, because the array is just a list of thing.

Now what you can ask is: what’s the e value of the 3rd Ball I have in my array. That you can do by writing balls[2].e.

I also wanna point out that you don’t have any w variable in your Ball class so I’m not sure of what you are trying to achieve there.

The other problem pointed out by @TfGuy44 is that you are asking for something that you don’t know yet. You are creating your Ball with this peice of code:

for (int i = 0; i < balls.length; i++) {
  balls[i] = new Ball(); // Here to be precise

And this is after you try to get the e value of your Ball. How can the program know what it is then since it is not there yet?

Thank you for the replies.
thanks to both of you the collision check is working now.
So thank you.