# Creating collision

**URL:** <https://discourse.processing.org/t/creating-collision/23029>\
**Category:** Coding Questions\
**Created:** [August 4, 2020, 9:27am UTC](https://discourse.processing.org/t/creating-collision/23029 "2020-08-04T09:27:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeidaraLP](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@DeidaraLP](https://discourse.processing.org/u/DeidaraLP)\
**Post date:** [August 4, 2020, 9:27am UTC](https://discourse.processing.org/t/creating-collision/23029/1 "2020-08-04T09:27:18Z")

</div>

Hello /sub

I’ve been trying to create a simple game based on different classes. However, i’m currently at a dead end, since i’m trying to create a collision detector between the player class and the asteroid class.

However, do i have to detect the collision outside the classes, in either setup or draw for it to work? I wanted to implement the collision inside one of the classes.

How would i create this collision dectection? I wanna know, if one of the asteroid rectangles overlap the player rectangle.

You can check the source code and run the application on: [https://www.openprocessing.org/sketch/941548](https://www.openprocessing.org/sketch/941548)

---

<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:** [August 4, 2020, 11:46am UTC](https://discourse.processing.org/t/creating-collision/23029/2 "2020-08-04T11:46:27Z")

</div>

I would do the for loop outside the classes in draw(). You for loop over all asteroids here.

But you can have a function in the asteroid class isColliding to check its distance to the player and return a boolean variable for yes/ no.

The collision detection itself: use if(dist(…)\< 30) …

See reference for dist

---

<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:** [August 4, 2020, 3:58pm UTC](https://discourse.processing.org/t/creating-collision/23029/3 "2020-08-04T15:58:19Z")

</div>

> [@DeidaraLP](#):
>
> , do I have to detect the collision outside the classes,

As @Chrisir mentioned, it’s best to place the loop outside the involved classes.

> [@DeidaraLP](#):
>
> I wanted to implement the collision inside one of the classes.

The collision detection part can be turned into a method inside 1 or both classes.

For example, the sketch from the link below got a method named **gotStick()** inside class Beaver w/ a Stick parameter, which checks whether whether the current Beaver is colliding w/ the passed Stick:

[Studio.ProcessingTogether.com/sp/pad/export/ro.9bTfM49wCIJza](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9bTfM49wCIJza)

```auto
boolean gotStick(Stick s) {
  return x+SIZE > s.x & x < s.x+Stick.W & y+SIZE > s.y & y < s.y+Stick.H;
}

```

Extra info about rect-to-rect collision algorithm:

> **[Collision Detection](http://jeffreythompson.org/collision-detection/rect-rect.php)**
>
> An online book about collision detection using Processing.

---

<div class="post-metadata">

**Author:** ![DeidaraLP](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@DeidaraLP](https://discourse.processing.org/u/DeidaraLP)\
**Post date:** [August 4, 2020, 4:08pm UTC](https://discourse.processing.org/t/creating-collision/23029/4 "2020-08-04T16:08:35Z")

</div>

Great! The gotStick example made great sense to me. And that was excatly what i was looking for. Thanks alot 🙂

Now i can just call the method inside the for loop checkCollision(m1[i])

---

<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:** [August 4, 2020, 4:13pm UTC](https://discourse.processing.org/t/creating-collision/23029/5 "2020-08-04T16:13:36Z")

</div>

> **[r/processing - Creating collision](https://www.reddit.com/r/processing/comments/i3g3w6/creating_collision/)**
>
> 2 votes and 7 comments so far on Reddit
