updated 13 to not show ants

main
annieversary 2021-09-04 21:45:10 +02:00
parent d333c9c9d6
commit 858ed70ab4
1 changed files with 34 additions and 29 deletions

View File

@ -1,5 +1,4 @@
use nannou::prelude::*;
use nannou::rand::seq::SliceRandom;
use utils::*;
fn main() {
@ -21,21 +20,27 @@ fn model(_app: &App) -> Model {
fn update(app: &App, model: &mut Model, _update: Update) {
let _t = app.elapsed_frames() as f32 / 60.0;
let mut clusters = model.clustered.clone();
clusters.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap());
// sort them by x to be a bit more efficient with the collision detection
model
.clustered
.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap());
let clusters = model.clustered.clone();
let mut to_remove = vec![];
for (i, p) in &mut model.walkers.iter_mut().enumerate() {
*p += vec2_range(-1.0, 1.0) * 3.5;
*p += vec2_range(-1.0, 1.0) * 3.5 + 0.5 * vec2(p.y, -p.x).normalize();
const RAD: f32 = 6.0;
for &c in &clusters {
if c.x > p.x + RAD {
break;
}
// skip the ones outside the x range
if c.x < p.x - RAD {
continue;
}
if c.x > p.x + RAD {
break;
}
// if it's colliding, make it be a clustered point
if p.distance_squared(c) < RAD * RAD {
model.clustered.push(*p);
to_remove.push(i);
@ -44,6 +49,8 @@ fn update(app: &App, model: &mut Model, _update: Update) {
}
}
// remove the ones that have become clustered
// backwards cause otherwise you shift the indexes
for i in to_remove.into_iter().rev() {
model.walkers.remove(i);
}
@ -54,35 +61,33 @@ fn view(app: &App, model: &Model, frame: Frame) {
let draw = app.draw();
drawing::draw_soft_bg(&draw, app, SNOW, 0.03);
drawing::draw_soft_bg(&draw, app, SNOW, 1.0);
for &p in &model.walkers {
draw.ellipse().xy(p).radius(1.0).color(BLACK);
}
for &p in &model.clustered {
let v = map_sin(t + (p.x.powi(2) + p.y.powi(2) + t) * 0.00003, -20.0, 20.0);
let h = (100.0 + v) / 360.0;
draw.ellipse()
.xy(p)
.radius(4.0)
.color(hsla(h, 0.5, 0.5, 0.3));
draw.ellipse().xy(p).radius(4.0).color(hsl(h, 0.5, 0.5));
}
let mut rng = nannou::rand::thread_rng();
let n = (frame.nth() as f32 * 0.7) as usize;
for _ in 0..n {
let a = model.clustered.choose(&mut rng);
let b = model.clustered.choose(&mut rng);
if let (Some(&a), Some(&b)) = (a, b) {
let v = map_sin(t * 2.0 + a.x * a.y, -20.0, 20.0);
let h = (100.0 + v) / 360.0;
draw.line()
.stroke_weight(3.0)
.points(a, b)
.color(hsla(h, 0.5, 0.5, 0.04));
}
for &p in &model.walkers {
draw.ellipse().xy(p).radius(0.0).color(BLACK);
}
// use nannou::rand::seq::SliceRandom;
// let mut rng = nannou::rand::thread_rng();
// let n = (frame.nth() as f32 * 0.7) as usize;
// for _ in 0..n {
// let a = model.clustered.choose(&mut rng);
// let b = model.clustered.choose(&mut rng);
// if let (Some(&a), Some(&b)) = (a, b) {
// let v = map_sin(t * 2.0 + a.x * a.y, -20.0, 20.0);
// let h = (100.0 + v) / 360.0;
// draw.line()
// .stroke_weight(3.0)
// .points(a, b)
// .color(hsla(h, 0.5, 0.5, 0.04));
// }
// }
draw.to_frame(app, &frame).unwrap();
utils::record::record(app, &frame);