Collision Detection with Bullet

Hi everyone, I’m unsure on how to code collision detection in processing. I get the overall idea of how it works and have watched tutorials on YouTube however they mainly show collision detection in terms of changing colour rather than making the object disappear once it hit. This is my code so far.

int x = 30;
int y = 60;
float gunX = x+20;
float gunY = y+20;
float circleY;
float circleX;
boolean bulletFlies=false;
float bulletX = 20;
float bulletY = 20;

void setup() {
  size(500, 500);
   circleY = random(25,480);
   circleX = 450;
}

void draw() {
  background(0);
  fill(255, 56, 99);
  ellipse(x, y, 60, 60);
  gunX = x+12;
  gunY = y;
  bullet();
  
   fill(255);
circle(circleX,circleY, 80);
circleX=circleX-0.5;

}

void keyPressed() {
  if (key == 's') y = y + 8;
  else if (key == 'w') y = y - 8;
  else if (key == ' ') {
    if(bulletFlies)
    return;
    bulletFlies=true;
    bulletX = gunX+14;
    bulletY = gunY+4;
  }
}

void bullet() {
  if (bulletFlies) {
    ellipse(bulletX, bulletY, 6, 6);
    bulletX = bulletX+5; 
    if (bulletX>width+12) { 
      bulletFlies=false;
         }else if (key == ' ' && !bulletFlies){
    }
  }
}

basically just measure the distance between the bullet and the target if it is less than the target radius (simply because the bullet doesn’t really have one but could) then “reset” the bullet. there is some other stuff here you could change such as having gunX and x & gunY and y which are basically the same thing. also seperating this into classes would be a good idea. maybe a gun class a particle class for the bullets and a target class. anyways best of luck.

int x = 30;
int y = 60;
float gunX = x+20;
float gunY = y+20;
float circleY;
float circleX;
float circleRadius;
float circleDiameter;
boolean bulletFlies=false;
float bulletX;
float bulletY;

void setup() {
  size(500, 500);
  circleY = random(25, 480);
  circleX = 450;
  circleRadius = 80;
  circleDiameter = circleRadius * 2;
}

void draw() {
  background(0);
  fill(255, 56, 99);
  ellipse(x, y, 60, 60);
  ellipse(bulletX, bulletY, 6, 6);
  gunY = y;

  fill(255);
  circle(circleX, circleY, circleDiameter);
  circleX=circleX-0.5;
  
  bullet();
}

void keyPressed() {
  if (key == 's') y = y + 8;
  else if (key == 'w') y = y - 8;
  else if (key == ' ') {
    if (!bulletFlies) {
      bulletFlies=true;
      bulletX = gunX+14;
      bulletY = gunY+4;
    }
  }
}

void bullet() {
  if (bulletFlies) {
    //check the bullet hasn't left the screen
    if (bulletX > width) {
      bulletFlies = false;
      return;
    }
    bulletX = bulletX+5;
    //calculate the distance between the bullet and the circle
    float dx = bulletX - circleX;
    float dy = bulletY - circleY;
    float dist = sqrt(dx * dx + dy * dy);
    if (dist < circleRadius) {
      bulletFlies = false;
    }
  }
}

Ah okay I see, how would I add the line of code to void the ellipse coming from the right side to the left when the bullet hits?

you can ignore the particles i just added them cause i was bored :slight_smile:

int x = 30;
int y = 60;
float gunX = x+20;
float gunY = y+20;
float circleY;
float circleX;
float circleRadius;
float circleDiameter;
boolean bulletFlies=false;
float bulletX;
float bulletY;

ArrayList<Particle> particles;

void setup() {
  size(500, 500);
  noStroke();
  
  circleY = random(25, 480);
  circleX = 450;
  circleRadius = 80;
  circleDiameter = circleRadius * 2;

  particles = new ArrayList<Particle>();
}

void draw() {
  background(0);
  fill(255, 56, 99);
  ellipse(x, y, 60, 60);
  if(bulletFlies)
    ellipse(bulletX, bulletY, 6, 6);
  gunY = y;

  fill(255);
  circle(circleX, circleY, circleDiameter);
  circleX=circleX-0.5;

  bullet();

  for (int i = particles.size() - 1; i >= 0; i--) {
    Particle p = particles.get(i);
    p.update();
    p.present();
    if (p.ttl <= 0) {
      particles.remove(i);
    }
  }
}

void keyPressed() {
  if (key == 's') y = y + 8;
  else if (key == 'w') y = y - 8;
  else if (key == ' ') {
    if (!bulletFlies) {
      bulletFlies=true;
      bulletX = gunX+14;
      bulletY = gunY+4;
    }
  }
}

void bullet() {
  if (bulletFlies) {
    //check the bullet hasn't left the screen
    if (bulletX > width) {
      bulletFlies = false;
      return;
    }
    bulletX = bulletX+5;
    //calculate the distance between the bullet and the circle
    float dx = bulletX - circleX;
    float dy = bulletY - circleY;
    float dist = sqrt(dx * dx + dy * dy);
    if (dist < circleRadius) {
      //the target has been hit so lets explode it and reset its position
      explode();
      //reset the bullet as well
      bulletX = gunX+14;
      bulletY = gunY+4;
      bulletFlies = false;
    }
  }
}

void explode() {
  int amt = int(random(10, 50));
  for (int i = 0; i < amt; i++) {
    Particle p = new Particle(circleX, circleY, 100);
    p.addForce(random(-5, 5), random(-5, 5));
    particles.add(p);
  }
  //reset the circle so it appears there is a new one
  circleY = random(25, 480);
  circleX = width + circleRadius;
}

class Particle {
  PVector pos, vel, acc;
  int ttl;

  Particle(float x, float y, int ttl) {
    this.pos = new PVector(x, y);
    this.vel = new PVector();
    this.acc = new PVector();
    this.ttl = ttl;
  }

  void addForce(float x, float y) {
    this.acc.x += x;
    this.acc.y += y;
  }

  void update() {
    this.vel.add(this.acc);
    this.acc.mult(0);
    this.pos.add(this.vel);
    this.ttl--;
  }

  void present() {
    fill(color(255, 255, 255, (ttl / 100.0) * 255));
   // fill(255);
   ellipse(this.pos.x, this.pos.y, 10, 10);
  }
}

this is the pertinent piece of code for determining when to reset the target

float dx = bulletX - circleX;
    float dy = bulletY - circleY;
    float dist = sqrt(dx * dx + dy * dy);
    if (dist < circleRadius) {
      //the target has been hit so lets explode it and reset its position
      explode();
      //reset the bullet as well
      bulletX = gunX+14;
      bulletY = gunY+4;
      bulletFlies = false;
    }

edit: you really want to start packaging things into classes at this point. it will make things so much easier for yourself. also if you are looking for some good beginner tutorials you can’t go wrong with these by codeincomplete

1 Like

Okay, thank you ! :slight_smile: