
About The Project

A quadtree plugin for bevy.
Function:
- Auto update following
Changed<GlobalTransform>, all users need to do is querying.
- LooseQuadTree supported.
- Compile time optimized with const type parameters.
Features:
gizmos: show gizmos of the quadtree boundaries.
sprite: enable CollisionRect to track sprite.custom_size.
Built With
Usage
- Add the plugin to your
Cargo.toml:
[dependencies]
bevy_quadtree = { version = "0.15.1" }
- Add the plugin to your Bevy app:
use bevy::prelude::*;
use bevy_quadtree::{QuadTreePlugin, CollisionCircle, CollisionRect};
fn main() {
#[cfg(feature = "sprite")]
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(QuadTreePlugin::<(
(CollisionCircle, GlobalTransform), (CollisionRect, Sprite), (CollisionRect, GlobalTransform)),
40, 100, 100, 20>::default()
)
.run();
}
- Spawn CollisionShapes as Components:
cmds.spawn((
Sprite {
color: Color::WHITE,
custom_size: Some(CUSTOM_SIZE),
..Default::default()
},
CollisionRect::from(Rect::from_center_size(pos, CUSTOM_SIZE)),
Transform::from_xyz(pos.x, world_pos.y, 1.),
))
- Query the quadtree like bevy's
Or, Not:
type MyQuadTree = QuadTree<40, 100, 100, 20>;
fn pick(
mut gizmos: Gizmos,
btn: Res<ButtonInput<MouseButton>>,
key: Res<ButtonInput<KeyCode>>,
quadtree: Res<MyQuadTree>,
mut start: Local<Option<Vec2>>,
...
) {
if !btn.pressed(MouseButton::Left) {
*start = None;
return;
}
let world_position = ...;
let cancel_pick = key.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]);
match *start {
Some(start) => {
gizmos.rect_2d(
(start + world_pos) / 2.,
(start - world_pos).abs(),
if cancel_pick { RED } else { WHITE },
);
let res = if start.x > world_pos.x {
quadtree.query::<_, QOr<(Overlap, Contain)>>(&CollisionRect::from(
Rect::from_corners(start, world_pos),
))
} else {
quadtree
.query::<_, Contain>(&CollisionRect::from(Rect::from_corners(start, world_pos)))
};
if cancel_pick {
...
} else {
...
}
}
None => *start = Some(world_pos),
}
}
- However, you may need manually update collision shapes in some case
xx.observe(
|trigger: Trigger<TextRefreshEvent>,
mut q_box: Query<(&mut Sprite, &mut CollisionRect)>| {
if let Ok((mut s, mut c)) = q_box.get_mut(trigger.entity()) {
let ev = trigger.event();
let delta = Vec2::new(ev.width * FONT_WIDTH, (ev.height - 1.) * FONT_HEIGHT);
s.custom_size = Some(CUSTOM_SIZE + delta);
c.set_init_size(CUSTOM_SIZE + delta);
}
},
)
See this repo graph for more complete examples.
For more details, please refer to the Documentation
Roadmap
See the open issues for a full list of proposed features (and known issues).
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature)
- Commit your Changes (
git commit -m 'Add some AmazingFeature')
- Push to the Branch (
git push origin feature/AmazingFeature)
- Open a Pull Request
License
Distributed under the MIT License. See LICENSE.txt for more information.
Contact
Louis - 836250617@qq.com
Project Link: https://github.com/kingwingfly/bevy_quadtree
Acknowledgments