From 858ed70ab4ac6e6914098c98820b9925b729caeb Mon Sep 17 00:00:00 2001 From: annieversary Date: Sat, 4 Sep 2021 21:45:10 +0200 Subject: [PATCH] updated 13 to not show ants --- crates/subtitled13/src/main.rs | 63 ++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/crates/subtitled13/src/main.rs b/crates/subtitled13/src/main.rs index 2d084f5..88ca398 100644 --- a/crates/subtitled13/src/main.rs +++ b/crates/subtitled13/src/main.rs @@ -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);