updated 13 to not show ants
parent
d333c9c9d6
commit
858ed70ab4
|
@ -1,5 +1,4 @@
|
||||||
use nannou::prelude::*;
|
use nannou::prelude::*;
|
||||||
use nannou::rand::seq::SliceRandom;
|
|
||||||
use utils::*;
|
use utils::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -21,21 +20,27 @@ fn model(_app: &App) -> Model {
|
||||||
fn update(app: &App, model: &mut Model, _update: Update) {
|
fn update(app: &App, model: &mut Model, _update: Update) {
|
||||||
let _t = app.elapsed_frames() as f32 / 60.0;
|
let _t = app.elapsed_frames() as f32 / 60.0;
|
||||||
|
|
||||||
let mut clusters = model.clustered.clone();
|
// sort them by x to be a bit more efficient with the collision detection
|
||||||
clusters.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap());
|
model
|
||||||
|
.clustered
|
||||||
|
.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap());
|
||||||
|
let clusters = model.clustered.clone();
|
||||||
|
|
||||||
let mut to_remove = vec![];
|
let mut to_remove = vec![];
|
||||||
for (i, p) in &mut model.walkers.iter_mut().enumerate() {
|
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;
|
const RAD: f32 = 6.0;
|
||||||
for &c in &clusters {
|
for &c in &clusters {
|
||||||
if c.x > p.x + RAD {
|
// skip the ones outside the x range
|
||||||
break;
|
|
||||||
}
|
|
||||||
if c.x < p.x - RAD {
|
if c.x < p.x - RAD {
|
||||||
continue;
|
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 {
|
if p.distance_squared(c) < RAD * RAD {
|
||||||
model.clustered.push(*p);
|
model.clustered.push(*p);
|
||||||
to_remove.push(i);
|
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() {
|
for i in to_remove.into_iter().rev() {
|
||||||
model.walkers.remove(i);
|
model.walkers.remove(i);
|
||||||
}
|
}
|
||||||
|
@ -54,35 +61,33 @@ fn view(app: &App, model: &Model, frame: Frame) {
|
||||||
|
|
||||||
let draw = app.draw();
|
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 {
|
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 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;
|
let h = (100.0 + v) / 360.0;
|
||||||
draw.ellipse()
|
draw.ellipse().xy(p).radius(4.0).color(hsl(h, 0.5, 0.5));
|
||||||
.xy(p)
|
|
||||||
.radius(4.0)
|
|
||||||
.color(hsla(h, 0.5, 0.5, 0.3));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut rng = nannou::rand::thread_rng();
|
for &p in &model.walkers {
|
||||||
let n = (frame.nth() as f32 * 0.7) as usize;
|
draw.ellipse().xy(p).radius(0.0).color(BLACK);
|
||||||
for _ in 0..n {
|
}
|
||||||
let a = model.clustered.choose(&mut rng);
|
// use nannou::rand::seq::SliceRandom;
|
||||||
let b = model.clustered.choose(&mut rng);
|
// let mut rng = nannou::rand::thread_rng();
|
||||||
if let (Some(&a), Some(&b)) = (a, b) {
|
// let n = (frame.nth() as f32 * 0.7) as usize;
|
||||||
let v = map_sin(t * 2.0 + a.x * a.y, -20.0, 20.0);
|
// for _ in 0..n {
|
||||||
let h = (100.0 + v) / 360.0;
|
// 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()
|
// draw.line()
|
||||||
.stroke_weight(3.0)
|
// .stroke_weight(3.0)
|
||||||
.points(a, b)
|
// .points(a, b)
|
||||||
.color(hsla(h, 0.5, 0.5, 0.04));
|
// .color(hsla(h, 0.5, 0.5, 0.04));
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
draw.to_frame(app, &frame).unwrap();
|
draw.to_frame(app, &frame).unwrap();
|
||||||
utils::record::record(app, &frame);
|
utils::record::record(app, &frame);
|
||||||
|
|
Loading…
Reference in New Issue