# How do I add collision between player projectile and enemies?

**URL:** https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238
**Category:** Coding Questions
**Created:** [October 13, 2022, 5:13am UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238 "2022-10-13T05:13:04Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [October 13, 2022, 8:00am UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/2 "2022-10-13T08:00:44Z")

</div>

Hi @Marfmamow,

> [@Marfmamow](#):
>
> I’ve tried following most of the rectangle collision guides, but I can’t seem to figure out how to apply it to my game because there will be multiple enemies and projectiles which are changing in position every frame

At each frame, for every object that can collide you need to check if it collides with any of all other objects (except itself).

This depends on what you want:

- Do projectiles collide with other projectiles? Same for enemies?
- Do projectiles kill enemies?
- Do you have multiple players?
- Is your player hitbox the whole tile or the size of the sprite?

Also you might want to use Java’s object oriented features, specially abstract classes. [Abstract classes](https://en.wikipedia.org/wiki/Abstract_type) define a “template” for classes that you need to implement later.

For example you might have a `Collidable` abstract class that is defined like so:

```auto
abstract class Collidable {
  int x, y, w, h;

  Collidable(int x, int y, int w, int h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
  }

  boolean isCollidingWith(Collidable other) {
    // Rectangle / Rectangle collision
    return x + w > other.x 
      && y + h > other.y 
      && x < other.x + other.w
      && y < other.y + other.h;
  }
}

class Player extends Collidable {
  Player(int x, int y, int w, int h) {
    super(x, y, w, h);
  }
}

class Enemy extends Collidable {
  Enemy(int x, int y, int w, int h) {
    super(x, y, w, h);
  }
}

```

Each collidable object has coordinates and size (assuming they are all rectangular shaped) and has a default implementation of rectangle collision. Then you can define a `Player` and `Enemy` class **extending** the collidable class so that they get the collision behavior without re-implementing it.

You can then test for collision with every object that is `Collidable` (thanks to [polymorphism](https://en.wikipedia.org/wiki/Polymorphism_(computer_science))):

```java
Player player = new Player(0, 0, 50, 50);
Enemy enemy = new Enemy(30, 30, 20, 20);

println(player.isCollidingWith(enemy));

// => true

```

**Bonus** : here is an example on how you can implement collision for objects with different shapes

> <https://stackoverflow.com/questions/6163457/design-pattern-for-checking-collision-between-shapes>

Linked to your previous post:

> [@How do I add sprite collision?](https://discourse.processing.org/t/how-do-i-add-sprite-collision/39115):
>
> I’m currently trying to make a 2d game. The game window is 720x660 pixels, and each sprite I have is 20x20 pixels, so the actual grid is 36x33 tiles. The type of tile on the grid is stored in a 36x33 array. I currently have these classes: Player Walls and I’m trying to add these classes: Bullet Enemy Currently my walls are stored in an arraylist, and I loop through them, adding 20 to their x and y position every time and drawing it For collision, I currently have a variable for grid coord…

---

_[View the full topic](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238)._
