# Let the image object move in different directions

**URL:** <https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929>\
**Category:** Coding Questions\
**Created:** [January 9, 2021, 6:45pm UTC](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929 "2021-01-09T18:45:39Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [January 9, 2021, 7:43pm UTC](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929/2 "2021-01-09T19:43:12Z")

</div>

Let’s say your object is at x,y.

You move it by

```auto

x+=xadd; 
y+=yadd;

```

xadd and yadd can be positive or negative.

Now, use the function mousePressed() and compare the mouse pos mouseX, mouseY  
with x,y.  
When

- `mouseX < x say xadd = -1;`
- `mouseX > x say xadd = 1;`
- `mouseY < y say yadd = -1;`
- `mouseY > y say yadd = 1;`

So you can get a vertical, diagonal and horizontal movement.

For movement in any direction see atan2 - [Understanding the Atan2, Please help - #6 by CodeMasterX](https://discourse.processing.org/t/understanding-the-atan2-please-help/26928/6)

---

_[View the full topic](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929)._
