spatial_hash_new 1.0.3 copy "spatial_hash_new: ^1.0.3" to clipboard
spatial_hash_new: ^1.0.3 copied to clipboard

An easy-to-use Spatial Hash implementation for Dart. Common use case is for collision detection in games or querying spatially-related data. This is a fork of [spatial_hash](https://github.com/chrisno [...]

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.

0
likes
130
pub points
25%
popularity

Publisher

unverified uploader

An easy-to-use Spatial Hash implementation for Dart. Common use case is for collision detection in games or querying spatially-related data. This is a fork of [spatial_hash](https://github.com/chrisnorman7/spatial_hash), which is itself a fork of [spatialhash](https://github.com/novemberisms/spatialhash).

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on spatial_hash_new