Simple spacial partition for 2D browser games.
npm install --save partition2d
npm run test
import Grid from 'partition2d';
// create new spacial grid with 100px cells
const grid = new Grid(100);
const grid = new Grid(100);
let unit = {
id: Math.random().toString(16).slice(2),
xPosition: 25,
yPosition: 25,
radius: 5
};
// add a new unit to the grid
grid.add(unit)
// remove unit from the grid
grid.remove(unit)
// move an object in the grid
// move(<unit>, x, y)
grid.move(unit, 10, 10)
const circleToCircle = function(a, b) {
const vx = a.xPosition - b.xPosition;
const vy = a.yPosition - b.yPosition;
const length = Math.sqrt(vx * vx + vy * vy);
if(length < a.radius + b.radius){
return true;
}
return false;
};
// check the local space for collisions with a unit using a circle-to-circle collision function
grid.checkCollisions(unit, circleToCircle)
grid.searchNeighborhood(unit, (neighbor) => {
// neighbor filter function
return true;
})