no more overlapping balls!

This commit is contained in:
Zynh0722 2024-02-17 02:12:04 -08:00
parent be7c6763f0
commit b7a5277a11

View file

@ -14,6 +14,7 @@ use rapier2d::{
const WINDOW_WIDTH: u32 = 512; const WINDOW_WIDTH: u32 = 512;
const WINDOW_HEIGHT: u32 = WINDOW_WIDTH; const WINDOW_HEIGHT: u32 = WINDOW_WIDTH;
const PARTICLE_COUNT: u32 = 200; const PARTICLE_COUNT: u32 = 200;
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.
@ -22,17 +23,33 @@ struct Model {
engine: PhysicsEngine, engine: PhysicsEngine,
} }
fn fill_particles(_app: &App, colliders: &mut ColliderSet, bodies: &mut RigidBodySet) { fn random_vec(_app: &App) -> nalgebra::Vector2<f32> {
let boundary = _app.window_rect(); let boundary = _app.window_rect();
for _ in 0..PARTICLE_COUNT {
let x = random_range(-100., 100.); let x = random_range(-100., 100.);
let y = random_range(-150., boundary.top()); 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<nalgebra::Vector2<f32>> = Vec::new();
for _ in 0..PARTICLE_COUNT {
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. */ /* Create the bouncing ball. */
let rigid_body = RigidBodyBuilder::dynamic() let rigid_body = RigidBodyBuilder::dynamic().translation(xy).build();
.translation(vector![x, y]) let collider = ColliderBuilder::ball(PARTICLE_SIZE)
.restitution(0.1)
.build(); .build();
let collider = ColliderBuilder::ball(5.).restitution(0.1).build();
let ball_body_handle = bodies.insert(rigid_body); let ball_body_handle = bodies.insert(rigid_body);
colliders.insert_with_parent(collider, ball_body_handle, bodies); colliders.insert_with_parent(collider, ball_body_handle, bodies);
} }