# Collision between two ArrayList elements

**URL:** <https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932>\
**Category:** Coding Questions\
**Created:** [November 25, 2018, 6:01pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932 "2018-11-25T18:01:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rangerz132](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@Rangerz132](https://discourse.processing.org/u/Rangerz132)\
**Post date:** [November 25, 2018, 6:01pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/1 "2018-11-25T18:01:10Z")

</div>

Hi everyone!

I’m currently at University in Multimedia and I’m learning Processing (which is pretty cool).

For my lastest project, I need to do a game where there is some collisions. The problem is that I know how to do some hits test object (collisions) between some elements in my own class, but the collisions need to be between two different objects.

Actually, I have an ArrayList where there are Enemies (they spawn according to some rules I made) and I have an other ArrayList where there are Bullets (need to kill the enemy). So I don’t really know how to detect a collision between two different ArrayList elements.

By the way, my game is in 3D. I don’t think it will make a difference because i just need to put an other value to the dist(), but I wanted to say it anyway hehe!.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 25, 2018, 6:38pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/2 "2018-11-25T18:38:37Z")

</div>

[Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o)  
[Studio.ProcessingTogether.com/sp/pad/export/ro.9K-kjhuONcJDZ](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9K-kjhuONcJDZ)

---

<div class="post-metadata">

**Author:** ![Rangerz132](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@Rangerz132](https://discourse.processing.org/u/Rangerz132)\
**Post date:** [November 25, 2018, 8:48pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/3 "2018-11-25T20:48:40Z")

</div>

Thanks a lot!

But is there a way to do with instance or extends class?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 25, 2018, 9:10pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/4 "2018-11-25T21:10:12Z")

</div>

You can make a base class w/ methods & fields that should be shared for all subclasses. 💡

---

<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:** [November 25, 2018, 10:23pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/5 "2018-11-25T22:23:44Z")

</div>

Do you want a collision direction – like a 3D bouncing ball? Or is this just hitbix detection?

---

<div class="post-metadata">

**Author:** ![Rangerz132](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@Rangerz132](https://discourse.processing.org/u/Rangerz132)\
**Post date:** [November 25, 2018, 10:41pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/6 "2018-11-25T22:41:06Z")

</div>

Well, I simply need a collision detection.

Like… There are some enemies flying (move in many directions) and when the user MousePressed(), there is a bullet that move in the Z axes (the user need to aim and kill the enemy).

---

<div class="post-metadata">

**Author:** ![Rangerz132](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@Rangerz132](https://discourse.processing.org/u/Rangerz132)\
**Post date:** [November 25, 2018, 10:43pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/7 "2018-11-25T22:43:11Z")

</div>

It would be simplier if Java could support a superclass that can inherit many classes

---

<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:** [November 25, 2018, 10:52pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/8 "2018-11-25T22:52:59Z")

</div>

Use an interface for that.

---

<div class="post-metadata">

**Author:** ![Rangerz132](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@Rangerz132](https://discourse.processing.org/u/Rangerz132)\
**Post date:** [November 26, 2018, 6:29pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/9 "2018-11-26T18:29:31Z")

</div>

thanks a lot guys! I read the documentation and I didn’t know there was such thing as an **Interface** with **implements**. Great community 😃

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [November 26, 2018, 6:35pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/10 "2018-11-26T18:35:26Z")

</div>

[Docs.Oracle.com/javase/tutorial/java/concepts/index.html](http://Docs.Oracle.com/javase/tutorial/java/concepts/index.html)

---

<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:** [November 26, 2018, 7:27pm UTC](https://discourse.processing.org/t/collision-between-two-arraylist-elements/5932/11 "2018-11-26T19:27:52Z")

</div>

See also a recent related discussion for examples of interface, and other approaches:

> [@Looping different class instances](https://discourse.processing.org/t/looping-different-class-instances/5843/8):
>
> Abstract classes and reflection are fine solutions given your constraint (no interface!) but I’m a bit confused about why this is an issue. If you don’t want to map a large method list up from Helper to A / B / C, then don’t. You can still use an interface. Just create an interface Helpful with only one method – Helper getHelper(), which returns the Helper object of that A/B/C object. Your classes A B C each implement that one method which makes them Helpful (class A implements Helpful … clas…
