Jump to content


- - - - -

Ease In / Ease Out Algorithm


3 replies to this topic

#1 thespongebob

    New Member

  • Members
  • Pip
  • 5 posts

Posted 23 May 2008 - 03:40 AM

Hi
My math is pretty basic. I have books but not much practice in application yet.
Here is what I want to do.

I have 30 frames to work with.
I need an output value between 0.0f and 4.0f to apply a "shake" to an object within that 30 frames.
I want the shake to ease in and ease out.

Could someone point me in the right direction?

#2 Omni

    New Member

  • Members
  • Pip
  • 7 posts

Posted 23 May 2008 - 07:31 AM

why don't you simply use the good old (co)sinus?
if you combine two near frequencies
f_1 = f_0 - d
f_2 = f_0 + d
you get a beat of frequency d:

so your function could look like this: f(t) = 2.0 * ( cos( (f_0 + (Pi/30)) * t ) - cos( (f_0 - (Pi/30)) * t ) )
f_0 is your shake oscillation and should be much larger than Pi/30

Posted Image

#3 Jare

    Valued Member

  • Members
  • PipPipPip
  • 247 posts

Posted 23 May 2008 - 07:53 AM

thespongebob said:

I have 30 frames to work with.
I need an output value between 0.0f and 4.0f to apply a "shake" to an object within that 30 frames.
I want the shake to ease in and ease out.
Sounds like a case for the classic cubic S-curve, which takes an input from 0 to 1 and outputs a value from 0 to 1 that is horizontal at both 0 and 1:

s(x) = 3*x^2 - 2*x^3 (note, a^b means a to the power of b)

so if your shake value eases in from 0 at frame 0 to max value at frame A, then stays at max until frame B, then and eases out from max value at frame B to 0 at frame 30:

shake(f) =
if f < A: return s(f/A)
if f > B: return 1.0 - s((f-B)/(30-B))
return 1.0

and multiply the output by 4.0 which is your desired max value. I love that little function. Other alternatives:

s(x) = sin(x*PI/2)^2 is slightly smoother
s(x) = x - sin(x*2*PI) / (2*PI) is noticeably smoother

Try them in for example http://rechneronline...unction-graphs/ to see what they look like.

[Edit]: I'm assuming that what you want is to output a continuous value that represents the "amplitude" of the shake, and then somewhere else you generate some vibration using that amplitude (probably some kind of cos() like Omni suggests) with a frequency of your choosing.

#4 thespongebob

    New Member

  • Members
  • Pip
  • 5 posts

Posted 23 May 2008 - 06:07 PM

Thanx Jare. That is exactly what I wanted. That site is a great help to actually visualize what I am doing. I want to have a user-interface (health hud) shake/grow when a bullet hits my avatar.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users