I need help with collision detection in clone game

Hello @PaddedMellie ,

Your bullets and ship will never be exactly at the same position!

Use println() statements to see what is going on.
Add a visual queue such as background() to flash when there is a collision.

This code is only comparing along the y-axis when they are within 30 pixels and flashes (code works here):

  void bulletCollision(){
   //println(x, y);
   //println(shipX, shipY);
   println(p.y, this.y);
   println(shipY);
   println();
   
   //if(x == shipX && y == shipY) // May never see this condition!
   {
   if(abs(y - shipY) <30) // If within 30!
     {
     println("COLLISION");
     background(255);
     }
   }
  }

Above is only intended to help get you started with troubleshooting tips.
It can be easily modified for x position.

Note:

I reduced the spaceships to one for testing:
Spaceships[] ships = new Spaceships[1];

It works (detects collision) with one spaceship or first spaceship if you have more than one.

Also look here for guidance:
Guidelines—Asking Questions

Keep at it!

:)

1 Like