vangogh500-physics
v0.0.4-a
Published
Custom physics library
Downloads
3
Readme
vangogh500-physics
version 0.0.1
Custom physics library aimed to work with vangogh500-react-pixi.
Docs/Api
Link to the docs.
User Guide
Creating a body
A body represents a 3d object. Body is a generic class and so it can carry with it properties that are not included.
import {Vector, Body, KinematicState} from 'vangogh500-physics'
// all generics must include mass and rotational mass
// flow annotations allows us to make these properties read only
type fluidLike = {
+viscosity: number, +m: number, +mr: number
}
// a kinematic state represents the body's kinematics. We set its positional vector to 0,0,0.
const kinematics = new KinematicState(new Vector())
const body: Body<fluidLike> = new Body({ viscosity: 1, m: 1, mr: 1}, new KinematicState(new Vector()))
Once a body is created, it can easily be manipulated by applying forces and torques.
body.applyForce(new Vector(10,0,0))
body.applyTorque(new Vector(0,0,1))
To update the body simply:
body.update()
which will reset the forces and torques applied and update its kinematics accordingly.
Creating forces and applying them
The body comes with a useful getState() function, which gives you a non-writable copy of the kinematics state and its properties. Forces that rely on the body's meta data should use this.
const state = body.getState()
state.v \\ gives you the velocity vector (readable but non-writable)
The library comes with some useful factories, which will help you create generic forces.
import {FluidMechanicsFactory} from 'vangogh500-physics'
// creates a drag force, where the env has a density 1.
const dragForce = FluidMechanicsFactory.drag(body.getState(), { p: 1 })
Since bodies are generic it will be easy to add properties to bodies that can than be used to create new forces.
Hooking everything up to a loop
Hooking everything to an event loop is easy to animate the physics. Simply apply all forces/torques to the body, update, and then render using your favorite graphics environment.
Notes
All classes are pure except for body. Calling update on a kinematics state will return a new state. Make sure to protect body using body.getState().
Installation
Run
npm install vangogh500-physics