Animation: Moving an object from Point A to Point B in a Linear fashion

Hello All,

I’m trying to add some light animation to my project. I’ve been reading up on ofBook, and some prior posts and am unfortunately still a little bit stuck on how to animate the movement of an object.

Say I have a sphere at location (200, 200) whose location is being stored by ofPoint. How would I move that sphere slowly to (384, 100)?

Any tips would be greatly appreciated, thanks!

To achieve an interpolation you need to do some math in relation to time. ofGetElapsedTimef() gives you the “current app time” in floating-point seconds.

When you start the animation, you save ofGetElapsedTimef() into a variable defined in your class header, for example float animation_start_time. From that point, by subtracting animation_start_time from ofGetElapsedTimef(), you know how long the animation has been going.

You can them map this progressing value to your desired duration and range. So for a 10 second animation you’d do in your update() method:

auto x_pos = ofMap(ofGetElapsedTimef()-animation_start_time ,0, 10, 200, 384, true).

While it’s interesting to work low-level like that, it gets tedious as soon as you have more a few params, even more so if the interpolations timelines are asynchronous, or if you want to update or cancel them. There are many addons tackling these problems with different approaches, and it’s probably productive to invest the time to learn to use some. I generally reach for one of these two:

You will find more here:

i recently found tweeny. a very nice modern c++ lib. it might be helpful for you as well.

Works!!! Thank you sou much!

Thanks, will take a look!