Skip to content

An easy-to-use spatial hash implemented in Dart

License

Notifications You must be signed in to change notification settings

6bangs/spatial_hash

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:spatial_hash_new/spatial_hash_new.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.

  mySpatialHash.remove(entity);

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)) {
      entity.onCollide(otherEntity);
    }
  }
}

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.

About

An easy-to-use spatial hash implemented in Dart

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 100.0%