Skip to content

Commit b0bdff1

Browse files
committed
- remove unrelated type params of QuadTree
- support center setting
1 parent e8e1e28 commit b0bdff1

File tree

9 files changed

+195
-248
lines changed

9 files changed

+195
-248
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com
99
-->
1010

1111
## [Unreleased]
12+
## [0.15.1-beta.3] - 2025-01-23
13+
14+
- remove unrelated type params of `QuadTree`
15+
- support center setting
16+
1217
## [0.15.1-beta.2] - 2025-01-23
1318

1419
- improved the organization of public exports (`pub use`) to enhance usability and clarity.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ members = ["bevy_quadtree"]
33
resolver = "2"
44

55
[workspace.package]
6-
version = "0.15.1-beta.2"
6+
version = "0.15.1-beta.3"
77
authors = ["Louis <[email protected]>"]
88
description = "A quadtree plugin for bevy"
99
license = "MIT"

bevy_quadtree/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ A quadtree plugin for bevy.
6868
Function:
6969
- Auto update following `Changed<GlobalTransform>`, all users need to do is querying.
7070
- LooseQuadTree supported.
71-
- Compile time optimized with const type parameters.
7271

7372
Features:
7473
- `gizmos`: show gizmos of the quadtree boundaries.
7574
- `sprite`: enable `CollisionRect` and `CollisionRotatedRect` to track `sprite.custom_size`.
76-
- `multi-quadtree`: support multiple quadtrees in one world.
75+
- `multi-quadtree`: support multiple quadtrees in one world, see [`MultiQuadTreePlugin`].
7776

7877
Version:
7978

@@ -85,6 +84,9 @@ And upgrade will be like `bevy_version.beta.x`
8584
For those who upgrade from version <= 0.15.1-alpha7, pay attention to the new type paramter `D` in `QuadTreePlugin`.
8685
And shapes now have `ID` as well. Moreover, the tree memory is pre-allocated, no longer dynamically allocating.
8786

87+
For those who upgrade from version <= 0.15.1-beta.2, `X` `Y` type params added to Plugins to set the origin of boundary.
88+
The QuadTree's type params is simplified to only `ID` as well.
89+
8890
<p align="right">(<a href="#readme-top">back to top</a>)</p>
8991

9092

