| Crates.io | bevy_quadtree |
| lib.rs | bevy_quadtree |
| version | 0.16.0 |
| created_at | 2025-01-19 16:18:46.680484+00 |
| updated_at | 2025-05-01 14:12:28.49694+00 |
| description | A quadtree plugin for bevy |
| homepage | |
| repository | https://github.com/kingwingfly/bevy_quadtree |
| max_upload_size | |
| id | 1522959 |
| size | 331,990 |
A quadtree plugin for Bevy
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
A quadtree plugin for bevy.
Function:
Changed<GlobalTransform>, all users need to do is querying.Features:
gizmos: show gizmos of the quadtree boundaries.sprite: enable CollisionRect and CollisionRotatedRect to track sprite.custom_size.multi-quadtree: support multiple quadtrees in one world, see [MultiQuadTreePlugin].Version:
To align with Bevy, the version is always the same as supported Bevy's version.
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.
For those who upgrade from version <= 0.15.1-beta.2, X Y type params added to Plugins to set the origin of boundary.
The QuadTree's type params is simplified to only ID as well.
Cargo.toml:[dependencies]
bevy_quadtree = { version = "0.16.0" }
# #[cfg(feature = "sprite")]
{
# use bevy_app::prelude::*;
# use bevy_transform::prelude::*;
# use bevy_sprite::Sprite;
use bevy_quadtree::{QuadTreePlugin, CollisionCircle, CollisionRect};
fn main() {
App::new()
.add_plugins(QuadTreePlugin::<(
(CollisionCircle, GlobalTransform), (CollisionRect, (GlobalTransform, Sprite)),
),
40, 8, 100, 100, 0, 0, 20, 114514>::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, center at (0, 0)
// 20 / 10 = 2.0 = outlet_boundary / inlet_boundary (for loose quadtree)
// quadtree id 114514
.run();
}
# }
// 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, pos.y, 1.),
))
Or, Not:// default id 0
type MyQuadTree = QuadTree<114514>;
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),
}
}
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
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!
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)Distributed under the MIT License. See LICENSE.txt for more information.
Louis - 836250617@qq.com
Project Link: https://github.com/kingwingfly/bevy_quadtree