better window bounding boxes

I also switch to using generalized Rect instead of an app for places where i was using the window boundary
main
Zynh0722 2024-02-17 02:38:20 -08:00
parent 4c36162aa5
commit e259bf6019
1 changed files with 11 additions and 8 deletions

View File

@ -18,7 +18,7 @@ const PARTICLE_SIZE: f32 = 5.;
struct Model { struct Model {
// Store the window ID so we can refer to this specific window later if needed. // Store the window ID so we can refer to this specific window later if needed.
_window: WindowId, window: WindowId,
// particles: Vec<Particle>, // particles: Vec<Particle>,
engine: PhysicsEngine, engine: PhysicsEngine,
} }
@ -30,11 +30,10 @@ fn random_vec_in_rect(boundary: &Rect) -> nalgebra::Vector2<f32> {
vector![x, y] 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 // Keeping track of already placed balls to avoid overlap
// Need to look into a way to do this with rapier directly // Need to look into a way to do this with rapier directly
let mut positions: Vec<nalgebra::Vector2<f32>> = Vec::new(); let mut positions: Vec<nalgebra::Vector2<f32>> = Vec::new();
let boundary = &app.window_rect();
for _ in 0..PARTICLE_COUNT { for _ in 0..PARTICLE_COUNT {
let mut xy = random_vec_in_rect(boundary); let mut xy = random_vec_in_rect(boundary);
@ -62,7 +61,7 @@ fn model(app: &App) -> Model {
// per app // per app
// Create a new window! Store the ID so we can refer to it later. // Create a new window! Store the ID so we can refer to it later.
let _window = app let window = app
.new_window() .new_window()
.size(WINDOW_WIDTH, WINDOW_HEIGHT) .size(WINDOW_WIDTH, WINDOW_HEIGHT)
.title("nannou") .title("nannou")
@ -79,7 +78,7 @@ fn model(app: &App) -> Model {
..Default::default() ..Default::default()
}; };
let boundary = app.window_rect(); let boundary = app.window(window).unwrap().rect();
/* Create the ground. */ /* Create the ground. */
let collider = ColliderBuilder::cuboid(boundary.w(), 4.) let collider = ColliderBuilder::cuboid(boundary.w(), 4.)
@ -103,9 +102,13 @@ fn model(app: &App) -> Model {
.build(); .build();
engine.state.colliders.insert(collider); 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 // 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( fill_particles(
app, &app.window(model.window).unwrap().rect(),
&mut model.engine.state.colliders, &mut model.engine.state.colliders,
&mut model.engine.state.bodies, &mut model.engine.state.bodies,
); );