pub fn update_velocities(&mut self) {
        let boid_query = <(Read<Position>, Write<Velocity>)>::query().filter(tag::<Boid>());

        let mut neighbours = vec![];
        let mut pairs = vec![];
        let mut v_refs = vec![];

        for (position, velocity) in boid_query.iter(&mut self.world) {
            neighbours.push(BoidNeighbours::new(position.as_vec(), velocity.as_vec()));
            pairs.push((position.as_vec(), velocity.as_vec()));
            v_refs.push(velocity);
        }

        for (i, (position, velocity)) in pairs.iter().enumerate() {
            for (j, (other_position, other_velocity)) in pairs.iter().skip(i + 1).enumerate() {
                // compute the distance between these two boids
                let distance = (*position - *other_position).square_magnitude();

                if distance <= DISTANCE_THRESHOLD {
                    neighbours[i].update(*other_position, *other_velocity);
                    neighbours[i + j + 1].update(*position, *velocity);
                }
            }

            let v_direction = *velocity + update_boid_velocity(&neighbours[i]);
            let new_velocity = v_direction.normalize().unwrap() * VELOCITY;
            *v_refs[i] = Velocity::new(new_velocity.x, new_velocity.y);
        }
    }
/// Updates the current velocities of each boid in the world
Updates the current velocities of each boid in the world
Updates the current velocities of each boid in the world
pub fn update_velocities(&mut self) {
        let boid_query = <(Read<Position>, Write<Velocity>)>::query().filter(tag::<Boid>());
        let mut neighbours = vec![];
        let mut pairs = vec![];
        let mut v_refs = vec![];
        for (position, velocity) in boid_query.iter(&mut self.world) {
            neighbours.push(BoidNeighbours::new(position.as_vec(), velocity.as_vec()));
            pairs.push((position.as_vec(), velocity.as_vec()));
            v_refs.push(velocity);
        }
        for (i, (position, velocity)) in pairs.iter().enumerate() {
            for (j, (other_position, other_velocity)) in pairs.iter().skip(i + 1).enumerate() {
                // compute the distance between these two boids
                let distance = (*position - *other_position).square_magnitude();
                if distance <= DISTANCE_THRESHOLD {
                    neighbours[i].update(*other_position, *other_velocity);
                    neighbours[i + j + 1].update(*position, *velocity);
                }
            }
            let v_direction = *velocity + update_boid_velocity(&neighbours[i]);
            let new_velocity = v_direction.normalize().unwrap() * VELOCITY;
            *v_refs[i] = Velocity::new(new_velocity.x, new_velocity.y);
        }
    }
pub fn update_velocities ( & mut self ) { let boid_query = < ( Read < Position > , Write < Velocity > ) > :: query ( ) . filter ( tag :: < Boid > ( ) ) ; let mut neighbours = vec ! [ ] ; let mut pairs = vec ! [ ] ; let mut v_refs = vec ! [ ] ; for ( position , velocity ) in boid_query . iter ( & mut self . world ) { neighbours . push ( BoidNeighbours :: new ( position . as_vec ( ) , velocity . as_vec ( ) ) ) ; pairs . push ( ( position . as_vec ( ) , velocity . as_vec ( ) ) ) ; v_refs . push ( velocity ) ; } for ( i , ( position , velocity ) ) in pairs . iter ( ) . enumerate ( ) { for ( j , ( other_position , other_velocity ) ) in pairs . iter ( ) . skip ( i + 1 ) . enumerate ( ) { // compute the distance between these two boids let distance = ( * position - * other_position ) . square_magnitude ( ) ; if distance <= DISTANCE_THRESHOLD { neighbours [ i ] . update ( * other_position , * other_velocity ) ; neighbours [ i + j + 1 ] . update ( * position , * velocity ) ; } } let v_direction = * velocity + update_boid_velocity ( & neighbours [ i ] ) ; let new_velocity = v_direction . normalize ( ) . unwrap ( ) * VELOCITY ; * v_refs [ i ] = Velocity :: new ( new_velocity . x , new_velocity . y ) ; } }
Updates the current velocities of each boid in the world
Updates the current velocities of each boid in the world
&mut self
&mut self