Godot Spring Damper
By Hyphinett
A simple spring damper implementation based on this post. Works with floats and vectors.
Examples can be found in the examples folder.
These spring dampers are intended to be instanced from code as an interpolation utility for Node implementations to keep things as lightweight as possible.
You can create a spring damper system like so:
new_sd = SpringDamper.new(init_pos,init_vel,init_freq:float,init_damp:float,init_delta:float = 0.0)
The constructor arguments are as follows:
- init_pos: The initial position of the spring. Either a vector or float value.
- init_vel: The initial velocity of the spring of same type as the initial position.
- init_freq: The initial angular frequency of the spring. A float that determines the springs strength.
- init_damp: The initial damping ratio of the spring, a float that determines how soft the movement is, a value between 0 and 1 is under damped and will behave more like a spring a value of 1 is critically damped and higher values are more heavily damped.
- init_delta: The initial delta time value, do not provide a value for this if you are updating the spring damper in _process, instead you should pass the delta value of the _process function into the update call for the spring damper.
You can update a spring damper like so:
new_sd.update_spring_damper(targ,delta:float=0)
The update arguments are:
- targ: the updated target position of the spring damper. This should be of the same type as the initial position argument provided to the constructor.
- delta: the current frames delta time. Only pass this if you are not calling the update function at a fixed interval.
The return value of this function is the updated position however you may wish to access the updated velocity of the spring damper after such a call and can do so with new_sd.vel
It is recommended that you use the Spring Damper on a fixed update if possible for better performance.
You can achieve this by passing get_physics_process_delta_time() to the init_delta parameter and calling update_spring_damper in _physics_process. There is no need to supply a delta value to the update function if you provided an init_delta to the constructor, however if you do, it will update the spring dampers fixed_delta value.
If for whatever reason you are working with a fixed timestep that changes occasionally you can avoid unnecessary calculations by only passing the delta time on frames where the timestep changes.
Version Support
The examples in this project will only work as far back as Godot 4.5, however the spring damper code itself works on any version after 4.0. For versions prior to 4.5 you can download the no-examples version here. For versions 4.5 and 4.6 you can install Spring Damper through the Asset Library and for versions starting at 4.7 you can install from the Asset Store.
Changelog for version v1.1.1-no-examples
No changelog provided for this version.