Player Health & Collision not working properly together

For some reason, in the game that I am attempting to make for a class project whenever the player hits an asteroid at speed, the playerHealth variable goes down by multiple numbers instead of by one. The below are functions from my Asteroid class.

void collision() {
    if (asteroidX <= 0) {
    //if (!invincible) {  //unimplemented invincibility frames
      for (int i = 0; i < asteroids.length; i++) {
        if (dist(asteroids[i].asteroidX, asteroids[i].asteroidY, player.playerX, player.playerY) < asteroids[i].asteroidSize + player.playerSize) {        
          playerHealth -= 1;
       // }

  void respawn() {
    asteroidX = width*1.3;  //spawn beyond screen
    asteroidY = random(height);
    asteroidSize = random(10, 25);
    asteroidSpeed= random(2, 5);

Does anyone know why exactly this is happening? I have a vague feeling but I’m too new at this to really understand what exactly is to be fixed. I’m down for posting my full code if that’s necessary.

respawn works on some variables but it doesn’t work on asteroids[i].asteroidX, asteroids[i].asteroidY


Thank you for pointing me in the right direction! I changed the contents of the for() loop in my collisions and it all works as intended now.

What I meant was:

This function:

This should look like this:

  void respawn( int i ) {
    asteroids[i].asteroidX = width*1.3;  //spawn beyond screen
    asteroids[i].asteroidY = random(height);
    asteroids[i].asteroidSize = random(10, 25);
    asteroids[i].asteroidSpeed= random(2, 5);

THEN the asteroid that collided will get a new position and can’t count for the health again


These variables from respawn() like asteroidX asteroidY etc. shouldn’t really exist…

you got all the information in the class and in the array asteroids[]

Hey, and welcome to the forum!

Great to have you here!