# Need Simple Colision Example

**URL:** <https://discourse.processing.org/t/need-simple-colision-example/5002>\
**Category:** Coding Questions\
**Created:** [October 29, 2018, 7:56pm UTC](https://discourse.processing.org/t/need-simple-colision-example/5002 "2018-10-29T19:56:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Staalone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/staalone/32/2128_2.png) [@Staalone](https://discourse.processing.org/u/Staalone)\
**Post date:** [October 29, 2018, 7:56pm UTC](https://discourse.processing.org/t/need-simple-colision-example/5002/1 "2018-10-29T19:56:34Z")

</div>

Hy, I need a code for a ball that’s controlled by the mouse that when it hits any of the corners the ball goes back to the middle, thank you!

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [October 29, 2018, 8:44pm UTC](https://discourse.processing.org/t/need-simple-colision-example/5002/2 "2018-10-29T20:44:59Z")

</div>

We are glad to help you if you have problems getting your Code to run, or don’t know how to do something, but please try it yourself first, but obviously we still can direct you to the right answer, so that you can achive it yourself and therefore learn and improve your problem-solving and Processing skills.  
To make what you what, you just need to create a circle for the ball, which can be done with ellipse(x, y, width, height). Then you can move the ball either using translate(x, y) to move it. You can also get the x and y coordinates of the mouse by using the mouseX and mouseY variables respectively. Then you just need to check if the position of the ball is outside the bounds of your sketch in this case, or a rectangle in general. This can be done by checking if the x or y position of your ball is bigger than the size of your sketch, or smaller than 0 in your case. And if it is, then set the position of the ball to where you want it to be. In your case width/2, height/2.  
Hope this helps 😊

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [October 30, 2018, 5:43pm UTC](https://discourse.processing.org/t/need-simple-colision-example/5002/3 "2018-10-30T17:43:51Z")

</div>

Shameless self-promotion: here is a tutorial on collision detection in Processing:

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

---

<div class="post-metadata">

**Author:** ![Eeyorelife](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@Eeyorelife](https://discourse.processing.org/u/Eeyorelife)\
**Post date:** [October 31, 2018, 9:31am UTC](https://discourse.processing.org/t/need-simple-colision-example/5002/4 "2018-10-31T09:31:27Z")

</div>

Like @Lexyth is saying, you have to try to do something. The reason is that it gives a good indication of your skill level. And what you want/what you want to achieve. There are many ways to solve a problem.

If I were you I would start by coding a ball, or an ellipse.  
And then try to figure out how to controll it with the mouse. And then how to do the collision detection.
