facebookarchive / pop

An extensible iOS and OS X animation library, useful for physics-based interactions.
Other
19.66k stars 2.88k forks source link

Critically damped spring #304

Closed trive closed 8 years ago

trive commented 8 years ago

Hi,

Is there a set of parameters for bounciness and speed that create the behavior of a critically damped spring?

grp commented 8 years ago

You could calculate it out! Wikipedia has a good reference for the formulas.

Critical damping is where ζ = 1, and (in Pop terminology) ζ = friction / (2 * sqrt(mass * tension)). If you set mass = 1 then you should be able to pick your tension and friction to make it critically damped.

hfossli commented 8 years ago

Cool! So something like this then?

+ (CGFloat)frictionWhenCriticallyDampedWithMass:(CGFloat)mass tension:(CGFloat)tension
{
    // https://github.com/facebook/pop/issues/304
    // https://www.wolframalpha.com/input/?i=solve+1+%3D+f+%2F+(2+*+sqrt(t+*+m))+for+f
    return 2.0 * sqrt(mass * tension);
}

+ (CGFloat)tensionWhenCriticallyDampedWithMass:(CGFloat)mass friction:(CGFloat)friction
{
    // https://github.com/facebook/pop/issues/304
    // https://www.wolframalpha.com/input/?i=solve+1+%3D+f+%2F+(2+*+sqrt(t+*+m))+for+t
    return (friction * friction) / (4.0 * mass);
}

What about velocity? Does it come into play as well?

grp commented 8 years ago

I think it should work. Would need to test, for sure — if it's good, might be a useful utility to include in Pop itself.