How to move a object, not in draw()

Hi folks, how to move a object, not in draw()?
I make a function ship(), how can I make the ship() move from one point to another point slowly ? Is there any function relate to time, for example ship() can move from point (0, 0) to point(100, 100) druing 5 seconds ?
Thanks.

1 Like

Welcome to the forum.

Going to have to be more specific, thread() can be used to run things outside draw, but its an asynchronous function.

Hello!

Welcome to the forum!

A sketch like this https://www.processing.org/examples/linear.html can run in draw() but also in ship(), when ship() is called from draw().

When you add only small numbers, the ship moves slowly.


Movement with timer

To have a precise movement during 5 seconds, you have to put some thoughts in.

You could set a timer int timerStart=millis();

Then the formula millis()-timerStart gives you the time since the ship started:

timeSinceStart=millis()-timerStart;

Then you can calculate the amt value for lerp:

amt = map(timeSinceStart, 0, 5000, 0,1); // amt is between 0 and 1

Don’t forget to set amt to 1 once 1 is reached (otherwise it runs on I think and goes beyond 100,100).

then say

 shipX=lerp(0,100,amt); 
 shipY=lerp(0,100,amt);

to get the ship position between 0 and 100 for both x and y.

Regards, Chrisir

1 Like

Hi sir. very appreciate for your reply, i have never try thread(), it looks a advanced topic to me, but i wll try.

Hi sir, thank you for your so detail help, i will try your idea in my project, thanks for your time.

1 Like