bopit v1.0.2
Bopit
Simple, quick, AABB framework-independent, collision system. We do rectangles only.
What it Does
Bopitonly does axis-aligned bounding-box (AABB) collisions. If you need anything more complicated than that (circles, polygons, etc.) use another library.- Handles tunnelling - all items are treated as "bullets". The fact that we only use AABBs allows doing this fast.
- Strives to be fast while being economic in resources
- It's centered on detection, but it also offers some (minimal & basic) collision response
- Can also return the items that touch a point, a segment or a rectangular zone.
Bopitis gameistic instead of realistic.
Usage
World
import {World, Body} from 'bopit'
const world = new World()
const body = new Body(0, 0, 10, 10)
body.add(world)new World(cellSize)
Creates a new virtual space for tracking physical interactions split into cells.
- Parameters *
cellSizeIs an optional number. It defaults to 64. It represents the size of the sides of the (squared) cells that will be used internally to provide the data. In tile based games, it's usually a multiple of the tile side size. So in a game where tiles are 32x32, cellSize will be 32, 64 or 128. In more sparse games, it can be higher.
- Returns *
- World
world.queryRect(x, y, width, height)
Queries a rectangle in the world space and will return any bodies that are within that area.
- Parameters *
xx coordinate, Numberyy coordinate, Numberwidthwidth of the rectangle, Numberheightheight of the rectangle, Number
- Returns *
- Body an array of Bodies, will return an empty array if none found
world.queryPoint(x, y)
Queries a single point in the world and will return any bodies under that point.
- Parameters *
xx coordinate, Numberyy coordinate, Number
- Returns *
- Body an array of Bodies, will return an empty array if none found
world.querySegment(x1, y1, x2, y2)
Queries a line through the space and will return any bodies that are on that line.
- Parameters *
xx coordinate, Numberyy coordinate, Number
- Returns *
- Body an array of Bodies, will return an empty array if none found
new Body(x, y, width, height)
Creates a new physical, rectangular object that can collide with other things.
- Parameters *
xx coordinate, Numberyy coordinate, Numberwidthwidth of the rectangle, Numberheightheight of the rectangle, Number
- Returns *
- Body
body.add(world)
Adds the body to a world
body.destroy()
Removes the body from the world that it is in.
body.move(x, y, filter)
Will check the collisions and update the position of the body after the collisions
- Parameters *
xx coordinate, Numberyy coordinate, NumberfilterFunction(body, collision, goalX, goalY)bodythe body that the check has been run on.collisionthe collision currently in progressgoalXx position of where the body is headedgoalYy position of where the body is headed
- Returns *
finalXthe final x resting spot after collisionsfinalYthe final y resting spot after collisionscollisionsall of the collision data from collisions that happened along the way
body.check(x, y, filter)
Same as move but without changing the body. This is good for checking before moving.
body.distanceTo(other)
Returns the distance from one body to another
Example
Try out the example by running npm i && npm start in the example directory and
then opening http://localhost:8080
import {World, Body} from 'bopit'
const world = new World(32)
const body = new Body(120, 120, 20, 20)
body.add(world)
const wall1 = new Body(100, 100, 400, 10, true)
wall1.add(world)
const [finalX, finalY, collisions] = body.move(body.x - 10, body.y - 10)Thanks to
kikitohttps://github.com/kikito/bump.lua for writing such an awesome library that I keep re-writing it in other languages.