diff --git a/src/main.rs b/src/main.rs index 994cb0a..953454e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use rapier2d::{ const WINDOW_WIDTH: u32 = 512; const WINDOW_HEIGHT: u32 = WINDOW_WIDTH; const PARTICLE_COUNT: u32 = 200; +const PARTICLE_SIZE: f32 = 5.; struct Model { // Store the window ID so we can refer to this specific window later if needed. @@ -22,17 +23,33 @@ struct Model { engine: PhysicsEngine, } -fn fill_particles(_app: &App, colliders: &mut ColliderSet, bodies: &mut RigidBodySet) { +fn random_vec(_app: &App) -> nalgebra::Vector2 { let boundary = _app.window_rect(); + + let x = random_range(-100., 100.); + let y = random_range(-150., boundary.top()); + + vector![x, y] +} + +fn fill_particles(app: &App, colliders: &mut ColliderSet, bodies: &mut RigidBodySet) { + let mut positions: Vec> = Vec::new(); + for _ in 0..PARTICLE_COUNT { - let x = random_range(-100., 100.); - let y = random_range(-150., boundary.top()); + let mut xy = random_vec(app); + while !positions + .iter() + .all(|pos| pos.metric_distance(&xy) > PARTICLE_SIZE * 2.) + { + xy = random_vec(app) + } + positions.push(xy); /* Create the bouncing ball. */ - let rigid_body = RigidBodyBuilder::dynamic() - .translation(vector![x, y]) + let rigid_body = RigidBodyBuilder::dynamic().translation(xy).build(); + let collider = ColliderBuilder::ball(PARTICLE_SIZE) + .restitution(0.1) .build(); - let collider = ColliderBuilder::ball(5.).restitution(0.1).build(); let ball_body_handle = bodies.insert(rigid_body); colliders.insert_with_parent(collider, ball_body_handle, bodies); }