#bevy #2d-grid #gamedev #sparse #querying #broad-phase #collision

bevy_sparse_grid_2d

Wrapper around a 2d hash map made for collision broadphase

4 releases (2 breaking)

0.3.1 Jan 6, 2024
0.3.0 Nov 7, 2023
0.2.0 Jul 10, 2023
0.1.0 Mar 7, 2023

#1703 in Game dev

MIT/Apache

12KB
252 lines

bevy_sparse_grid_2d

crates.io MIT/Apache 2.0

An opinionated 2D sparse grid made for use with Bevy. For storing and querying entities.

Personally, I'm using it for simple stupid collision broad phase in a couple of my projects.

Usage

use bevy::{
    utils::HashSet,
    prelude::*,
    math::vec2,
};
use bevy_sparse_grid_2d::{Aabb, SparseGrid2d};
const TILE_SIZE: usize = 1; // how wide each cell is

let mut db = SparseGrid2d::<TILE_SIZE>::default();
let e1 = Entity::from_raw(1);
let e2 = Entity::from_raw(2);
db.insert_point(vec2(0.5, 0.5), e1);
db.insert_point(vec2(0.499, 0.501), e2);

// query a single point
let matches: HashSet<_> = db.point_iter(vec2(0.499, 0.501)).collect();
assert!(matches.contains(&e1));
assert!(matches.contains(&e2));
assert_eq!(matches.len(), 2);

// query an aabb
let matches = db.query_aabb(Aabb {
    min: vec2(0.0, 0.0),
    max: vec2(1.0, 1.0)
});
assert!(matches.contains(&e1));
assert!(matches.contains(&e2));
assert_eq!(matches.len(), 2);

See tests in lib.rs

Bevy Version Support

The main branch targets the latest bevy release.

bevy bevy_sparse_grid_2d
0.12 0.3, main
0.11 0.2
0.10 0.1

License

MIT or Apache-2.0

Contributions

PRs welcome!

Dependencies

~21MB
~393K SLoC