@downpourdigital/physics v2.0.1
@downpourdigital/physics
Spring physics for (UI-) animation!
Installation
yarn add @downpourdigital/physicsnpm i --save @downpourdigital/physicsAll multi-dimensional versions of the springs depend on gl-matrix, which you'll have to install manually.
Usage
import {
EulerSpring,
RK4Spring,
LinearMotion,
Passthrough,
defaultWorld,
} from "@downpourdigital/physics";
import {
EulerSpring2D,
EulerSpring3D,
EulerSpringQuat,
RK4Spring2D,
RK4Spring3D,
RK4SpringQuat,
} from "@downpourdigital/physics/multi";
const loop = () => {
defaultWorld.step( performance.now() );
requestAnimationFrame( loop );
};
requestAnimationFrame( loop );Simulation Variants
Springs are available as 1D, 2D, 3D, and quaternion versions, as well as with two different integrators.
EulerSprings are integrated via the semi-implicit Euler method. They are cheap to compute and probably good enough for most applications.RK4Springs are integrated via the Runge–Kutta fourth order method. While computation-heavy, they are more accurate and may also be more stable under certain conditions.
Timestep
All simulations use a semi-fixed timestep internally. While the actual calculation is tied to the framerate, you can specify a maximum duration per simulation timestep (maxTimestep). If the interval between frames is higher than the max duration, it will be divided into multiple simulation timesteps. The amount of 'substeps' can be limited via maxStepCount.
Decreasing maxTimestep generally yields higher accuracy.
Example
import {
RK4Spring,
} from "@downpourdigital/physics";
const spring = new RK4Spring({
maxTimestep: 17, // in ms (default: 17)
maxStepCount: 8, // (default: 8)
});Springs
Config
A spring represents a mass-spring-damper system. It accepts the following options:
massmass in kg (default:1)stiffnessusually1-500(default:250) Higher values cause the spring to be stiffer. (spring constant k from Hooke's Law)dampingusually1-100(default:15) Higher values reduce oscillations more. (damping coefficient b)valueinitial position (default:0)velocityinitial velocity (default:0)restDeltathe minimum speed (velocity magnitude) after which the system is considered resting. (default:.0001)
Example
import {
RK4Spring,
} from "@downpourdigital/physics";
const spring = new RK4Spring({
mass: 1,
stiffness: 250,
damping: 15,
});Props and Methods
isResting: booleanIndicates wether the simulation is in a resting state or actively changing.
set( value: number ): voidSets the spring target to a given position. The spring will subsequently begin moving towards that position.
get(): numberReturns the current spring position.
getVelocity(): numberReturns the current velocity.
reset(): voidResets the spring to its initial position and to zero velocity.
resetTo( value: number ): voidResets the spring to a given position and to zero velocity.
stop(): voidEnds the simulation.
2D, 3D and quaternion specific methods
set( value: vec2 | vec3 | quat ): voidandresetTo( value: vec2 | vec3 | quat ): voidsetandresetToexpect the value to be agl-matrixvec2/vec3/quaton the 2D, 3D and quat versions respectively.
get( out: vec2 | vec3 | quat ): voidandgetVelocity( out: vec2 | vec3 | quat ): voidSimilarly to
gl-matrixfunctions,getandgetVelocitydon't return the position but rather expect anoutvector to write the result to. This allows for better optimisation.
read(): vec2 | vec3 | quatandreadVelocity(): vec2 | vec3 | quatreadandreadVelocityreturn the internal vector representation of position and velocity respectively.
As they are being used internally, they should only be read. Mutating them will mess up the simulation. Also their values will change as the simulation runs.
Passthrough
Passthrough is a drop-in replacement for any 1D spring. As the name suggests, it just passes through the target value unaltered.
LinearMotion
LinearMotion represents a uniform linear motion with constant velocity/zero acceleration. It moves towards a given target at a given constant speed.
The API surface is the same as with other 1D springs.
Example
import {
LinearMotion,
} from "@downpourdigital/physics";
const spring = new LinearMotion({
value: 0, // initial position (default: 0)
speed: 1, // speed in units per second (default: 1)
});Manual stepping
Stepping can also be done manually: if autoStep: false is passed in the config of any simulation, it won't be attached to the default physics world. You can now step each simulation individually or bundle multiple simulations together in another physics world.
Example
import {
World,
EulerSpring,
} from "@downpourdigital/physics";
const spring = new EulerSpring({
autoStep: false,
});
// either step individually
spring.step( time );
// or bundle in a world
const world = new World();
world.add( spring );
world.step( time );Usage with @downpourdigital/scheduler
import { loop, update } from '@downpourdigital/scheduler';
import { defaultWorld } from '@downpourdigital/physics';
loop( () => [update( ( delta, time ) => defaultWorld.step( time ) )]);License
© 2021 DOWNPOUR DIGITAL, licensed under BSD-4-Clause