# 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:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marfmamow](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Marfmamow](https://discourse.processing.org/u/Marfmamow)\
**Post date:** [October 13, 2022, 5:13am UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/1 "2022-10-13T05:13:04Z")

</div>

I’m making a 2d tile based game, and I have implemented collision between the player and wall tiles.  
I did this by placing each tile in an array, and then giving the player’s position an index in the array

However, this type of collision only works between one moving and one stationary tile.  
I’m now trying to implement enemies and projectiles, which means my current collision won’t work because they should be able to collide when travelling between tiles.

How can I create collision between moving players, moving projectiles, and moving enemies?  
They’re all 20x20 sprites

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

---

<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…

---

<div class="post-metadata">

**Author:** ![Marfmamow](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Marfmamow](https://discourse.processing.org/u/Marfmamow)\
**Post date:** [October 13, 2022, 1:06pm UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/3 "2022-10-13T13:06:22Z")

</div>

Thanks! I want the player to be able to collide with the enemies and the enemies projectile. The player and enemy projectiles should also be able to collide

The player projectiles are stored in an array list. The enemies are also stored in an arraylist, and their projectiles will be in their own array list as well.

Would I have to loop through each of these array lists every draw() and check for collision?

---

<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, 1:10pm UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/4 "2022-10-13T13:10:31Z")

</div>

> [@Marfmamow](#):
>
> Would I have to loop through each of these array lists every draw() and check for collision?

Yes it’s probably the most straight forward solution. The logic can be extracted into its own system as stated here:

> <https://gamedev.stackexchange.com/questions/13004/where-should-collision-detection-logic-be-placed>

---

<div class="post-metadata">

**Author:** ![Marfmamow](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Marfmamow](https://discourse.processing.org/u/Marfmamow)\
**Post date:** [October 14, 2022, 11:58am UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/5 "2022-10-14T11:58:50Z")

</div>

I’m currently working on changing my wall collision to this type of collision.  
I’ve managed to get the player to recognise that its collided with the wall, but how do I make it stop when it touches the wall? Right now the player still goes into the wall, but knows its collided with a wall.

I’m controlling the player using arrow keys

---

<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 14, 2022, 3:36pm UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/6 "2022-10-14T15:36:33Z")

</div>

> [@Marfmamow](#):
>
> I’ve managed to get the player to recognise that its collided with the wall, but how do I make it stop

If it’s a tile based game, you can try to move temporarily the player then if it collides with something you don’t move it.

Or you before you move you can check if the corresponding tile (up, down, left, right) has a wall and if yes you don’t move.

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [October 14, 2022, 4:36pm UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/7 "2022-10-14T16:36:29Z")

</div>

Maybe something like this: `if (!collided()) move();`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 14, 2022, 10:42pm UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/8 "2022-10-14T22:42:30Z")

</div>

> [@Marfmamow](#):
>
> managed to get the player to recognise that its collided with the wall, but how do I make it stop when it touches the wall?

Good comments already

Or set a marker like hasCollided=true;

And then evaluate it so that it cannot move when it’s true

---

<div class="post-metadata">

**Author:** ![Marfmamow](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@Marfmamow](https://discourse.processing.org/u/Marfmamow)\
**Post date:** [October 17, 2022, 3:19pm UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/9 "2022-10-17T15:19:55Z")

</div>

I’ve managed to get two moving objects to detect whether they’ve touched or not, but currently it only works when the center of the sprites touch.

I tried adding the sprite width and height into the collision check, but it still doesn’t work.  
This is what I have:

```auto
boolean collidingWith(Collidable other){
        return x + w + movementSpeed> other.x &&
               x - movementSpeed< other.x + other.w &&
               y + h + movementSpeed> other.y &&
               y - movementSpeed < other.y + other.h;
    }

```

It also doesn’t work very consistently when both objects are moving

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 17, 2022, 4:14pm UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/10 "2022-10-17T16:14:53Z")

</div>

You can also use the dist() command

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [October 17, 2022, 4:21pm UTC](https://discourse.processing.org/t/how-do-i-add-collision-between-player-projectile-and-enemies/39238/11 "2022-10-17T16:21:08Z")

</div>

Hello!  
An EXCELLENT tutorial here on the Happy Coding site:

> **[Processing Collision Detection](https://happycoding.io/tutorials/processing/collision-detection)**
>
> Learn how to trigger an action when objects overlap in Processing!

🤓
