Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hughsk/topdown-physics
Basic, grid-based, 2D top-down player physics for continuous ndarrays
https://github.com/hughsk/topdown-physics
Last synced: 12 days ago
JSON representation
Basic, grid-based, 2D top-down player physics for continuous ndarrays
- Host: GitHub
- URL: https://github.com/hughsk/topdown-physics
- Owner: hughsk
- Created: 2013-07-16T12:11:03.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-10-15T04:14:45.000Z (about 11 years ago)
- Last Synced: 2024-10-17T16:41:12.893Z (22 days ago)
- Language: JavaScript
- Size: 295 KB
- Stars: 30
- Watchers: 6
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# topdown-physics [![experimental](https://rawgithub.com/hughsk/stability-badges/master/dist/experimental.svg)](http://github.com/hughsk/stability-badges) #
Basic, grid-based, 2D top-down player physics for
[continuous ndarrays](http://github.com/hughsk/ndarray-continuous).You can find a demo [here](http://hughsk.github.io/topdown-physics),
the code for which is
[here](https://github.com/hughsk/topdown-physics/blob/master/demo.js).The implementation could be improved a lot - patches welcome :)
## Installation ##
Written to be used with [browserify](http://browserify.org/).
``` bash
npm install topdown-physics
```## Usage ##
### `physics = require('topdown-physics')(player, field[, options])` ###
`player` is an axis-aligned bounding box - use
[`aabb-2d`](http://npmjs.org/package/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, the
more quickly the player will slow down.
* `physical`: A function that should the value of a cell and returns whether
or not it is solid, i.e. the player cannot move through it. Alternatively,
you can just pass in a constant value. Defaults to 0.
* `interval`: The granularity of collision checks - generally, this is best
set to the reciprocal of the width in pixels of each cell when rendered.
So if each block is 32x32, you should set this to `1 / 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.