Sign in to follow this  
stromchin

Soft movement between 2 points?

Recommended Posts

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

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


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
instead of
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 this post


Link to post
Share on other sites
My go-tos for ease-in/ease-out smooth interpolations are the [url="http://en.wikipedia.org/wiki/Smoothstep"]smoothstep[/url] function and the [url="http://en.wikipedia.org/wiki/Cubic_hermite_spline"]hermite spline[/url].

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1312785613' post='4846079']
My go-tos for ease-in/ease-out smooth interpolations are the [url="http://en.wikipedia.org/wiki/Smoothstep"]smoothstep[/url] function and the [url="http://en.wikipedia.org/wiki/Cubic_hermite_spline"]hermite spline[/url].
[/quote]

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

Share this post


Link to post
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:

[code]a * (1 - x) + b * x[/code]

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

[code]x = frame / max[/code]

However, if you change it to

[code]x = (frame * frame) / (max * max)[/code]

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 this post


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

[code]
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;
}
}[/code]


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this