Skip to content

Super simple class for partitioning 2D games. Builtin collision detection.

Notifications You must be signed in to change notification settings

rgruesbeck/partition2d

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Partition 2D

Simple spacial partition for 2D browser games.

Install

npm install --save partition2d

Run Tests

npm run test

Use

Create

import Grid from 'partition2d';

// create new spacial grid with 100px cells
const grid = new Grid(100);

Add Units

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 Units

// remove unit from the grid
grid.remove(unit)

Move Units

// move an object in the grid
// move(<unit>, x, y)
grid.move(unit, 10, 10)

Check Unit for collisions

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)

Search Unit's local space.

grid.searchNeighborhood(unit, (neighbor) => {
    // neighbor filter function
    return true;
})

About

Super simple class for partitioning 2D games. Builtin collision detection.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published