facebookarchive / rebound

A Java library that models spring dynamics and adds real world physics to your app.
http://facebook.github.io/rebound/
Other
5.44k stars 818 forks source link

Use algebraic integration for better performance and accuracy #49

Open iamralpht opened 9 years ago

iamralpht commented 9 years ago

It's possible to use algebraic integration on the damped spring equation, and you get something that implements the same model as Rebound but is faster to compute, a pure function of time and more accurate than doing a numerical integration like RK4.

I did a Java implementation of this here: https://github.com/iamralpht/gravitas/blob/master/Gravitas/src/com/infinite_imagination/physics/Spring.java (the interface was inspired by Rebound, and I originally made it after playing with Rebound but wanting something fast enough that I could generate keyframes for a spring animation in a couple of ms on device).

Also I transliterated it to JavaScript here: https://github.com/iamralpht/iamralpht.github.io/blob/master/fab/demo/fab.js (you can see it in action here: http://iamralpht.github.io/fab -- the UI was inspired by the Facebook Home menu system, which I really liked!).

It ends up being a lot less code than Rebound (but maybe Rebound does a lot more than damped springs...).

Finally, here's a link to the physics textbook I cribbed the equations from (although after playing around in Maxima I was able to reproduce them there): http://www.stewartcalculus.com/data/CALCULUS%20Concepts%20and%20Contexts/upfiles/3c3-AppsOf2ndOrders_Stu.pdf

willbailey commented 9 years ago

This is great stuff. I was aware of the algebraic solution, but hadn't looked into implementing it. I did add the ability to precompute the numerical integration in a tight loop to support key frame animations, but I haven't benchmarked that yet to see how the performance is. It'd be really interesting to compare it with this approach and see what the performance gain is. Have you run any benchmarks comparing Gravitas to Rebound? Are there any tradeoffs with the algebraic approach that you are aware of?

Also nice work on the Inbox demo. I wrote the menu system for Facebook Home, and I totally agree with you on the importance of continuous/interruptible gestures and animations.

iamralpht commented 9 years ago

I haven't benchmarked anything yet, it's on my list :). I'll do it once I've got some demos of Gravitas going.

I don't know of any negative tradeoffs with the algebraic solution for a damped spring. For more complex systems (e.g.: when you drag the message bubbles in iMessage) it probably makes more sense to do something with numerical integration -- you can't integrate every equation. But if all you want to do is damped springs then it's a better solution (imo; numbers pending!).

ppamorim commented 9 years ago

+1

ghost commented 9 years ago

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.