A quadtree plugin for Bevy
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
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
: enableCollisionRect
andCollisionRotatedRect
to tracksprite.custom_size
.
For those who upgrade from version <= 0.15.1-alpha7, pay attention to the new type paramter D
in QuadTreePlugin
.
And shapes now have ID
as well. Moreover, the tree memory is pre-allocated, no longer dynamically allocating.
- Add the plugin to your
Cargo.toml
:
[dependencies]
bevy_quadtree = { version = "0.15.1" }
- Add the plugin to your Bevy app:
# use bevy_app::prelude::*;
# use bevy_transform::prelude::*;
# #[cfg(feature = "sprite")]
# use bevy_sprite::Sprite;
use bevy_quadtree::{QuadTreePlugin, CollisionCircle, CollisionRect};
fn main() {
#[cfg(feature = "sprite")]
App::new()
.add_plugins(QuadTreePlugin::<(
(CollisionCircle, GlobalTransform), (CollisionRect, (GlobalTransform, Sprite)),
),
8, 40, 100, 100, 20>::default()
)
// CollisionCircle follows GlobalTransform, CollisionRect follows Sprite and GlobalTransform
// at most 40 entities in a node
// at most 8 levels
// 100 x 100 world size
// 20 / 10 = 2 = outlet_boundary / inlet_boundary (for loose quadtree)
.run();
}
- Spawn CollisionShapes as Components:
// in systems
cmds.spawn((
Sprite {
color: Color::WHITE,
custom_size: Some(CUSTOM_SIZE),
..Default::default()
},
// Spawn collision shape `CollisionRect` with `Sprite`,
// the plugin will auto-update it following `Changed<GlobalTransform>` and `Changed<Sprite>`
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 {
// left pick
quadtree.query::<QOr<(Overlap, Contain)>>(&CollisionRect::from(
Rect::from_corners(start, world_pos),
))
} else {
// right pick
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);
// the plugin default only track `Changed<GlobalTransform>`
// without feature `sprite` enabled, you can also do this way.
c.set_init_size(CUSTOM_SIZE + delta);
}
},
)
See this repo graph for more complete examples.
For more details, please refer to the Documentation
- Feature
See the open issues for a full list of proposed features (and known issues).
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
Distributed under the MIT License. See LICENSE.txt
for more information.
Louis - [email protected]
Project Link: https://github.com/kingwingfly/bevy_quadtree