swarms, but doesn't flock

This commit is contained in:
Joe Ardent 2023-11-25 14:46:43 -08:00
parent ed7b947f12
commit b51c75ddb8
2 changed files with 27 additions and 11 deletions

View file

@ -2,11 +2,14 @@ use argh::FromArgs;
use bevy::prelude::*; use bevy::prelude::*;
// toid // toid
const SPEED: f32 = 1.0; const SPEED: f32 = 10.0;
const SPEED_DIFF_RANGE: f32 = 0.08; // +/- 8% const SPEED_DIFF_RANGE: f32 = 0.08; // +/- 8%
// how far from origin before really wanting to come back // how far from origin before really wanting to come back
const RADIUS: f32 = 100.0; const RADIUS: f32 = 800.0;
// how close to try to stay to your buddies
const BUDDY_RADIUS: f32 = 15.0;
const MIN_ALTITUDE: f32 = 1.5; const MIN_ALTITUDE: f32 = 1.5;
@ -45,8 +48,12 @@ pub fn turkey_time(
let speed = SPEED + (SPEED * speed_diff); let speed = SPEED + (SPEED * speed_diff);
let vel = unit_vec(r) * speed; let vel = unit_vec(r) * speed;
let buddies = r.gen_range(6..=8); let buddies = r.gen_range(6..=8);
let spatial_bundle = SpatialBundle {
transform: Transform::from_xyz(0.0, MIN_ALTITUDE + 0.1, 0.0),
..Default::default()
};
commands commands
.spawn(SpatialBundle::default()) .spawn(spatial_bundle)
.insert((Velocity(vel), Buddies::default(), Toid { speed, buddies })) .insert((Velocity(vel), Buddies::default(), Toid { speed, buddies }))
.with_children(|t| { .with_children(|t| {
t.spawn(SceneBundle { t.spawn(SceneBundle {
@ -96,21 +103,30 @@ pub fn update_vel(
let dt = time.delta_seconds(); let dt = time.delta_seconds();
for (xform, mut vel, toid) in toids.iter_mut() { for (xform, mut vel, toid) in toids.iter_mut() {
let speed = toid.speed; let speed = toid.speed;
let mut dir = xform.forward(); let mut dir = vel.normalize();
let pos = xform.translation; let pos = xform.translation;
// nudge up if too low // nudge up if too low
if pos.y < MIN_ALTITUDE { if pos.y < MIN_ALTITUDE {
let dh = MIN_ALTITUDE - pos.y; let dh = MIN_ALTITUDE - pos.y;
let pct = (dh / MIN_ALTITUDE).min(1.0); let pct = (dh / MIN_ALTITUDE).min(1.0).powi(2);
let up = Quat::from_rotation_arc(dir, Vec3::Y); let rot = Quat::from_rotation_arc(dir, Vec3::Y);
let rot = xform.rotation.slerp(up, pct); let (axis, ang) = rot.to_axis_angle();
let rot = Quat::from_axis_angle(axis, ang * pct).normalize();
dir = rot.mul_vec3(dir); dir = rot.mul_vec3(dir);
} }
// nudge toward origin if too far // nudge toward origin if too far
let dist = pos.length(); {
let dist = pos.length();
let toward_origin = -pos.normalize();
let forward = dir.normalize();
let rot = Quat::from_rotation_arc(forward, toward_origin);
let pct = (dist / RADIUS).min(1.0);
let (axis, ang) = rot.to_axis_angle();
let rot = Quat::from_axis_angle(axis, ang * pct).normalize();
dir = rot.mul_vec3(dir);
}
// find buddies and orient: point more towards further-away buddies // find buddies and orient: point more towards further-away buddies
**vel = dir * speed; **vel = dir * speed;

View file

@ -15,7 +15,7 @@ fn main() {
Update, Update,
( (
update_vel, update_vel,
add_gizmos, //add_gizmos,
update_pos, update_pos,
update_buddies, update_buddies,
rotate_camera, rotate_camera,
@ -37,7 +37,7 @@ fn setup(
let rand = &mut rand::thread_rng(); let rand = &mut rand::thread_rng();
commands.spawn(Camera3dBundle { commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0., 3.5, 10.).looking_at(Vec3::ZERO, Vec3::Y), transform: Transform::from_xyz(0., 3.5, 40.).looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
..default() ..default()
}); });
// plane // plane