Coder Social home page Coder Social logo

unity-spring's Introduction

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://[email protected]/thammin/unity-spring.git

via OpenUPM

openupm add com.thammin.unity-spring

Usage

Every solver is just a simple class with few fields.

using UnityEngine;
using Spring = UnitySpring.ClosedForm.Spring;

public class Ball : MonoBehaviour
{
    Spring spring;

    void Start()
    {
        // interpolate from -10f to 10f
        spring = new Spring()
        {
            startValue = -10f,
            endValue = 10f
        };
    }

    void Update()
    {
        var x = spring.Evaluate(Time.deltaTime);
        transform.position = new Vector3(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 coefficients
float exp = 1F / (1F + x + 0.48F * x * x + 0.235F * x * x * x);

References

Analytical:

Numerical:

General:

License

MIT

unity-spring's People

Contributors

thammin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.