首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Bevy系统中执行嵌套查询?

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

Stack Overflow用户
提问于 2020-09-07 03:43:46
回答 1查看 736关注 0票数 4

我正在用Bevy做一个小的boids玩具,每个boid的速度/加速度取决于它周围boid的位置和速度值。这意味着对于每个boid,我想运行一些依赖于其他boid的子集的逻辑。

这看起来像是一个嵌套的for循环:

代码语言:javascript
复制
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

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

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

代码语言:javascript
复制
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

我不能对同一查询进行嵌套迭代,因此我不确定获取每个boid周围boid信息的最佳方法。我是否应该将第一个查询中的每个boid的位置和速度信息复制到一个HashMap<Entity, BoidData>中,然后在其中进行查找?我还能做些什么更地道的事吗?

EN

回答 1

Stack Overflow用户

发布于 2020-09-10 06:37:05

所以我想出了一个非常不优雅的答案,我不确定它是否适用于boids。由于在同一个作用域中有两个可变引用,所以您尝试做的事情将不起作用。

我能够获得其他boids,并使用ids的Vec<u128>对它们进行比较。我不会使用hashmap,因为我听说他们有较慢的查找速度,但我还没有尝试过。

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

代码语言:javascript
复制
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;
            }
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63768244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档