如何在Bevy系统中执行嵌套查询?

问题描述

我正在用Bevy制作一个小小的boids玩具,每个物体的速度/加速度取决于周围物体的位置和速度值。这意味着对于每个投标,我都希望运行一些依赖于其他投标的子集的逻辑。

这似乎基本上可以是嵌套的for循环:

for boid in boids {
    for other_boid in boids {
        if boid.id == other_boid.id {
            continue;
        }
        
        if boid.position.distance_to(other_boid.position) < perception_disTANCE {
            // change boid's veLocity / acceleration
        }
    }
}

但是,我不确定如何使用Bevy中的查询执行此操作。假设我有一个系统move_boids

fn move_boids(mut query: Query<&Boid>) {
    for boid in &mut query.iter() {
        // I can't iterate over *other* boids here
    }
}

我收到类似这样的错误,因为我在两个循环中都是可变地借用query

error[E0499]: cannot borrow `query` as mutable more than once at a time
  --> src\main.rs:10:32
   |
10 |     for boid in &mut query.iter() {
   |                      ------------
   |                      |          |
   |                      |          ... and the first borrow might be used here,when that temporary is dropped and runs the `Drop` code for type `bevy::bevy_ecs::system::query::QueryBorrow`
   |                      first mutable borrow occurs here
   |                      a temporary with access to the first borrow is created here ...
...
11 |         for other_boid in &mut query.iter() {}
   |                                ^^^^^ second mutable borrow occurs here

我无法在同一查询上进行嵌套迭代,因此我不确定获取有关每个投标周围投标的信息的最佳方法。我是否应该将第一个查询中每个主体的位置和速度信息复制到HashMap<Entity,BoidData>中,然后在其中进行查找?我还能做些惯用的事吗?

解决方法

所以我想出了一个非常微妙的答案,而且我不确定它是否可以很好地适用于投标。您尝试执行的操作由于在同一范围内有两个可变引用而无法使用。

我能够获得其他投标,并使用Vec<u128>的投标进行比较。我不会使用哈希图,因为我听说它们的查找速度较慢,但​​是我没有尝试过。

在我的示例中,我假设boid的数量恒定,但是您可以使用boid向量的长度。我还为示例制作了一个随机boid结构,并使用了rand。

const NUM_BOIDS: u32 = 10;
struct Boids(Vec<u128>);
struct Boid {
    position: f32,velocity: f32,}
fn main() {
    App::build()
        ...
        .add_resource(Boids(Vec::new()))
        ...
}

fn setup(mut commands: Commands,mut boids: ResMut<Boids>) {
    let mut rng = rand::thread_rng();
    for i in 0..10 {
        // create 10 boids,and add there id's to the boids vec
        if let Some(entity) = commands
            .spawn((Boid {
                position: rng.gen_range(0.0,100.0),velocity: rng.gen_range(0.0,25.0),},))
            .current_entity()
        {
            boids.0.push(entity.id());
        };
    }
}

fn get_boids(mut boids: ResMut<Boids>,mut query: Query<(Entity,&mut Boid)>) {
    // go through all the boid ids
    for (i,boid_id) in boids.0.iter().enumerate() {
        // go through a second time
        for j in 0..NUM_BOIDS {
            // this gets the next boid id unless its at the end
            let other_boid_id = match boids.0.get(j as usize..(j as usize + 1)) {
                Some(e) => e,None => {
                    continue;
                }
            };
            // skip if it is the same boid
            if *boid_id == other_boid_id[0] {
                continue;
            }
            
            // since you can't have two mutable references at the same time
            // this gets the positions so those can be compared
            // and if the statement is true,a mutable reference can be gotten again.
            let mut first_position = 0.0;
            {
                let en = Entity::from_id(*boid_id);
                let first_boid: RefMut<Boid> = query.get_mut::<Boid>(en).unwrap();
                first_position = first_boid.position.clone();
            }
            let mut second_position = 0.0;
            {
                let en2 = Entity::from_id(other_boid_id[0]);
                let second_boid: RefMut<Boid> = query.get_mut::<Boid>(en2).unwrap();
                second_position = second_boid.position.clone();
            }
            if first_position - second_position < 0.001 {
                let en = Entity::from_id(*boid_id);
                let mut first_boid: RefMut<Boid> = query.get_mut::<Boid>(en).unwrap();
                first_boid.velocity = 15.0;
            }
        }
    }
}