npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

quadtree2

v0.6.0

Published

JavaScript implementation of quadtree datastructure for collision detection.

Downloads

34

Readme

Quadtree2.js

is a Node.js npm-package / JavaScript implementation of two dimensional quadtree for collision detection. Exported for client side use with the help of browserify. It is a work in progress, look for the first stable release if interested.

Build Status

Important notes

After experimenting around with performance tests, the current implementation does not justify the use in game projects. I've started the grid2.js project for collision detection and ray casting calculations in two dimensional space. It is not scaling structure, but performs lot better at the moment in browser games. Give it a try, if that is your use case.

Try it!

Visit the projects GitHub IO Page and play around.

If you notice incorrect behavior please click on the "Log" button, copy the stuff and post it in the issues and describe the problem (or see the contribute section).

About

A quadtree is a scaling data structure for collision detection. You can find theory on the WIKI.

A simple example usecase would be a two dimensional game, with some moving objects like bullets and players. You want to know when a collision occours. Could easly just compare all the objects position with each other, but if there are a lot of them, that is not the right thing to do.

Upon adding objects to the quadtree you either specify the unique number identifier attribute of the objects, like id, or the quadtree will do this automatically.

Install

  • browser
  • Node.js
    • var Vec2 = require('vec2');
    • var Quadtree2 = require('quadtree2');

Contribute

If you want to submit a bugfix, push a test for it as well (should fail without the fix).

  • test with mocha (after npm install) grunt test or grunt watch if you want to run the tests on every change in source files
  • test with browser by opening the index.html
  • build it with grunt

If you've played around on the GitHub IO Page and noticed some incorrect behavior click on the "Log" button, copy the code and create a test case in the test/demos.js file. I'm glad for just a test without any fix as well.

Please follow the git flow branching model.

Use

var // Variable to save the collisions
    alicesCollisions,
    bobsCollisions,
    bobsDeadlyCollisions,

    // This will initialize a quadtree with a 100x100 resolution,
    // with an object limit of 3 inside a quadrant.
    quadtree = new Quadtree2(new Vec2(100, 100), 3),

    // 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 quadtree.
quadtree.addObjects([alice, rocket, bunker, bob]);

// On the start Alice collides with her own rocket.
alicesCollisions = quadtree.getCollisionsForObject(alice);
// Object.keys(alicesCollisions).length;
// >> 1;

// Bob is just sitting and waiting.
bobsCollisions = quadtree.getCollisionsForObject(bob);
// Object.keys(bobsCollisions).length;
// >> 0;

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

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

// Lets get the deadly hit
bobsDeadlyCollisions = quadtree.getCollisionsForObject(bob);
// Object.keys(bobsDeadlyCollisions).length;
// >> 1;

License

MIT License