spatialhash 1.0.1

  • Example
  • Installing
  • Versions
  • 55

SpatialHash #

An easy-to-use implementation of a Spatial Hash for Dart.

Usage #

Creating a spatial hash for Entities 10 cells wide and 10 cells tall, where each cell is 100 x 100 pixels.

import "dart:math";
import "package:spatialhash/spatialhash.dart";

var mySpatialHash = SpatialHash<Entity>(10, 10, 100, 100);

Adding an entity to a spatial hash is done with add.

  var entity = Entity();
  mySpatialHash.add(entity, Rectangle(entity.x, entity.y, entity.width, entity.height));

To update the position of an entity already in the spatial hash, use update.

  mySpatialHash.update(entity, Rectangle(entity.x, entity.y, entity.width, entity.height));

For removing an entity from the spatial hash, use remove.


The main utility of a spatial hash comes from the near method. This provides a set of items that are potentially colliding with the given item.

Sample efficient collision detection. Assume you have an expensive function isOverlapping that computes for pixel-perfect overlap between two entities of arbitrary shape. Instead of comparing each entity's shape with every other entity's shape, you can narrow down the possible entity's to compare against using a spatial hash.

/// calls the [entity]'s `onCollide` method for each entity it is in collision with.
void detectCollisions(Entity entity) {
  for (final otherEntity in mySpatialHash.near(entity)) {
    if (isOverlapping(entity.shape, otherEntity.shape)) {

See the docs for more details and more methods. Or better yet, take a look at the source code! It's only a single dart file.

License #

This package is licensed under the MIT License.

1.0.0 #

  • Initial upload to pub. Full functionality.

1.0.1 #

  • Updated description and ran dartfmt through the code.


import "dart:math";

import "package:spatialhash/spatialhash.dart";

void main() {
  // create a new spatial hash whose origin is at (0, 0), with 10 x 10 cells.
  // each cell will be 100 x 100 pixels in size. therefore the spatial hash stretches
  // from (0, 0) to (1000, 1000).
  final spatialHash = SpatialHash<String>(10, 10, 100, 100);

  // add an item to the spatial hash with the given bounding box.
  // this item is located at (35, 35) and has dimensions of 100 x 100 pixels
  spatialHash.add("some item", Rectangle(35, 35, 100, 100));

  // add a second item to the spatial hash

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  spatialhash: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:spatialhash/spatialhash.dart';
Version Uploaded Documentation Archive
1.0.1 Mar 10, 2019 Go to the documentation of spatialhash 1.0.1 Download spatialhash 1.0.1 archive
1.0.0 Mar 10, 2019 Go to the documentation of spatialhash 1.0.0 Download spatialhash 1.0.0 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jun 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:spatialhash/spatialhash.dart.

Health suggestions

Fix lib/src/spatialhash_base.dart. (-1.99 points)

Analysis of lib/src/spatialhash_base.dart reported 4 hints:

line 88 col 7: DO use curly braces for all flow control structures.

line 97 col 43: DO use curly braces for all flow control structures.

line 135 col 41: DO use curly braces for all flow control structures.

line 149 col 43: DO use curly braces for all flow control structures.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test ^1.0.0