# Collision Detection with Bullet

**URL:** <https://discourse.processing.org/t/collision-detection-with-bullet/32557>\
**Category:** Coding Questions\
**Created:** [October 2, 2021, 6:17am UTC](https://discourse.processing.org/t/collision-detection-with-bullet/32557 "2021-10-02T06:17:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarkShaker](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@MarkShaker](https://discourse.processing.org/u/MarkShaker)\
**Post date:** [October 2, 2021, 6:17am UTC](https://discourse.processing.org/t/collision-detection-with-bullet/32557/1 "2021-10-02T06:17:07Z")

</div>

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.

```auto
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){
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [October 2, 2021, 6:41am UTC](https://discourse.processing.org/t/collision-detection-with-bullet/32557/2 "2021-10-02T06:41:35Z")

</div>

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.

```auto
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;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![MarkShaker](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@MarkShaker](https://discourse.processing.org/u/MarkShaker)\
**Post date:** [October 3, 2021, 4:26am UTC](https://discourse.processing.org/t/collision-detection-with-bullet/32557/3 "2021-10-03T04:26:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [October 3, 2021, 6:18am UTC](https://discourse.processing.org/t/collision-detection-with-bullet/32557/4 "2021-10-03T06:18:37Z")

</div>

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

```auto
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

```auto
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](https://codeincomplete.com/games/)

---

<div class="post-metadata">

**Author:** ![MarkShaker](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@MarkShaker](https://discourse.processing.org/u/MarkShaker)\
**Post date:** [October 4, 2021, 7:32am UTC](https://discourse.processing.org/t/collision-detection-with-bullet/32557/5 "2021-10-04T07:32:40Z")

</div>

Okay, thank you ! 🙂

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [October 10, 2021, 9:00pm UTC](https://discourse.processing.org/t/collision-detection-with-bullet/32557/6 "2021-10-10T21:00:05Z")

</div>


