# Access other objects in an object array

**URL:** <https://discourse.processing.org/t/access-other-objects-in-an-object-array/40716>\
**Category:** Coding Questions\
**Created:** [January 26, 2023, 4:20am UTC](https://discourse.processing.org/t/access-other-objects-in-an-object-array/40716 "2023-01-26T04:20:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![geggslegs](https://avatars.discourse-cdn.com/v4/letter/g/839c29/32.png) [@geggslegs](https://discourse.processing.org/u/geggslegs)\
**Post date:** [January 26, 2023, 4:20am UTC](https://discourse.processing.org/t/access-other-objects-in-an-object-array/40716/1 "2023-01-26T04:20:12Z")

</div>

I’m trying to write a 2d ball pit simulation using verlet integration.  
But I’m having trouble accessing the other balls while looping through them.  
In the section commented “//collide the balls” I’m trying to access the other balls but it seems to return true everytime and all the balls instantly turn white, I think the issue might be that I’m accessing the other balls in the array wrong.  
Any help would be appreciated!

```auto
Particle[] balls = new Particle[100];
float mapSize;

void setup() {
  size(640, 640);
  mapSize = min(width, height)-32;
  noStroke();
  colorMode(HSB, 360, 100, 100);
  for(int i=0; i<balls.length; i++) {
    balls[i] = new Particle(random(100, width-100), random(100, height-100), color(random(0, 360), 100, 100), 10);
  }
}

void draw() {
  background(64);
  fill(0);
  ellipse(width/2, height/2, mapSize, mapSize);
  for(int i=0; i<balls.length; i++) {
    balls[i].updateSpeed();
  }
  for(int i=0; i<balls.length; i++) {
    balls[i].updatePosition();
  }
  for(int i=0; i<balls.length; i++) {
    balls[i].collision();
  }
  for(int i=0; i<balls.length; i++) {
    balls[i].display();
  }
}

class Particle {
  float posX;
  float posY;
  float speedX;
  float speedY;
  color c;
  float diameter;
  float PposX;
  float PposY;
  
  Particle(float posXTemp, float posYTemp, color colorTemp, float diameterTemp) {
    posX = posXTemp;
    posY = posYTemp;
    PposX = posXTemp + random(-10, 10);
    PposY = posYTemp + random(-10, 10);
    c = colorTemp;
    diameter = diameterTemp;
  }
  void updateSpeed() {
    //calculate speed
    speedX = posX - PposX;
    speedY = posY - PposY;
    //apply gravity to speed
    speedY = speedY + 0.1;
  }
  void updatePosition() {
    //store old position
    PposX = posX;
    PposY = posY;
    //calculate new position
    posX = posX + speedX;
    posY = posY + speedY;
  }
  void collision() {
    //move position back into bounds
    if(dist(posX, posY, width/2, height/2) > (mapSize/2)-(diameter/2)) {
      float outOfBoundsDist = ((dist(posX, posY, width/2, height/2) - (mapSize/2))+(diameter/2)) / mapSize;
      posX = lerp(posX, width/2, outOfBoundsDist*1.6);
      posY = lerp(posY, height/2, outOfBoundsDist*1.6);
    }
    //collide the balls
    for(int j=0; j<balls.length; j++) {
      if(dist(balls[j].posX, balls[j].posY, posX, posY) < (diameter/2) + (balls[j].diameter/2)) {
        c = color(0, 0, 100);
      }
    }
  }
  void display() {
    fill(c);
    ellipse(posX, posY, diameter, diameter);
  }
}

```

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [January 26, 2023, 4:23am UTC](https://discourse.processing.org/t/access-other-objects-in-an-object-array/40716/2 "2023-01-26T04:23:16Z")

</div>

Every ball is colliding with itself.

Instead, only test each ball `i` with balls `0` through `i-1`. When you detect a collision, set both of their colors.

But also, you need to set the color back to non-colliding before your collision test or it will always stay set once a collision has occurred. Unless that’s your intention.

---

<div class="post-metadata">

**Author:** ![geggslegs](https://avatars.discourse-cdn.com/v4/letter/g/839c29/32.png) [@geggslegs](https://discourse.processing.org/u/geggslegs)\
**Post date:** [January 29, 2023, 5:10am UTC](https://discourse.processing.org/t/access-other-objects-in-an-object-array/40716/3 "2023-01-29T05:10:58Z")

</div>

The issue I was having appeared to be that the variable weren’t corresponding to the correct ball, I fixed it by moving it out of the class and then marking each variable with balls[i] or balls[j], which appeared to fix it

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 29, 2023, 7:57am UTC](https://discourse.processing.org/t/access-other-objects-in-an-object-array/40716/4 "2023-01-29T07:57:09Z")

</div>

Please show this code section, we are interested

---

<div class="post-metadata">

**Author:** ![geggslegs](https://avatars.discourse-cdn.com/v4/letter/g/839c29/32.png) [@geggslegs](https://discourse.processing.org/u/geggslegs)\
**Post date:** [February 9, 2023, 10:48pm UTC](https://discourse.processing.org/t/access-other-objects-in-an-object-array/40716/5 "2023-02-09T22:48:01Z")

</div>

Sorry but I no longer have this code, after a lot of optimization it works completely differently now
