GithubHelp home page GithubHelp logo

gamesserver / quadtree Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evilkiwi/quadtree

0.0 2.0 0.0 16 KB

A simple, efficient Quadtree system.

Home Page: https://quadtree.oyed.io

License: GNU General Public License v3.0

JavaScript 100.00%

quadtree's Introduction

Quadtree

Dependencies License

Quadtrees are a method of splitting a game world in to separate nodes in order to increase the performance of collision-based operations.

Whilst this library isn't written specifically for games and can be used for any quadtree-like system (Dynamic or static), I'll focus on documentation being for gamedev related purposes.

Installation

npm install --save @oyed/quadtree

Demo

I have created a demo repo that you can use here:

Explanation

A regular way of checking collisions in a game would be getting ObjectA and checking it's position against every other Object/Entity in my game, and then doing the same for every other Object/Entity. This is minimal with a smaller amount of entities, but isn't scalable. And whilst you can optimise this somewhat, it fundamentally cannot scale. This method is called the n^2 method, as if you have 100 Object/Entities, you'll need to do 100^2 checks to run the collision logic (10,000 checks).

The idea behind a Quadtree in Game Development is to get around this by having spacial awareness. For example, if I'm checking collisions for ObjectA, I only need to check the other Entities/Objects that are around it - there's no point checking for collision against an Entity that's on the other side of the game world. You could do something similar to this in the n^2 method by looping over every single Object/Entity, checking whether its position is close to ObjectA and only then running collision logic on it, but this is still fundamentally unscalable as you are still doing some level of logic on every Object/Entity in the game world, hence the performance bottlenecks of n^2 still apply.

A Quadtree does this by taking Objects that you supply it (In Game Dev, these would be Entities), and then subdividing the game world in to quads based on how many Objects are in a given area. For example, see this image:

Example 1

The blue lines show the quads that were generated by the quadtree after inserting the red objects (Entities).

For more information on Quadtrees and how they work, check out this video: What are Quadtrees

quadtree's People

Watchers

 avatar  avatar

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.