# Detecting object collision

**URL:** <https://discourse.processing.org/t/detecting-object-collision/14336>\
**Category:** Coding Questions\
**Created:** [October 1, 2019, 5:44am UTC](https://discourse.processing.org/t/detecting-object-collision/14336 "2019-10-01T05:44:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![quoi](https://avatars.discourse-cdn.com/v4/letter/q/a3d4f5/32.png) [@quoi](https://discourse.processing.org/u/quoi)\
**Post date:** [October 1, 2019, 5:44am UTC](https://discourse.processing.org/t/detecting-object-collision/14336/1 "2019-10-01T05:44:58Z")

</div>

Hello! I’m new to processing, and i’m wondering how can i compare two variables from different classes with each other. Can someone please guide me in the right direction…

if you are wondering here is my code:

```auto
float ang, speed, x, y;
int b_count; 
int z_count = 10;
boolean up,down,left,right, shooting = false;

bullet[] b = new bullet[90000];
zombie[] z = new zombie[90000];

void setup() {
  size(1200, 800);
  smooth();
  ang = 0;
  speed = 3;
  x = width/2;
  y = height/2;
  
   for(int i = 0; i < b.length;i++){
    b[i] = new bullet();
    
  }
  
  for(int i = 0; i < z.length;i++){
    z[i] = new zombie();
    
  }
  
  
}

void draw() {
  rectMode(CORNER);
  background(0);
  pushMatrix();
  translate(x, y);
  rotate(ang);
  rect(-20, -7.5, 40, 15); // Center the rect
  translate(20, 0); // Translate to default Polar Coordinate 0 radians
  ellipse(0, 0, 40, 40); // Display direction
  popMatrix();
  
  for (int i = 0; i < b_count; i++){
    b[i].show(x,y,ang);
    b[i].move();
  }
  
  for (int i = 0; i < z_count; i++){
    z[i].show();
    
  }
  
  
  //keys functions!
  if(down == true){
    x += cos(ang)*speed;
    y += sin(ang)*speed;
  }
  
  if(up == true){
    x -= cos(ang)*speed;
      y -= sin(ang)*speed;
  }
  
  if(left == true){
      ang -= 0.05;
  }
  
  if(right == true){
    ang += 0.05;
  }
  
  
  }

//keys!!
  void keyPressed() {
    if(key == 's'){
      down = true;
    }
    
    if(key == 'w'){
      up = true;
    }
    if(key == 'a'){
      left = true;
    }
    if(key == 'd'){
      right = true;     
    }
    
    ///
    if(key == ' '){
      b_count++;
    }
   
  }
    
    void keyReleased() {
    if(key == 's'){
      down = false;
    }
    
    if(key == 'w'){
      up = false;
    }
    if(key == 'a'){
      left = false;
    }
    if(key == 'd'){
      right = false;     
    }
    
    

    }

class bullet{
  float bx,by,bw,bh,bang,bx_,by_;
  int bc;
  float bspeed = 5;
  
  bullet(){
   
    
  }
  void show(float x_,float y_,float ang_){
    if(bc == 0){
      bx_ = x_;
      by_ = y_;
     bx = 0;
    by = 0;
    bang = ang_/2;
    }
    
    bc++;
    
    pushMatrix();
    translate(bx_,by_);
    rotate(bang);
    rectMode(CENTER);; rect(bx,by,10,10);
    popMatrix();
    
  }
  
  void move(){
   bx -= cos(bang)*bspeed;
    by -= sin(bang)*bspeed;
    
    
  }
  
  
  
  
}

class zombie{
  float zx = random(0,width);
  float zy = random(0,height);
  float r = 10;
  float zspeed = 1;
  boolean hit;
  
  
  void show(){
    ellipse(zx,zy,r*2,r*2);   
  }
}

```

---

<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:** [October 1, 2019, 11:58am UTC](https://discourse.processing.org/t/detecting-object-collision/14336/2 "2019-10-01T11:58:27Z")

</div>

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 - -

* * *

> [@quoi](#):
>
> two variables from different classes

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

```auto
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();
}

```

 ![two_class_collision](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4a2d953894a641321acc82411c92f42d92655ec4.png)

* * *

for a serious collision detection see  
[Collision Detection](http://www.jeffreythompson.org/collision-detection/table_of_contents.php) here
