Basic, grid-based, 2D top-down player physics for continuous ndarrays
Basic, grid-based, 2D top-down player physics for continuous ndarrays.
You can find a demo here, the code for which is here.
The implementation could be improved a lot - patches welcome :)
Written to be used with browserify.
npm install topdown-physics
physics = require('topdown-physics')(player, field[, options])
player
is an axis-aligned bounding box - use
aabb-2d
for this. field
is a
continuous ndarray to check collisions against. You can also pass the
following options:
friction
: The amount of friction to apply - the higher the value, thephysical
: A function that should the value of a cell and returns whetherinterval
: The granularity of collision checks - generally, this is best1 / 32
.physics.tick()
Steps forward the simulation by one tick - this should be called each frame.
physics.spd
An array you're free to modify directly. The first value is the horizontal speed (in cells per tick), and the second is the vertical speed.
physics.on('collision', callback)
Emitted when a collision is made during a tick.