@@ -122,13 +124,14 @@ fn main() {
122124
.add_plugins(QuadTreePlugin::<(
123125
(CollisionCircle, GlobalTransform), (CollisionRect, (GlobalTransform, Sprite)),
124126
),
125-
40, 8, 100, 100, 20>::default()
127+
40, 8, 100, 100, 0, 0, 20, 114514>::default()
126128
)
127129
// CollisionCircle follows GlobalTransform, CollisionRect follows Sprite and GlobalTransform
128130
// at most 40 entities in a node
129131
// at most 8 levels
130-
// 100 x 100 world size
131-
// 20 / 10 = 2 = outlet_boundary / inlet_boundary (for loose quadtree)
132+
// 100 x 100 world size, center at (0, 0)
133+
// 20 / 10 = 2.0 = outlet_boundary / inlet_boundary (for loose quadtree)
134+
// quadtree id 114514
132135
.run();
133136
}
134137
# }
@@ -154,7 +157,8 @@ cmds.spawn((
154157
4. Query the quadtree like bevy's `Or, Not`:
155158

156159
```rust ignore
157-
type MyQuadTree = QuadTree<40, 8, 100, 100, 20>;
160+
// default id 0
161+
type MyQuadTree = QuadTree<114514>;
158162

159163
fn pick(
160164
mut gizmos: Gizmos,

bevy_quadtree/src/plugin/mod.rs

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ use bevy_transform::prelude::*;
3838
///
3939
/// `W`: The width of the root node boundary.
4040
/// `H`: The height of the root node boundary.
41-
/// The boundary's center is (0, 0).
41+
/// The boundary's center is (`X`, `Y`).
4242
///
43-
/// `K`: For `LooseQuadTree`, K / 10 = outlet_boundary / inlet_boundary. Set K to 10 by default and 20 is founded best.
43+
/// `K`: For `LooseQuadTree`, K / 10 = outlet_boundary / inlet_boundary. K is set to 20 by default.
4444
/// K should >= 10. Only if the object move and is **no longer completely contained** by the outlet_boundary will it be inserted again.
4545
///
4646
/// `ID`: Set a lucky number as you like for the quadtree.
@@ -61,7 +61,7 @@ use bevy_transform::prelude::*;
6161
/// (CollisionRotatedRect, Sprite),
6262
/// (CollisionRect, (GlobalTransform, Sprite)),
6363
/// ),
64-
/// 40, 4, 100, 100, 20>::default());
64+
/// 40, 4, 100, 100, 0, 0, 20>::default());
6565
/// # }
6666
/// ```
6767
///
@@ -81,7 +81,9 @@ pub struct QuadTreePlugin<
8181
const D: usize,
8282
const W: usize,
8383
const H: usize,
84-
const K: usize = 10,
84+
const X: usize = 0,
85+
const Y: usize = 0,
86+
const K: usize = 20,
8587
const ID: usize = 0,
8688
> where
8789
P: TrackingPair,
@@ -95,9 +97,11 @@ impl<
9597
const N: usize,
9698
const W: usize,
9799
const H: usize,
100+
const X: usize,
101+
const Y: usize,
98102
const K: usize,
99103
const ID: usize,
100-
> Default for QuadTreePlugin<P, N, D, W, H, K, ID>
104+
> Default for QuadTreePlugin<P, N, D, W, H, X, Y, K, ID>
101105
where
102106
P: TrackingPair,
103107
{
@@ -114,9 +118,11 @@ impl<
114118
const D: usize,
115119
const W: usize,
116120
const H: usize,
121+
const X: usize,
122+
const Y: usize,
117123
const K: usize,
118124
const ID: usize,
119-
> Plugin for QuadTreePlugin<P, N, D, W, H, K, ID>
125+
> Plugin for QuadTreePlugin<P, N, D, W, H, X, Y, K, ID>
120126
where
121127
P: TrackingPair,
122128
{
@@ -126,11 +132,11 @@ where
126132
assert!(W > 0, "W should > 0");
127133
assert!(H > 0, "H should > 0");
128134
assert!(K >= 10, "K should >= 10");
129-
app.init_resource::<QuadTree<N, D, W, H, K, ID>>()
135+
app.insert_resource(QuadTree::<ID>::new(N, D, W, H, X, Y, K))
130136
.add_systems(PreUpdate, P::update_collision())
131-
.add_systems(Update, P::update_quadtree::<N, D, W, H, K, ID>());
137+
.add_systems(Update, P::update_quadtree::<ID>());
132138
#[cfg(feature = "gizmos")]
133-
app.add_systems(PostUpdate, P::show_boundary::<N, D, W, H, K, ID>());
139+
app.add_systems(PostUpdate, P::show_boundary::<ID>());
134140
}
135141
}
136142

@@ -140,24 +146,10 @@ pub trait TrackingPair: Send + Sync + 'static {
140146
/// return the system to update collision
141147
fn update_collision() -> SystemConfigs;
142148
/// return the system to update quadtree
143-
fn update_quadtree<
144-
const N: usize,
145-
const D: usize,
146-
const W: usize,
147-
const H: usize,
148-
const K: usize,
149-
const ID: usize,
150-
>() -> SystemConfigs;
149+
fn update_quadtree<const ID: usize>() -> SystemConfigs;
151150
/// return the system to show box
152151
#[cfg(feature = "gizmos")]
153-
fn show_boundary<
154-
const N: usize,
155-
const D: usize,
156-
const W: usize,
157-
const H: usize,
158-
const K: usize,
159-
const ID: usize,
160-
>() -> SystemConfigs;
152+
fn show_boundary<const ID: usize>() -> SystemConfigs;
161153
/// return the shape id, to ensure no duplicate shape updating system added
162154
#[cfg(debug_assertions)]
163155
fn shape_id() -> std::any::TypeId;
@@ -172,27 +164,13 @@ macro_rules! impl_tracking_pair {
172164
fn update_collision() -> SystemConfigs {
173165
update_collision::<S, $c>.ambiguous_with_all()
174166
}
175-
fn update_quadtree<
176-
const N: usize,
177-
const D: usize,
178-
const W: usize,
179-
const H: usize,
180-
const K: usize,
181-
const ID: usize,
182-
>() -> SystemConfigs {
183-
update_quadtree::<S, N, D, W, H, K, ID>.ambiguous_with_all()
167+
fn update_quadtree<const ID: usize>() -> SystemConfigs {
168+
update_quadtree::<S, ID>.ambiguous_with_all()
184169
}
185170
#[cfg(feature = "gizmos")]
186-
fn show_boundary<
187-
const N: usize,
188-
const D: usize,
189-
const W: usize,
190-
const H: usize,
191-
const K: usize,
192-
const ID: usize,
193-
>() -> SystemConfigs {
171+
fn show_boundary<const ID: usize>() -> SystemConfigs {
194172
use crate::system::show_boundary;
195-
show_boundary::<S, N, D, W, H, K, ID>.ambiguous_with_all()
173+
show_boundary::<S, ID>.ambiguous_with_all()
196174
}
197175
#[cfg(debug_assertions)]
198176
fn shape_id() -> std::any::TypeId {
@@ -216,15 +194,15 @@ macro_rules! impl_tracking_pair_tuple {
216194
// update_collision has Mut<S>, so chain them
217195
($(update_collision::<S, $c>),+,).chain()
218196
}
219-
fn update_quadtree<const N: usize, const D: usize, const W: usize, const H: usize, const K: usize, const ID: usize>(
197+
fn update_quadtree<const ID: usize>(
220198
) -> SystemConfigs {
221-
update_quadtree::<S, N, D, W, H, K, ID>.ambiguous_with_all()
199+
update_quadtree::<S, ID>.ambiguous_with_all()
222200
}
223201
#[cfg(feature = "gizmos")]
224-
fn show_boundary<const N: usize, const D: usize, const W: usize, const H: usize, const K: usize, const ID: usize>(
202+
fn show_boundary<const ID: usize>(
225203
) -> SystemConfigs {
226204
use crate::system::show_boundary;
227-
show_boundary::<S, N, D, W, H, K, ID>.ambiguous_with_all()
205+
show_boundary::<S, ID>.ambiguous_with_all()
228206
}
229207
#[cfg(debug_assertions)]
230208
fn shape_id() -> std::any::TypeId {
@@ -253,7 +231,7 @@ macro_rules! impl_tracking_pairs {
253231
// no duplicate shape in `P`s, so ambiguous_with_all
254232
($([<P $i>]::update_collision()),+,).ambiguous_with_all()
255233
}
256-
fn update_quadtree<const N: usize, const D: usize, const W: usize, const H: usize, const K: usize, const ID: usize>(
234+
fn update_quadtree<const ID: usize>(
257235
) -> SystemConfigs {
258236
#[cfg(debug_assertions)]
259237
{
@@ -264,10 +242,10 @@ macro_rules! impl_tracking_pairs {
264242
}
265243
);+
266244
}
267-
($([<P $i>]::update_quadtree::<N, D, W, H, K, ID>()),+,).ambiguous_with_all()
245+
($([<P $i>]::update_quadtree::<ID>()),+,).ambiguous_with_all()
268246
}
269247
#[cfg(feature = "gizmos")]
270-
fn show_boundary<const N: usize, const D: usize, const W: usize, const H: usize, const K: usize, const ID: usize>(
248+
fn show_boundary<const ID: usize>(
271249
) -> SystemConfigs {
272250
#[cfg(debug_assertions)]
273251
{
@@ -278,7 +256,7 @@ macro_rules! impl_tracking_pairs {
278256
}
279257
);+
280258
}
281-
($([<P $i>]::show_boundary::<N, D, W, H, K, ID>()),+,).ambiguous_with_all()
259+
($([<P $i>]::show_boundary::<ID>()),+,).ambiguous_with_all()
282260
}
283261
#[cfg(debug_assertions)]
284262
fn shape_id() -> std::any::TypeId {
@@ -317,6 +295,8 @@ mod tests {
317295
4,
318296
100,
319297
100,
298+
0,
299+
0,
320300
20,
321301
>::default());
322302
}
@@ -331,6 +311,8 @@ mod tests {
331311
4,
332312
100,
333313
100,
314+
0,
315+
0,
334316
20,
335317
>::default());
336318
}
@@ -350,6 +332,8 @@ mod tests {
350332
4,
351333
100,
352334
100,
335+
0,
336+
0,
353337
20,
354338
>::default());
355339
}
@@ -367,6 +351,8 @@ mod tests {
367351
4,
368352
100,
369353
100,
354+
0,
355+
0,
370356
20,
371357
>::default());
372358
}
@@ -382,6 +368,8 @@ mod tests {
382368
4,
383369
100,
384370
100,
371+
0,
372+
0,
385373
20,
386374
>::default());
387375
}
@@ -398,6 +386,8 @@ mod tests {
398386
4,
399387
100,
400388
100,
389+
0,
390+
0,
401391
20,
402392
>::default());
403393
}

0 commit comments

Comments
 (0)