# Help with collision between two array objects

**URL:** https://discourse.processing.org/t/help-with-collision-between-two-array-objects/20827
**Category:** Coding Questions
**Created:** [May 13, 2020, 2:33pm UTC](https://discourse.processing.org/t/help-with-collision-between-two-array-objects/20827 "2020-05-13T14:33:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![maverick24](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@maverick24](https://discourse.processing.org/u/maverick24)
#### Post date: [May 13, 2020, 2:33pm UTC](https://discourse.processing.org/t/help-with-collision-between-two-array-objects/20827/1 "2020-05-13T14:33:13Z")

</div>

I’m a beginner coder learning in Processing and I’m trying to figure out how I can detect a collision between two array objects so that both objects can no longer display on the screen.

The game is a shooter game, but I tried to simplify the code as much as I could. Any help would be greatly appreciated.

```auto
//arrays
Meteor[] meteor = new Meteor[10];
ArrayList<Bullet> bullets;

//variables
float rectX = 300;
int speed = 1;
int meteorW = 90;
int bulletChange = 3;
int meteorXMin = 50;
int meteorXMax = 550;
int bulletWidth = 10;
int bulletHeight = 25;
int bulletYStart = 730;

//image
PImage meteors;

void setup() {
  size(600, 800);
  
  //meteors
  for(int i = 0; i < meteor.length; i++) {
    meteor[i] = new Meteor(random(meteorXMin, meteorXMax), random(-2 * height));
    
  //bullets
   bullets = new ArrayList<Bullet>();
   bullets.add(new Bullet(width/2, 0, bulletWidth, bulletHeight));
  }
  
  //load and resize image
  imageMode(CENTER);
  meteors = loadImage("meteor.png");
  meteors.resize(meteorW, meteorW); 
}

void draw() {
  background(0);
  
  //meteor objects
    for(int i = 0; i < meteor.length; i++) {
    meteor[i].drawTheMeteors();
    meteor[i].moveTheMeteors();
  }
  
  //bullet objects
  for (int i = bullets.size()-1; i >= 0; i--) { 
    Bullet bullet = bullets.get(i);
    bullet.moveTheBullets();
    bullet.displayTheBullets(); 
  }  
}

void mousePressed() {
  bullets.add(new Bullet(rectX, bulletYStart, bulletWidth, bulletHeight));
}

class Bullet {
  float x;
  float y;
  float speed;
  float w;
  float h;
  float life = 255;
  
  Bullet(float tempX, float tempY, float tempW, float tempH) {
    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    speed = 3;
  }  

  void moveTheBullets() {
    y = y - speed;
  }
  
  void displayTheBullets() {
    fill(255, 0, 0);
    ellipse(x, y, w, h);
  }
}

class Meteor {
  float x;
  float y;
  float speed = 1;
  
  Meteor(float tempX, float tempY) {
    x = tempX;
    y = tempY;
  }
  
  void drawTheMeteors() {
    image(meteors, x, y);
  }
  
  void moveTheMeteors() {
    y = y + speed;
  }
}

```

---

<div class="post-metadata">

### Author: ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)
#### Post date: [May 13, 2020, 3:02pm UTC](https://discourse.processing.org/t/help-with-collision-between-two-array-objects/20827/2 "2020-05-13T15:02:23Z")

</div>

Hello @maverick24,

You will need to check the distance between objects. And this can be done with the **dist()** function. Excellent tutorial here by Abe Pazos on youtube on how to use:

[![]( " - YouTube") ](https://www.youtube.com/watch?v=N5Ka4C3RVmw&t=293s)

Also, I recommend looking in **reference section** of [processing.org](http://processing.org) site.  
As well as **this example in the examples section** for slightly more advanced implementation:  
[Circle Collision with Swapping Velocities / Examples / Processing.org](https://processing.org/examples/circlecollision.html)

🤓
