# Soft movement between 2 points?

This topic is 2391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'll go straight to the problem:
I got this function:

 template<class T> static T interpolate(T a, T b, T max, T frame) { return a + (b - a) * frame / max; }

so that I have my cursor in position a, and I want it to go to position b, in max frames. Every frame I update "frame" and thus I get the current position.
This is kind of nice, but what I wanted was something more... like... it started out fast, and then it slowed down when it was reaching the point.
Something like this:
from 0 to 10 in 10 frames:
2, 4, 6, 7, 8, 8.5, 9, 9.5, 9.75, 10
1, 2, 3, 4, 5, 6, 7, 8 ,9, 10

it's just a pointer type thing in front of the selected menu item...
I've tried to google interpolation and soft interpolation and soft camera movement and stuff but I dont know, google just doesn't work for me as well as it used to.

Any help appreciated, thanks!

##### Share on other sites
My go-tos for ease-in/ease-out smooth interpolations are the smoothstep function and the hermite spline.

##### Share on other sites

My go-tos for ease-in/ease-out smooth interpolations are the smoothstep function and the hermite spline.

thanks for the quick reply! That seems like it'll work. Thanks

##### Share on other sites
What you're doing is linear interpolation. You can try parabolic interpolation if you'd like to get it to move quickly at first and then slow down at the end, or you can reverse it and have it slowly accelerate (though it won't ease into the final position if you reverse this). It's a little different than the Smooth Step algorithm Hodgman suggests, though if you want to ease into and out of the movement, then his suggestion would work well. Here's the idea:

Interpolation from a to b works by doing:

a * (1 - x) + b * x

Where x is some number between 0 and 1 (and if you do some factoring, you'll see it's pretty much the same as what you have in your code right now). Right now, you're calculating x like this (linearly):

x = frame / max

However, if you change it to

x = (frame * frame) / (max * max)

you'll get a nice parabolic interpolation. Note that if you do it like this it'll have an accelerating motion, rather than the decelerating motion that you're desiring. The principle is the same, however, and it just takes a little bit of fiddling with the math to figure it out, which I'll leave for you to do as an exercise. Try drawing a and b on a paper, where a is at the bottom left and b is at the top right, and then draw the curve you want connecting the two, and then figure out the math needed to generate that curve.

##### Share on other sites
Thank you both.
It ended up like this (looks good enough cause I dont have that many frames anyways)

 template<class T> static T interpolate(T a, T b, T max, T frame, bool soft = false) { if(soft) { return a + (b - a) * (frame * frame) / (max * max); } else { return a + (b - a) * frame / max; } }

but thanks for giving me the pointers about smoothstep, it helped me found this site:
http://sol.gfxile.net/interpolation/
with lots of interesting smooth movements I will try in the future. Thank you!