# Animating an object from one position to other

**URL:** https://discourse.processing.org/t/animating-an-object-from-one-position-to-other/7995
**Category:** Coding Questions
**Created:** [January 31, 2019, 6:11pm UTC](https://discourse.processing.org/t/animating-an-object-from-one-position-to-other/7995 "2019-01-31T18:11:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sim](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@sim](https://discourse.processing.org/u/sim)
#### Post date: [January 31, 2019, 6:11pm UTC](https://discourse.processing.org/t/animating-an-object-from-one-position-to-other/7995/1 "2019-01-31T18:11:21Z")

</div>

Hello there,

I would really appreciated some help with this question that I’m stuck on. im trying to get an object to move from one position (last mouse click) to a new position (most recent mouse click).  
I’m familiar with functions like: “mouseClicked, pmouseX, mouseX”. so it would be helpful if anyone can use those functions to give me an example of how I can animate an object like ellipse at a certain speed from one position to another.  
Thanks in advance 🙂

---

<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: [January 31, 2019, 6:15pm UTC](https://discourse.processing.org/t/animating-an-object-from-one-position-to-other/7995/2 "2019-01-31T18:15:25Z")

</div>

Shameless self-promotion: here is a guide on animation in Processing:

> **[Animation](https://happycoding.io/tutorials/processing/animation)**
>
> Learn how to create animations in Processing.

---

<div class="post-metadata">

### Author: ![sim](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@sim](https://discourse.processing.org/u/sim)
#### Post date: [January 31, 2019, 6:19pm UTC](https://discourse.processing.org/t/animating-an-object-from-one-position-to-other/7995/3 "2019-01-31T18:19:36Z")

</div>

do you know how I can make the ball follow to where I click my mouse inside of the “gravity”. sorry im new and unfamiliar with the concept of mouseClicked.

---

<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: [January 31, 2019, 6:21pm UTC](https://discourse.processing.org/t/animating-an-object-from-one-position-to-other/7995/4 "2019-01-31T18:21:08Z")

</div>

You’d probably want to store the clicked position in a set of variables, and use those variables to update your position each frame.

Start with something simpler. Can you create a small example sketch that moves a circle from one hard-coded position to another hard-coded position? Get that working before trying to move it to the mouse position.

---

<div class="post-metadata">

### Author: ![sim](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@sim](https://discourse.processing.org/u/sim)
#### Post date: [January 31, 2019, 6:26pm UTC](https://discourse.processing.org/t/animating-an-object-from-one-position-to-other/7995/5 "2019-01-31T18:26:09Z")

</div>

yes I have got that working, the object appears wherever I click my mouse. but I’m unable to make it smoothly transfer there at a constant speed.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 4, 2019, 10:17pm UTC](https://discourse.processing.org/t/animating-an-object-from-one-position-to-other/7995/6 "2019-02-04T22:17:57Z")

</div>

> [@sim](#):
>
> but I’m unable to make it smoothly transfer there at a constant speed.

Were you able to resolve this issue?

Normally, for a smooth transition, I would use `PVector.lerp()`.

> **[lerp() / Reference](https://processing.org/reference/PVector_lerp_.html)**
>
> Calculates linear interpolation from one vector to another vector. (Just like regular lerp(), but for vectors.) Note that there is one static version of this method, and two…

But you said “at a constant speed” – so I would just use a PVector and subtract a standard amount from the distance to the target, so long as target distance is farther away than the speed.

> **[sub() / Reference](https://processing.org/reference/PVector_sub_.html)**
>
> Subtracts x, y, and z components from a vector, subtracts one vector from another, or subtracts two independent vectors. The version of the method that substracts two vectors is a static method and re…

> **[dist() / Reference](https://processing.org/reference/PVector_dist_.html)**
>
> Calculates the Euclidean distance between two points (considering a point as a vector object).

However, there are many was to solve this problem!
