voxel-physics-engine
v0.13.0
Published
Simple but physical engine for voxel games
Downloads
453
Readme
voxel-physics-engine
An abstracted physics engine for voxel game engines.
This implements reasonably realistic physics for voxel games.
It was made to work with noa
or voxel.js
, but it just takes references to
an abstracted getBlock(x,y,z)
function and plain vectors,
so it can be used with other engines.
This replaces voxel-physical, though it works quite differently and behaves more physically.
The engine can be seen in action in noa or projects using it (such as this talk on voxels.
###Usage:
- Create engine and supply a
getBlock(x,y,z)
-like function (which should return true when block xyz is solid) - Register rigid bodies with
var body = addBody(..)
- Control them with
body.addImpulse[0,1,0]
and so on - Call
tick(dt)
and the engine will manage all the physics and collisions.
Example
import {Physics} from 'voxel-physics-engine'
var opts = { gravity: [0,-10,0] }
var getter = function(x,y,z) { /* your logic here */ }
var phys = new Physics(opts, getter)
var body = phys.addBody( aabb, mass, friction, restitution, gravityMult, onCollide, autoStep )
phys.tick( dt_in_miliseconds )
phys.removeBody( body )
Features
- Reasonably physical, supports standard properties (friction, restitution, etc.)
- Query the body's
resting[axis]
property (-1,0,1 on each axis) to tell if it's currently touching a solid voxel. E.g.body.resting[1]
is1
when the body is colliding with the ceiling. - Collisions with terrain trigger each body's
onCollide(impacts)
callback. The argument is avec3
of the change in velocity on each axis, so multiply by mass if you want the impulse. (A body resting on the ground will produce a small impact each tick due to gravity.) - If you set a body's
autoStep
property, the engine will cause it to automatically "step" up hills (i.e. 1-block obstructions).
Changes in latest version:
0.13.0
- minor fixes to typing and internals0.11.0
- export a named constructor function instead of a callback0.10.0
- Fixes
body.onCollide
arg to be a correctly scaled impulse vector - Body
onCollide
now passed before bounces are resolved (allowing client to adjustbody.restitution
depending on the terrain)
- Fixes
0.9.0
- Air and fluid friction properties renamed to
airDrag
andfluidDrag
, and now work equivalently (no drag at0
, large drag at1
) - Per body
airDrag
andfluidDrag
override global settings if>= 0
- Air and fluid friction properties renamed to
0.8.0
- Friction now uses regular coefficients, and works in all directions, not just downwards
- Bodies have an
.airFriction
that overrides the global value (if nonzero) - Air and regular friction should now scale correctly with frame rate