Detecting object collision

please format your code using the
</> Preformatted text button from the editor menu


and for the 2 times 90 000 long arrays want give you a - -


a very basic approach for collision would be to calculate the distance between 2 objects,
on case of array of class elements pls use like z[i].zx or b[i].by


a minified test with 2 classes

A[] as = new A[5];
B[] bs = new B[10];
int loop = 0;

void setup() {
  size(300, 300);
  rectMode(CENTER);
  for (int i = 0; i < as.length; i++) as[i] = new A(100+ i * 20, 100+ i * 20 );
  for (int i = 0; i < bs.length; i++) bs[i] = new B();
  noLoop();
  println("press any key");
}

void draw() {
  background(200, 200, 0);
  for (int i = 0; i < as.length; i++) as[i].show();
  for (int i = 0; i < bs.length; i++) bs[i].show();
  check_close();
  loop++;
}

void check_close() {
  for (int i = 0; i < as.length; i++) {
    for (int j = 0; j < bs.length; j++) {
      float dmin = bs[j].diam/2 + as[i].diam/2 ;
      if ( abs( bs[j].x - as[i].x ) < dmin &&
        abs( bs[j].y - as[i].y ) < dmin  ) {
        println("loop "+loop+" as["+i+"] and bs["+j+"] are close ");
        println("x ", as[i].x, bs[j].x, " y ", as[i].y, bs[j].y, dmin );
      }
    }
  }
}

class A {
  float x, y;                 // from main
  float diam = 5;             // local
  A(float _x, float _y) {
    x = _x;
    y = _y;
  }
  void show() {
    fill(0, 200, 0);
    rect(x, y, diam, diam);
  }
}

class B {
  float x, y;                 // from main
  float diam = 8;             // local
  B() {}
  void show() {
    x=random(width-diam);
    y=random(height-diam);
    fill(200, 0, 0);
    circle(x, y, diam);
  }
}

void keyPressed() {
  redraw();
}


for a serious collision detection see
Collision Detection here

2 Likes