diff --git a/src/main.rs b/src/main.rs index d5b083c..d392aac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use engine::{PhysicsEngine, PhysicsState}; use nalgebra::vector; use nannou::prelude::*; use rapier2d::{ - dynamics::{RigidBodyBuilder, RigidBodySet}, + dynamics::{RigidBodyBuilder, RigidBodyHandle, RigidBodySet}, geometry::{ColliderBuilder, ColliderSet}, }; @@ -82,16 +82,37 @@ fn model(app: &App) -> Model { } // Handle events related to the window and update the model if necessary -fn event(app: &App, _model: &mut Model, event: WindowEvent) { +fn event(app: &App, model: &mut Model, event: WindowEvent) { if let KeyReleased(Key::Escape) = event { app.quit() } - // if let Resized(_) = event { - // model.particles.clear(); - // - // fill_particles(app, &mut model.particles); - // } + if let Resized(_) = event { + let handles: Vec = model + .engine + .state + .bodies + .iter() + .map(|(handle, _)| handle) + .collect(); + + for handle in handles { + model.engine.state.bodies.remove( + handle, + &mut model.engine.state.islands, + &mut model.engine.state.colliders, + &mut model.engine.state.joints, + &mut model.engine.state.multibody_joints, + true, + ); + } + + fill_particles( + app, + &mut model.engine.state.colliders, + &mut model.engine.state.bodies, + ); + } // println!("{:?}", event); } diff --git a/src/particle.rs b/src/particle.rs deleted file mode 100644 index 9dd208e..0000000 --- a/src/particle.rs +++ /dev/null @@ -1,43 +0,0 @@ -use crate::drawable::Drawable; -use crate::PARTICLE_SIZE; - -use nannou::prelude::*; - -#[derive(Debug)] -pub struct Particle { - pub pos: Vec2, - pub start_pos: Vec2, - pub radius: f32, - pub time_offset: f32, -} - -impl Particle { - pub fn new(pos: Vec2) -> Self { - Self { - pos, - start_pos: pos, - ..Default::default() - } - } -} - -impl Default for Particle { - fn default() -> Self { - Self { - pos: Default::default(), - radius: PARTICLE_SIZE, - start_pos: Default::default(), - time_offset: random_range(-PI, PI), - } - } -} - -impl Drawable for Particle { - fn draw(&self, draw: &Draw) { - draw.ellipse() - .color(RED) - .stroke_weight(1.0) - .radius(self.radius) - .xy(self.pos); - } -}