Skip to content

Commit a658f4a

Browse files
authored
Merge pull request #7 from silentsokolov/bevy-13
Upgrade to bevy 0.13
2 parents 6ab3c48 + 7bd5782 commit a658f4a

File tree

3 files changed

+28
-37
lines changed

3 files changed

+28
-37
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ repository = "https://github.com/johanhelsing/bevy_sparse_grid_2d"
1010
version = "0.3.1"
1111

1212
[dependencies]
13-
bevy = { version = "0.12", default-features = false }
13+
bevy = { version = "0.13", default-features = false }
1414
smallvec = { version = "1.6", features = ["const_generics"] } # same as bevy 0.12

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Personally, I'm using it for simple stupid collision broad phase in a couple of
1111

1212
```rust
1313
use bevy::{
14-
utils::HashSet,
14+
math::{bounding::Aabb2d, vec2},
1515
prelude::*,
16-
math::vec2,
16+
utils::HashSet,
1717
};
18-
use bevy_sparse_grid_2d::{Aabb, SparseGrid2d};
18+
use bevy_sparse_grid_2d::SparseGrid2d;
1919
const TILE_SIZE: usize = 1; // how wide each cell is
2020

2121
let mut db = SparseGrid2d::<TILE_SIZE>::default();
@@ -31,10 +31,8 @@ assert!(matches.contains(&e2));
3131
assert_eq!(matches.len(), 2);
3232

3333
// query an aabb
34-
let matches = db.query_aabb(Aabb {
35-
min: vec2(0.0, 0.0),
36-
max: vec2(1.0, 1.0)
37-
});
34+
let aabb = Aabb2d::new(Vec2::new(0.001, 0.001), Vec2::new(0.001, 0.001));
35+
let matches = db.query_aabb(aabb);
3836
assert!(matches.contains(&e1));
3937
assert!(matches.contains(&e2));
4038
assert_eq!(matches.len(), 2);
@@ -48,7 +46,8 @@ The `main` branch targets the latest bevy release.
4846

4947
|bevy|bevy_sparse_grid_2d|
5048
|----|-------------------|
51-
|0.12|0.3, main |
49+
|0.13|0.4, main |
50+
|0.12|0.3 |
5251
|0.11|0.2 |
5352
|0.10|0.1 |
5453

src/lib.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22
#![doc = include_str!("../README.md")]
33

44
use bevy::{
5+
math::bounding::Aabb2d,
56
prelude::{Entity, Vec2},
67
reflect::Reflect,
78
utils::{HashMap, HashSet},
89
};
910
use smallvec::SmallVec;
1011

11-
/// Axis aligned bounding box
12-
#[derive(Reflect, Debug, Default, Clone, Copy, PartialEq)]
13-
pub struct Aabb {
14-
/// Lower left corner
15-
pub min: Vec2,
16-
/// Upper right corner
17-
pub max: Vec2,
18-
}
19-
2012
type Key = (i32, i32);
2113

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

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

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

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

@@ -95,8 +87,8 @@ struct KeyIter {
9587
}
9688

9789
impl KeyIter {
98-
fn new<const TILE_SIZE: usize>(aabb: impl Into<Aabb>) -> Self {
99-
let Aabb { min, max } = aabb.into();
90+
fn new<const TILE_SIZE: usize>(aabb: impl Into<Aabb2d>) -> Self {
91+
let Aabb2d { min, max } = aabb.into();
10092
// convert to key space
10193
let s = TILE_SIZE as f32;
10294
let min = ((min.x / s).floor() as i32, (min.y / s).floor() as i32);
@@ -132,7 +124,7 @@ impl Iterator for KeyIter {
132124

133125
#[cfg(test)]
134126
mod tests {
135-
use bevy::math::vec2;
127+
use bevy::math::{bounding::Aabb2d, vec2};
136128
use bevy::prelude::default;
137129
use bevy::utils::HashSet;
138130

@@ -142,7 +134,7 @@ mod tests {
142134

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

154146
#[test]
155147
fn keys_four_around_origin() {
156-
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb {
148+
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb2d {
157149
min: vec2(-0.001, -0.001),
158150
max: vec2(0.001, 0.001),
159151
})
@@ -170,15 +162,15 @@ mod tests {
170162
let entity = Entity::from_raw(123);
171163
let mut db = SparseGrid2d::<TILE_SIZE>::default();
172164
db.insert_aabb(
173-
Aabb {
165+
Aabb2d {
174166
min: vec2(-0.001, -0.001),
175167
max: vec2(0.001, 0.001),
176168
},
177169
entity,
178170
);
179171

180172
let matches: Vec<Entity> = db
181-
.aabb_iter(Aabb {
173+
.aabb_iter(Aabb2d {
182174
min: vec2(0.001, 0.001),
183175
max: vec2(0.001, 0.001),
184176
})
@@ -190,7 +182,7 @@ mod tests {
190182
#[test]
191183
fn key_negative() {
192184
let h = TILE_SIZE as f32 / 2.0;
193-
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb {
185+
let keys: Vec<Key> = KeyIter::new::<TILE_SIZE>(Aabb2d {
194186
min: vec2(-h, -h),
195187
max: vec2(-h, -h),
196188
})
@@ -235,29 +227,29 @@ mod tests {
235227
let e3 = Entity::from_raw(3);
236228
let mut db: SparseGrid2d = default();
237229
db.insert_aabb(
238-
Aabb {
230+
Aabb2d {
239231
min: vec2(-h, -h),
240232
max: vec2(h, h),
241233
},
242234
e1,
243235
);
244236
db.insert_aabb(
245-
Aabb {
237+
Aabb2d {
246238
min: vec2(h, h),
247239
max: vec2(h, h),
248240
},
249241
e2,
250242
);
251243
db.insert_aabb(
252-
Aabb {
244+
Aabb2d {
253245
min: vec2(-h, -h),
254246
max: vec2(-h, -h),
255247
},
256248
e3,
257249
);
258250

259251
let matches: Vec<Entity> = db
260-
.aabb_iter(Aabb {
252+
.aabb_iter(Aabb2d {
261253
min: vec2(-h, -h),
262254
max: vec2(h, h),
263255
})
@@ -267,7 +259,7 @@ mod tests {
267259
assert!(matches.contains(&e2));
268260
assert!(matches.contains(&e3));
269261

270-
let matches = db.query_aabb(Aabb {
262+
let matches = db.query_aabb(Aabb2d {
271263
min: vec2(-0.001, -0.001),
272264
max: vec2(-0.001, -0.001),
273265
});
@@ -276,7 +268,7 @@ mod tests {
276268
assert!(matches.contains(&e3));
277269

278270
let matches: Vec<Entity> = db
279-
.aabb_iter(Aabb {
271+
.aabb_iter(Aabb2d {
280272
min: vec2(-0.001, -0.001),
281273
max: vec2(-0.001, -0.001),
282274
})

0 commit comments

Comments
 (0)