Giter VIP home page Giter VIP logo

grid2.js's Introduction

Grid2.js

is a Node.js npm-package / JavaScript implementation of two dimensional grid for collision detection. Exported for client side use with the help of browserify.

Build Status

About

If you got some objects with 2d coordinates you can use this data structure to speed up some calculations like: field of view, collision detection.

It is a fixed grid, which indexes the inserted objects and keeps them up-to-date on demand.

Install

  • browser
  • Node.js
    • var Vec2 = require('vec2');
    • var Grid2 = require('grid2');

Example

var
  // Some helper variables to save info to.
  objectsOnAlicesPosition,
  objectsOnBobsPosition,
  objectsOnBobsPositionLater,
  objectsOnMap,

  // This will initialize a grid2 with a 100x100 resolution,
  // with cells of the size 10, 10.
  grid = new Grid2({
    'size'     : new Vec2(100, 100),
    'cellSize' : new Vec2(10, 10)
  }),

  // Alice will be staying fierce in the top left ...
  alice = {
    'pos' : new Vec2(20, 20),
    'rad' : 3
  },

  // ... with his rocket luncher, gonna try to shoot bob ...
  rocket = {
    'pos' : new Vec2(20, 20),
    'rad' : 5
  },

  // ... however there is a bunker on the field ...
  bunker = {
    'pos' : new Vec2(50, 50),
    'rad' : 10
  },

  // ... will it save bob?
  bob = {
    'pos' : new Vec2(80, 80),
    'rad' : 3
  };


// Add all of our beloved character to the grid.
grid.addObjects([alice, rocket, bunker, bob]);

// On the start Alice is near to her own rocket.
objectsOnAlicesPosition = grid.getObjectsOn(alice.pos);

// Object.keys(objectsOnAlicesPosition).length);
// >> 2;

// Bob is just sitting and waiting.
objectsOnBobsPosition = grid.getObjectsOn(bob.pos);

// Object.keys(objectsOnBobsPosition).length;
// >> 1;

// The rocket flys over to bob
rocket.pos.x = 78;
rocket.pos.y = 78;

// Update our data structure
grid.updateObject(rocket);

// Lets get some objects after the update.
objectsOnBobsPositionLater = grid.getObjectsOn(bob.pos);

// Object.keys(objectsOnBobsPositionLater).length;
// >> 1;

// Lets get every object for fun.
objectsOnMap = grid.getObjectsBetween(new Vec2(0, 0), new Vec2(100, 100));

// Object.keys(objectsOnMap).length;
// >> 4;

API

Preconditions/assumptions

  • Grid2 is not validating inputs, so make sure you are using it with right parameters
  • objects inserted into the structure need to have position (a Vec2 object) and radius (will be considered as the half length of the outer bounding box)

.addObject(object)

Adds an object to the grid.

  • @param {Object} object | to be inserted, must have .pos and a .rad properties.

.addObjects(objects)

Add objects to the grid, see above.

  • @param {Array} objects | to be inserted.

.getObjectsOn(position)

Returns all objects indexed by the cell having the position.

  • @param {Vec2} position.
  • @return {Object<id,object>} | objects mapped by their ids.

.getObjectsBetween(begPosition, endPosition)

Returns all objects indexed by the cells between the two given positions.

  • @param {Vec2} begPosition | of the searching bounding box.
  • @param {Vec2} endPosition | of the searching bounding box.
  • @return {Object<id,object>} | objects mapped by their ids.

.hasObjectsOn(position)

Returns if there is any objects indexed by the cell having the position.

  • @param {Vec2} position.
  • @return {boolean}

.updateObject(object)

Updates the cell indexes of an object. Use this after you've changed the position of an object placed in the grid.

  • @param {Object} object | to be updated.

.updateObjects(objects)

Updates the cell indexes of objects, see above.

  • @param {Array} objects | to be updated.

.setMetaOn(position, key, value)

Set metadata on cell by the indexed position. This is useful, if there is data, that must be shared between objects indexed by the same cells.

  • @param {Vec2} position | of the cell.
  • @param {string} key | of the metadata.
  • @param {string} value | of the metadata.

.getMetaOn(position, key)

Returns metadata set of a cell on the position. For setting metadata, see above.

  • @param {Vec2} position | of the cell.
  • @param {string} key | of the metadata.
  • @return {*} | whatever you've stored there before, see above.

.inspect()

Returns some inner variables of the grid. Useful for debugging/testing.

  • @return {Object} containing the cells and objects of the grid.

Releases

For more info see the CHANGELOG.md.

  • 0.5.0 (2015-10-29)
  • 0.4.1 (2014-06-09)
  • 0.4.0 (2014-06-08)
  • 0.3.0 (2014-06-08)
  • 0.2.0 (2014-04-27)
  • 0.1.0 (2014-04-27)
  • 0.0.1 (2014-04-26)

License

MIT License

grid2.js's People

Contributors

p1100i avatar

Stargazers

Alex Michaud avatar Faded Weiss avatar L avatar Sabin Tudor avatar gregory nicholas avatar Giorgi Chakhidze avatar  avatar samy kamkar avatar Joel Walker avatar km avatar Jonathan Core avatar Peter Smith avatar Michael Anthony avatar

Watchers

James Cloos avatar Michael Anthony avatar  avatar

grid2.js's Issues

Documentation?

Any chance of there being some simple documentation and code examples that just demonstrate how grid2 works?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.