avoid the ground
This commit is contained in:
parent
5e64325eb6
commit
ed7b947f12
1 changed files with 26 additions and 8 deletions
34
src/lib.rs
34
src/lib.rs
|
@ -1,10 +1,14 @@
|
||||||
use argh::FromArgs;
|
use argh::FromArgs;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
// toid
|
||||||
const SPEED: f32 = 1.0;
|
const SPEED: f32 = 1.0;
|
||||||
const SPEED_DIFF_RANGE: f32 = 0.08; // +/- 8%
|
const SPEED_DIFF_RANGE: f32 = 0.08; // +/- 8%
|
||||||
|
|
||||||
const RADIUS: f32 = 1_000.0;
|
// how far from origin before really wanting to come back
|
||||||
|
const RADIUS: f32 = 100.0;
|
||||||
|
|
||||||
|
const MIN_ALTITUDE: f32 = 1.5;
|
||||||
|
|
||||||
pub type Point = [f32; 3];
|
pub type Point = [f32; 3];
|
||||||
pub type Toint = rstar::primitives::GeomWithData<Point, Entity>;
|
pub type Toint = rstar::primitives::GeomWithData<Point, Entity>;
|
||||||
|
@ -20,11 +24,8 @@ pub struct Config {
|
||||||
#[derive(Resource, Deref, DerefMut, Default)]
|
#[derive(Resource, Deref, DerefMut, Default)]
|
||||||
pub struct Index(pub rstar::RTree<Toint>);
|
pub struct Index(pub rstar::RTree<Toint>);
|
||||||
|
|
||||||
#[derive(Resource, Deref, DerefMut, Default)]
|
|
||||||
pub struct Positions(pub std::collections::HashMap<Entity, Vec3>);
|
|
||||||
|
|
||||||
#[derive(Component, Debug, Clone, Deref, DerefMut, Default)]
|
#[derive(Component, Debug, Clone, Deref, DerefMut, Default)]
|
||||||
pub struct Buddies(Vec<Entity>);
|
pub struct Buddies(Vec<Toint>);
|
||||||
|
|
||||||
#[derive(Component, Debug, Clone, Deref, DerefMut, Default)]
|
#[derive(Component, Debug, Clone, Deref, DerefMut, Default)]
|
||||||
pub struct Velocity(Vec3);
|
pub struct Velocity(Vec3);
|
||||||
|
@ -72,14 +73,20 @@ pub fn add_gizmos(mut gizmos: Gizmos, toids: Query<(&Transform, Entity), With<To
|
||||||
pub fn update_pos(
|
pub fn update_pos(
|
||||||
mut toids: Query<(&mut Transform, &Velocity, Entity)>,
|
mut toids: Query<(&mut Transform, &Velocity, Entity)>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
mut index: Res<Index>,
|
mut index: ResMut<Index>,
|
||||||
) {
|
) {
|
||||||
|
let mut positions = Vec::with_capacity(toids.iter().len());
|
||||||
|
|
||||||
let dt = time.delta_seconds();
|
let dt = time.delta_seconds();
|
||||||
for (mut xform, vel, _entity) in toids.iter_mut() {
|
for (mut xform, vel, entity) in toids.iter_mut() {
|
||||||
xform.translation += vel.0 * dt;
|
|
||||||
let look_at = xform.translation + vel.0;
|
let look_at = xform.translation + vel.0;
|
||||||
|
xform.translation += vel.0 * dt;
|
||||||
xform.look_at(look_at, Vec3::Y);
|
xform.look_at(look_at, Vec3::Y);
|
||||||
|
let toint = Toint::new(xform.translation.to_array(), entity);
|
||||||
|
positions.push(toint);
|
||||||
}
|
}
|
||||||
|
let tree = rstar::RTree::bulk_load(positions);
|
||||||
|
**index = tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_vel(
|
pub fn update_vel(
|
||||||
|
@ -90,12 +97,23 @@ pub fn update_vel(
|
||||||
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 = xform.forward();
|
||||||
|
let pos = xform.translation;
|
||||||
|
|
||||||
// nudge up if too low
|
// nudge up if too low
|
||||||
|
if pos.y < MIN_ALTITUDE {
|
||||||
|
let dh = MIN_ALTITUDE - pos.y;
|
||||||
|
let pct = (dh / MIN_ALTITUDE).min(1.0);
|
||||||
|
let up = Quat::from_rotation_arc(dir, Vec3::Y);
|
||||||
|
let rot = xform.rotation.slerp(up, pct);
|
||||||
|
dir = rot.mul_vec3(dir);
|
||||||
|
}
|
||||||
|
|
||||||
// nudge toward origin if too far
|
// nudge toward origin if too far
|
||||||
|
let dist = pos.length();
|
||||||
|
|
||||||
// find buddies and orient: point more towards further-away buddies
|
// find buddies and orient: point more towards further-away buddies
|
||||||
|
|
||||||
|
**vel = dir * speed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue