Skip to content

Repository files navigation

A minimal spring physics library for Unity

Implement multiple solvers for damped harmonic oscillator.

Solvers:

Maybe not:

Performance rough check with 2.6 GHz Intel Core i7:

Install

via Package Manager UI

ssh://git@github.com/thammin/unity-spring.git

via OpenUPM

openupm add com.thammin.unity-spring

Usage

Every solver is just a simple class with few fields.

usingUnityEngine;usingSpring=UnitySpring.ClosedForm.Spring;publicclassBall:MonoBehaviour{Springspring;voidStart(){// interpolate from -10f to 10fspring=newSpring(){startValue=-10f,endValue=10f};}voidUpdate(){varx=spring.Evaluate(Time.deltaTime);transform.position=newVector3(x,0f,0f);}}

Screenshot or demo

Visualizer:

FAQ

Unity SmoothDamp

Source code: link

Based on closed-form solution, but only modeling critically damped spring. Using tweaked Exponential approximation (up to Taylor 3rd order) which claims as roughly 80 times faster and approximate less than 0.1% error than exp function.

// tweaked coefficientsfloatexp=1F/(1F+x+0.48F*x*x+0.235F*x*x*x);

References

Analytical:

Numerical:

General:

License

MIT

About

A minimal spring physics library for Unity

Topics

Resources

Stars

123 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages