Problem with Collision

HI! I'm very new to programming in general, and have been working on this project for quite a bit of time. As of right now, I cannot for the life of me figure out how to correctly create collision for my class EnemyShips. I've been working on this for an obscene amount of time, and have still not been able to get anything to work. If anyone can help, that would be an absolute lifesaver. This is a game that is based off of Space Invaders, and I need the collision of PlayerBullets to EnemyShips to work.

class AddedScore {
  int score;

  AddedScore(int _score) {
    score = _score;
  }
}

abstract class Bullet {
  int bulletColor;

  float x;
  float y;
  float ySpeed;

    x = spawnX;
    y = spawnY;
    ySpeed = _ySpeed;
  }

  abstract void display();

    y = y+ySpeed;
  }
}

class EnemyShip
{
  float x;
  float y;

  float velocity;

  int shipType;

  PImage ship;

  int shootTimer = floor(random(0, 30));

  EnemyShip(float spawnX, float spawnY) {
    spawnX=x;
    spawnY=y;

    velocity =3;
  }

  void display() {
    translate(x, y);
    image(ship, -50, -50);
    resetMatrix();

    for (int i=0; i<playerBullet.size(); i++) {
      PlayerBullet bullet = playerBullet.get(i);
      if (bullet.x < (x+40) && bullet.x > (x-50) && bullet.y < (y+40) && bullet.y > (x-40)) {
        playerBullet.remove(bullet);
        currentScore = currentScore + 50;
        addedScore.add(new AddedScore(50));
        enemyShip.remove(this);
      }
      else{
      }
    }

    if (y>625) {
      lives = lives -1;
      enemyShip.remove(this);
    }
  }

  void move() {
    this.x+=this.velocity;
    if (x> width*.9) {
      this.x = width*.9;
      this.velocity *=-1;
      this.y+=70;
    }
    //The following is the same as above, but is for when x is less than width*.1
    if (x< width*.1) {
      this.velocity*=-1;
      this.x = width*.1;
      this.y +=50;
    }
  }

  void shoot() {
    EnemyBullet enemyBullet = new EnemyBullet(this.x, this.y, 5);
  }
}

class PlayerBullet extends Bullet
{
  PlayerBullet(float spawnX, float spawnY, float ySpeed) {
    super(spawnX, spawnY, ySpeed);
  }

  void display() {
    fill(155, 237, 23);
    noStroke();
    rect(x,y,5,20);
    if (y>-5) {
      playerBullet.remove(this);
    }
  }
}

Please let me know if you need to see my setup() or draw(), and any suggestions or help is absolutely welcome. Thanks!

Sign In or Register to comment.