# Collision of the player with the moving rectangles

**URL:** <https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [May 21, 2020, 9:39am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124 "2020-05-21T09:39:22Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 9:39am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/1 "2020-05-21T09:39:22Z")

</div>

[https://editor.p5js.org/Nis\_Collect/sketches/U0p3GjOK1](https://editor.p5js.org/Nis_Collect/sketches/U0p3GjOK1)

Given above is the link of my project. Someone please help me by reviewing my code and telling me that how can I confirm the collision of the player with the moving rectangles.  
Please provide me the code of only the lines which specifies the collision of the player and the moving blocks.  
I tried so many ways but I can’t figure it out.  
Please please help me.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 21, 2020, 9:50am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/2 "2020-05-21T09:50:07Z")

</div>

Is that link to the right code, because I cannot see anything about collisions or even red rectangles?

---

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 10:04am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/3 "2020-05-21T10:04:44Z")

</div>

Yeah it’s correct link. But I am sorry that the moving rectangles have white colour.  
And ofcourse there cannot be any line of code of collision because that’s what I am asking

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 21, 2020, 10:17am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/4 "2020-05-21T10:17:57Z")

</div>

I see. So you want to check if two rectangles or objects have collided? It called [collision detection](https://en.wikipedia.org/wiki/Collision_detection). I don’t know how accurate you want to be, but possibly easiest way is to check distance of the objects and when it’s small enough collision has happened. Distance depends on size, shape and accuracy you’re looking for.

You get distance with [`dist()`-function](https://processing.org/reference/dist_.html). I would use center points of the objects to get distances. Naturally you need to take that to account when you draw those objects, so that you set them in place with the same center point.

---

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 10:33am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/5 "2020-05-21T10:33:03Z")

</div>

Ya you are right but I already have tried it. And unfortunately it doesn’t work there .  
Can, you please try this in my code and if you get that right, please send me the link of that code

---

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 10:34am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/6 "2020-05-21T10:34:42Z")

</div>

Please sir, please , try…

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 21, 2020, 11:46am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/7 "2020-05-21T11:46:48Z")

</div>

> [@Nis](#):
>
> Please provide me the code of only the lines which specifies the collision of the player and the moving blocks

There are plenty of examples out there for you that you can learn from.

Here is one that you may glean some insight from:

> **[examples | p5.js](https://p5js.org/examples/input-rollover.html)**
>
> p5.js a JS client-side library for creating graphic and interactive experiences, based on the core principles of Processing.

`:)`

---

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 12:06pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/8 "2020-05-21T12:06:10Z")

</div>

Can you please apply it in my code and check whether it is working or not?  
If it works, then send me the link of the code please

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 21, 2020, 12:06pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/9 "2020-05-21T12:06:46Z")

</div>

No, I won’t. Idea of processing is to learn. What would you learn if I wrote the code for you? I’m happy to help you find the solution. This is a free time activity for me so it shouldn’t feel like work.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 21, 2020, 12:07pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/10 "2020-05-21T12:07:28Z")

</div>

> [@Nis](#):
>
> Can you please apply it in my code and check whether it is working or not?  
> If it works, then send me the link of the code please

The rest is up to you.  
I will not be assisting you any further.

---

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 12:38pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/11 "2020-05-21T12:38:13Z")

</div>

I am not telling this to you because of my laziness but I am telling this to you because I have already tried it, even then it shows an eroor- dist() was expecting 4 arguments but received only 0 arguments.  
I think I am not writing the dist() function at a correct place and with correctly passed parameters. That’s why I am humbly requesting to please write that function in the proper place and with correctly passed parameters/arguments.  
Please please please tell Sir,please…please… Sir… please

---

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 12:40pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/12 "2020-05-21T12:40:11Z")

</div>

Sir, please try to understand. I am telling the same to you also. I am not telling this to you because of my laziness but I am telling this to you because I have already tried it, even then it shows an eroor- dist() was expecting 4 arguments but received on 0 arguments.  
I think I am not writing the dist() function at a correct place and with correctly passed parameters. That’s why I am humbly requesting to please write that function in the proper place and with correctly passed parameters/arguments.  
Please please please tell Sir,please…please… Sir… please

---

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 12:44pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/13 "2020-05-21T12:44:16Z")

</div>

It isn’t a free time service for me. I have been challenged and also I have a bet to develop a game in p5.js. And also, if I would be able to do so, I will get selected in my school’s CodingTeam.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 21, 2020, 12:44pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/14 "2020-05-21T12:44:25Z")

</div>

> [@Nis](#):
>
> then it shows an eroor- dist() was expecting 4 arguments but received only 0 arguments.  
> I think I am not writing the dist() function at a correct place and with correctly passed parameters.

Have you looked at the documentation of dist()-function? [dist() / Reference / Processing.org](https://processing.org/reference/dist_.html)

You need to give it x and y coordinates of two points (center points of rectangles for instance) and it will return distance between those points.

---

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 12:45pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/15 "2020-05-21T12:45:10Z")

</div>

So please sir try to understand me

---

<div class="post-metadata">

**Author:** ![Nis](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@Nis](https://discourse.processing.org/u/Nis)\
**Post date:** [May 21, 2020, 12:45pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/16 "2020-05-21T12:45:57Z")

</div>

Sir sir, even then it gives the same error

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [May 21, 2020, 2:50pm UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/17 "2020-05-21T14:50:13Z")

</div>

Did you give it four parameters? it should be something like `dist(x1,y1,x2,y2)` where x1 and y1 are coordinates of the first point and x2 and y2 are coordinates of the second point

---

<div class="post-metadata">

**Author:** ![Evlmnkey](https://avatars.discourse-cdn.com/v4/letter/e/aeb1de/32.png) [@Evlmnkey](https://discourse.processing.org/u/Evlmnkey)\
**Post date:** [May 22, 2020, 11:34am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/18 "2020-05-22T11:34:33Z")

</div>

Holy shit, @Nis

We all understand you. It sucks to not know where to start and what is wrong. Do the following thing:

1)Write a line of code that you think should give you the distance between the two rectangles. Even if it is wrong.

1. Post it here along with a description of what you think it should do, what it actually does and what you tried to fix it.

2. Help will arrive 🙂

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [May 22, 2020, 11:41am UTC](https://discourse.processing.org/t/collision-of-the-player-with-the-moving-rectangles/21124/19 "2020-05-22T11:41:47Z")

</div>

The topic continues here:

> [@Printing the y coordinate is "undefined"](https://discourse.processing.org/t/please-help-me-whith-this-simple-problem-please-please/21158):
>
> The above the picture of my code, where it isn’t printing the y coordinate of the moving rectangles, instead it is printing “undefined”

`:)`
