Skip to content

Commit

Permalink
Merge pull request #7 from silentsokolov/bevy-13
Browse files Browse the repository at this point in the history
Upgrade to bevy 0.13
  • Loading branch information
johanhelsing authored Jun 16, 2024
2 parents 6ab3c48 + 7bd5782 commit a658f4a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ repository = "https://github.com/johanhelsing/bevy_sparse_grid_2d"
version = "0.3.1"

[dependencies]
bevy = { version = "0.12", default-features = false }
bevy = { version = "0.13", default-features = false }
smallvec = { version = "1.6", features = ["const_generics"] } # same as bevy 0.12
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Personally, I'm using it for simple stupid collision broad phase in a couple of

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

let mut db = SparseGrid2d::<TILE_SIZE>::default();
Expand All @@ -31,10 +31,8 @@ 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)
});
let aabb = Aabb2d::new(Vec2::new(0.001, 0.001), Vec2::new(0.001, 0.001));
let matches = db.query_aabb(aabb);
assert!(matches.contains(&e1));
assert!(matches.contains(&e2));
assert_eq!(matches.len(), 2);
Expand All @@ -48,7 +46,8 @@ The `main` branch targets the latest bevy release.

|bevy|bevy_sparse_grid_2d|
|----|-------------------|
|0.12|0.3, main |
|0.13|0.4, main |
|0.12|0.3 |
|0.11|0.2 |
|0.10|0.1 |

Expand Down
48 changes: 20 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@
#![doc = include_str!("../README.md")]

use bevy::{
math::bounding::Aabb2d,
prelude::{Entity, Vec2},
reflect::Reflect,
utils::{HashMap, HashSet},
};
use smallvec::SmallVec;

/// Axis aligned bounding box
#[derive(Reflect, Debug, Default, Clone, Copy, PartialEq)]
pub struct Aabb {
/// Lower left corner
pub min: Vec2,
/// Upper right corner
pub max: Vec2,
}

type Key = (i32, i32);

/// A spatial container that allows querying for entities that share one or more grid cell
Expand All @@ -27,7 +19,7 @@ pub struct SparseGrid2d<const TILE_SIZE: usize = 1> {

impl<const TILE_SIZE: usize> SparseGrid2d<TILE_SIZE> {
/// Insert an entity in the given Aabb coordinates
pub fn insert_aabb(&mut self, aabb: impl Into<Aabb>, entity: Entity) {
pub fn insert_aabb(&mut self, aabb: impl Into<Aabb2d>, entity: Entity) {
for key in KeyIter::new::<TILE_SIZE>(aabb) {
self.map.entry(key).or_default().push(entity);
}
Expand All @@ -39,11 +31,11 @@ impl<const TILE_SIZE: usize> SparseGrid2d<TILE_SIZE> {
self.map.entry(key).or_default().push(entity);
}

/// Get an iterator with the entities in the grid cells covered by the given Aabb
/// Get an iterator with the entities in the grid cells covered by the given [`Aabb2d`]
///
/// may contain duplicates if some entities are in more than one grid cell
#[inline]
pub fn aabb_iter(&'_ self, aabb: impl Into<Aabb>) -> impl Iterator<Item = Entity> + '_ {
pub fn aabb_iter(&'_ self, aabb: impl Into<Aabb2d>) -> impl Iterator<Item = Entity> + '_ {
KeyIter::new::<TILE_SIZE>(aabb)
.filter_map(|key| self.map.get(&key))
.flatten()
Expand All @@ -61,9 +53,9 @@ impl<const TILE_SIZE: usize> SparseGrid2d<TILE_SIZE> {
.copied()
}

/// Creates a hash set with all the entities in the grid cells covered by the given Aabb
/// Creates a hash set with all the entities in the grid cells covered by the given [`Aabb2d`]
#[inline]
pub fn query_aabb(&self, aabb: impl Into<Aabb>) -> HashSet<Entity> {
pub fn query_aabb(&self, aabb: impl Into<Aabb2d>) -> HashSet<Entity> {
self.aabb_iter(aabb).collect()
}

Expand Down Expand Up @@ -95,8 +87,8 @@ struct KeyIter {
}

impl KeyIter {
fn new<const TILE_SIZE: usize>(aabb: impl Into<Aabb>) -> Self {
let Aabb { min, max } = aabb.into();
fn new<const TILE_SIZE: usize>(aabb: impl Into<Aabb2d>) -> Self {
let Aabb2d { min, max } = aabb.into();
// convert to key space
let s = TILE_SIZE as f32;
let min = ((min.x / s).floor() as i32, (min.y / s).floor() as i32);
Expand Down Expand Up @@ -132,7 +124,7 @@ impl Iterator for KeyIter {

#[cfg(test)]
mod tests {
use bevy::math::vec2;
use bevy::math::{bounding::Aabb2d, vec2};
use bevy::prelude::default;
use bevy::utils::HashSet;

Expand All @@ -142,7 +134,7 @@ mod tests {

#[test]
fn keys_single() {
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb {
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb2d {
min: vec2(0.001, 0.001),
max: vec2(0.001, 0.001),
})
Expand All @@ -153,7 +145,7 @@ mod tests {

#[test]
fn keys_four_around_origin() {
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb {
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb2d {
min: vec2(-0.001, -0.001),
max: vec2(0.001, 0.001),
})
Expand All @@ -170,15 +162,15 @@ mod tests {
let entity = Entity::from_raw(123);
let mut db = SparseGrid2d::<TILE_SIZE>::default();
db.insert_aabb(
Aabb {
Aabb2d {
min: vec2(-0.001, -0.001),
max: vec2(0.001, 0.001),
},
entity,
);

let matches: Vec<Entity> = db
.aabb_iter(Aabb {
.aabb_iter(Aabb2d {
min: vec2(0.001, 0.001),
max: vec2(0.001, 0.001),
})
Expand All @@ -190,7 +182,7 @@ mod tests {
#[test]
fn key_negative() {
let h = TILE_SIZE as f32 / 2.0;
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb {
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb2d {
min: vec2(-h, -h),
max: vec2(-h, -h),
})
Expand Down Expand Up @@ -235,29 +227,29 @@ mod tests {
let e3 = Entity::from_raw(3);
let mut db: SparseGrid2d = default();
db.insert_aabb(
Aabb {
Aabb2d {
min: vec2(-h, -h),
max: vec2(h, h),
},
e1,
);
db.insert_aabb(
Aabb {
Aabb2d {
min: vec2(h, h),
max: vec2(h, h),
},
e2,
);
db.insert_aabb(
Aabb {
Aabb2d {
min: vec2(-h, -h),
max: vec2(-h, -h),
},
e3,
);

let matches: Vec<Entity> = db
.aabb_iter(Aabb {
.aabb_iter(Aabb2d {
min: vec2(-h, -h),
max: vec2(h, h),
})
Expand All @@ -267,7 +259,7 @@ mod tests {
assert!(matches.contains(&e2));
assert!(matches.contains(&e3));

let matches = db.query_aabb(Aabb {
let matches = db.query_aabb(Aabb2d {
min: vec2(-0.001, -0.001),
max: vec2(-0.001, -0.001),
});
Expand All @@ -276,7 +268,7 @@ mod tests {
assert!(matches.contains(&e3));

let matches: Vec<Entity> = db
.aabb_iter(Aabb {
.aabb_iter(Aabb2d {
min: vec2(-0.001, -0.001),
max: vec2(-0.001, -0.001),
})
Expand Down

0 comments on commit a658f4a

Please sign in to comment.