From e259bf601932b7525bfadfae1557fb8f37d655e0 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Sat, 17 Feb 2024 02:38:20 -0800 Subject: [PATCH] better window bounding boxes I also switch to using generalized Rect instead of an app for places where i was using the window boundary --- src/main.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index f8a38d0..32edd06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ const PARTICLE_SIZE: f32 = 5.; struct Model { // Store the window ID so we can refer to this specific window later if needed. - _window: WindowId, + window: WindowId, // particles: Vec, engine: PhysicsEngine, } @@ -30,11 +30,10 @@ fn random_vec_in_rect(boundary: &Rect) -> nalgebra::Vector2 { vector![x, y] } -fn fill_particles(app: &App, colliders: &mut ColliderSet, bodies: &mut RigidBodySet) { +fn fill_particles(boundary: &Rect, colliders: &mut ColliderSet, bodies: &mut RigidBodySet) { // Keeping track of already placed balls to avoid overlap // Need to look into a way to do this with rapier directly let mut positions: Vec> = Vec::new(); - let boundary = &app.window_rect(); for _ in 0..PARTICLE_COUNT { let mut xy = random_vec_in_rect(boundary); @@ -62,7 +61,7 @@ fn model(app: &App) -> Model { // per app // Create a new window! Store the ID so we can refer to it later. - let _window = app + let window = app .new_window() .size(WINDOW_WIDTH, WINDOW_HEIGHT) .title("nannou") @@ -79,7 +78,7 @@ fn model(app: &App) -> Model { ..Default::default() }; - let boundary = app.window_rect(); + let boundary = app.window(window).unwrap().rect(); /* Create the ground. */ let collider = ColliderBuilder::cuboid(boundary.w(), 4.) @@ -103,9 +102,13 @@ fn model(app: &App) -> Model { .build(); engine.state.colliders.insert(collider); - fill_particles(app, &mut engine.state.colliders, &mut engine.state.bodies); + fill_particles( + &boundary, + &mut engine.state.colliders, + &mut engine.state.bodies, + ); - Model { _window, engine } + Model { window, engine } } // Handle events related to the window and update the model if necessary @@ -137,7 +140,7 @@ fn event(app: &App, model: &mut Model, event: WindowEvent) { } fill_particles( - app, + &app.window(model.window).unwrap().rect(), &mut model.engine.state.colliders, &mut model.engine.state.bodies, );