@@ -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 ,
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 >
101105where
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 >
120126where
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