Help with collision between two array objects

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.

//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;
 }
}

Hey sorry I can’t run your code, it gives me a NullPointerException because it can’t find the meteor.png file. (Normally when I post code I try to draw colored rectangles so it doesn’t require any images!)

If it helps, I wrote a tutorial about collision detection here:

It sounds like you’re looking for the “Collision Detection between Many Objects” section in that tutorial.

Let me know if that makes sense!

Thank you for your response! I know that tutorial has helped me countless times in the past, but I can’t figure out exactly what I’m doing wrong this time. From the previous post, I changed the meteor file to a circle to make it easier. I also commented out the code I was using for the collision (it said that I wasn’t using global variables). I applied your method under the “Collision Detection between Many Objects” to the best of my knowledge. If you could let me know where I went wrong, that would be great!

//outer space stars array
Meteor[] meteor = new Meteor[10];
ArrayList bullets;

//variables
float rectX = 300;
int rectY = 760;
int bulletY = 750;
float positionChange = 1.5;
int speed = 1;
int meteorW = 90;
int bulletChange = 3;
int meteorXMin = 50;
int meteorXMax = 550;
int spaceshipW = 80;
int bulletWidth = 15;
int bulletHeight = 15;
int bulletYStart = 730;

void setup() {
size(600, 800);

//meteors
for(int i = 0; i < meteor.length; i++) {
meteor[i] = new Meteor(random(meteorXMin, meteorXMax), random(-height), meteorW);

//bullets
bullets = new ArrayList();
bullets.add(new Bullet(width/2, 0, bulletWidth, bulletHeight));
}
}

void draw() {
background(0);

//meteor objects
for(int i = 0; i < meteor.length; i++) {
meteor[i].drawTheMeteors();
meteor[i].moveTheMeteors();
meteor[i].wrapTheMeteors();
}

//load the spaceship
rectMode(CENTER);
rect(rectX, rectY, 100, 50);
if(key == CODED) {
if(keyCode == LEFT) {
rectX = rectX - positionChange;
}
if(keyCode == RIGHT) {
rectX = rectX + positionChange;
}
}

//spaceship wrapping
if(rectX + 50 < 0) {
rectX = width + spaceshipW / 2;
}
if(rectX - 50 > width) {
rectX = -spaceshipW / 2;
}

//bullet objects
for (int i = bullets.size()-1; i >= 0; i–) {
Bullet bullet = bullets.get(i);
bullet.moveTheBullets();
bullet.displayTheBullets();
}

//collision detection
//if(dist(meteor.x, meteor.y, bullets.x, bullets.y) < 100) {
// meteor.x = 1000;
//}
}

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;

public Bullet(float x, float y, float w, float h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
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 d;
float speed = 1;

public Meteor(float x, float y, float d) {
this.x = x;
this.y = y;
this.d = d;
}

void drawTheMeteors() {
ellipse(x, y, d, d);
}

void moveTheMeteors() {
y = y + speed;
}

void wrapTheMeteors() {
if(y - 50 > height) {
y = -50;
}
}
}