diff --git a/src/main.rs b/src/main.rs index 7958cb8..0cb98c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,6 +28,10 @@ fn fill_particles(app: &App, particles: &mut Vec) { } fn model(app: &App) -> Model { + // One thing that tripped me up when begginning nannou was realizing + // that view and event methods are per window, and update functions are + // per app + // Create a new window! Store the ID so we can refer to it later. let _window = app .new_window() @@ -61,7 +65,12 @@ fn event(app: &App, model: &mut Model, event: WindowEvent) { } // Update the state of your application here. By default, this gets called right before `view`. -fn update(_app: &App, _model: &mut Model, _update: Update) {} +fn update(_app: &App, _model: &mut Model, _update: Update) { + _model.particles.iter_mut().for_each(|particle| { + let offset = (_app.time + particle.time_offset).sin() * 100.0; + particle.pos.x = particle.start_pos.x + offset + }); +} // Draw the state of your `Model` into the given `Frame` here. fn view(app: &App, model: &Model, frame: Frame) { diff --git a/src/particle.rs b/src/particle.rs index f34d181..9dd208e 100644 --- a/src/particle.rs +++ b/src/particle.rs @@ -4,15 +4,18 @@ use crate::PARTICLE_SIZE; use nannou::prelude::*; #[derive(Debug)] -pub(crate) struct Particle { - pos: Vec2, - radius: f32, +pub struct Particle { + pub pos: Vec2, + pub start_pos: Vec2, + pub radius: f32, + pub time_offset: f32, } impl Particle { - pub(crate) fn new(pos: Vec2) -> Self { + pub fn new(pos: Vec2) -> Self { Self { pos, + start_pos: pos, ..Default::default() } } @@ -23,6 +26,8 @@ impl Default for Particle { Self { pos: Default::default(), radius: PARTICLE_SIZE, + start_pos: Default::default(), + time_offset: random_range(-PI, PI), } } }