# 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:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 9, 2021, 6:45pm UTC](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929/1 "2021-01-09T18:45:39Z")

</div>

Hi everyone! I need some help with understanding, what can I use to make an image object move in the specified direction(vectors maybe…I am not that sure).  
I have an object that is represented by png image and I want it to move for a speified distance and in a direction, that would be defined by click. If I click on the left near the image it should move right, if I click on the top near the image it should go down and vice versa. For diagonals it should also work the same way e.g.left upper corner click sends image to right down corner.  
I’ve tried to find a tutorial or an example on [https://processing.org/](https://processing.org/) , but didn’t really find something like this. If you’ve seen such a tutorial I would be grateful if you share a link. Or maybe you can write a hint, how to do it. Thanks!!!

---

<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)

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 9, 2021, 7:53pm UTC](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929/3 "2021-01-09T19:53:14Z")

</div>

Oh, the thing with xadd = -1 didn’t come to my mind. Thanks a lot! I’ m going to try it now!

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 9, 2021, 8:27pm UTC](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929/4 "2021-01-09T20:27:08Z")

</div>

@Chrisir one more question. Is it possible to save mouseX and mouseY value when mousePressed(), so I can use them a little later?

---

<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, 8:39pm UTC](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929/5 "2021-01-09T20:39:16Z")

</div>

sure just say

**in mousePressed()**

```auto
mouseXStored = mouseX; 
mouseYStored = mouseY;

```

(and set a timer?)

**and of course before setup()**

`int mouseXStored, mouseYStored;`

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 11, 2021, 2:18pm UTC](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929/6 "2021-01-11T14:18:07Z")

</div>

Thank you so much! Your answers hepled me a lot!

---

<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 11, 2021, 2:34pm UTC](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929/7 "2021-01-11T14:34:49Z")

</div>

When you view the position where the mouse has been clicked as a TARGET (and not only as the general direction, like a form of cursor keys), consider the following:

you can calc the difference between target and current position and add that to the current position, thus player moves towards mouse position. You can make an easing, so it moves **slowly** towards it

cf. [Slow Movement of player to mouse position on mouse click - #2 by Chrisir](https://discourse.processing.org/t/slow-movement-of-player-to-mouse-position-on-mouse-click/26844/2)

---

<div class="post-metadata">

### Author: ![Elmpt](https://avatars.discourse-cdn.com/v4/letter/e/8491ac/32.png) [@Elmpt](https://discourse.processing.org/u/Elmpt)
#### Post date: [January 11, 2021, 2:40pm UTC](https://discourse.processing.org/t/let-the-image-object-move-in-different-directions/26929/8 "2021-01-11T14:40:37Z")

</div>

I’ve already implemented) I needed the image to go in other direction from where mouse was clicked. Thanks!
