Misc
This commit is contained in:
37
src/boids.rs
37
src/boids.rs
@@ -20,24 +20,30 @@ fn simulate_boids(time: Res<Time>, mut boids: Query<(&Transform, &mut Vel), With
|
|||||||
//Match Speed
|
//Match Speed
|
||||||
let dist = a.0.translation - b.0.translation;
|
let dist = a.0.translation - b.0.translation;
|
||||||
let d = dist.length();
|
let d = dist.length();
|
||||||
if d <= 100. {
|
if d <= 200. {
|
||||||
let avg_vel = (a.1.value + b.1.value) / 2.;
|
let avg_vel = (a.1.value + b.1.value) / 2.;
|
||||||
a_total_vel += ((avg_vel - a_total_vel) / 8.) * time.delta_seconds();
|
a_total_vel += ((avg_vel - a_total_vel) / 8.) * time.delta_seconds();
|
||||||
b_total_vel -= ((avg_vel - b_total_vel) / 8.) * time.delta_seconds();
|
b_total_vel -= ((avg_vel - b_total_vel) / 8.) * time.delta_seconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Collision Avoidance
|
//Collision Avoidance
|
||||||
if d < 15. {
|
if d < 10. {
|
||||||
a_total_vel += dist * time.elapsed_seconds();
|
a_total_vel += dist * time.elapsed_seconds();
|
||||||
b_total_vel += -dist * time.elapsed_seconds();
|
b_total_vel += -dist * time.elapsed_seconds();
|
||||||
} else {
|
}
|
||||||
|
if d < 200. {
|
||||||
//Flocking
|
//Flocking
|
||||||
// let avg_pos = (a.0.translation + b.0.translation) / 2.;
|
let avg_pos = (a.0.translation + b.0.translation) / 2.;
|
||||||
// a_total_vel += (avg_pos - a.0.translation) * 0.0001 * time.elapsed_seconds();
|
a_total_vel += tend_to_point(a.0.translation, avg_pos) * 0.008 * time.elapsed_seconds();
|
||||||
// b_total_vel += (avg_pos - b.0.translation) * 0.0001 * time.elapsed_seconds();
|
b_total_vel += tend_to_point(b.0.translation, avg_pos) * 0.008 * time.elapsed_seconds();
|
||||||
}
|
}
|
||||||
|
|
||||||
// println!("{}", a_total_vel.length());
|
// println!("{}", a_total_vel.length());
|
||||||
|
//Forward Vel
|
||||||
|
// let speed = 20.;
|
||||||
|
// a_total_vel += a.0.up() * speed * time.delta_seconds();
|
||||||
|
// b_total_vel += b.0.up() * speed * time.delta_seconds();
|
||||||
|
|
||||||
a.1.value = a_total_vel;
|
a.1.value = a_total_vel;
|
||||||
b.1.value = b_total_vel;
|
b.1.value = b_total_vel;
|
||||||
}
|
}
|
||||||
@@ -45,12 +51,14 @@ fn simulate_boids(time: Res<Time>, mut boids: Query<(&Transform, &mut Vel), With
|
|||||||
for (t, mut v) in &mut boids {
|
for (t, mut v) in &mut boids {
|
||||||
//Tend to Center
|
//Tend to Center
|
||||||
let mut d = t.translation.length();
|
let mut d = t.translation.length();
|
||||||
if d > 500. {
|
let max_d = 500.;
|
||||||
d -= 500.;
|
if d > max_d {
|
||||||
d /= 1000.;
|
d -= max_d;
|
||||||
v.value += -t.translation * time.elapsed_seconds() * remap(d, 0., 1., 0., 0.03);
|
d /= max_d;
|
||||||
|
v.value += tend_to_point(t.translation, Vec3::ZERO)
|
||||||
|
* time.elapsed_seconds()
|
||||||
|
* d.remap(0., 1., 0., 0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Limit Velocity
|
//Limit Velocity
|
||||||
if v.value == Vec3::ZERO {
|
if v.value == Vec3::ZERO {
|
||||||
continue;
|
continue;
|
||||||
@@ -59,8 +67,9 @@ fn simulate_boids(time: Res<Time>, mut boids: Query<(&Transform, &mut Vel), With
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remap(value: f32, low1: f32, high1: f32, low2: f32, high2: f32) -> f32 {
|
fn tend_to_point(from: Vec3, to: Vec3) -> Vec3 {
|
||||||
return low2 + (value - low1) * (high2 - low2) / (high1 - low1);
|
let diff = to - from;
|
||||||
|
return diff.normalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn limit_velocity(v: &Vel) -> Vec3 {
|
fn limit_velocity(v: &Vel) -> Vec3 {
|
||||||
@@ -88,7 +97,7 @@ fn init(
|
|||||||
) {
|
) {
|
||||||
commands.spawn(Camera2dBundle::default());
|
commands.spawn(Camera2dBundle::default());
|
||||||
|
|
||||||
let size = 50;
|
let size = 25;
|
||||||
|
|
||||||
for x in 0..size {
|
for x in 0..size {
|
||||||
for y in 0..size {
|
for y in 0..size {
|
||||||
|
|||||||
Reference in New Issue
Block a user