no more overlapping balls!
This commit is contained in:
parent
be7c6763f0
commit
b7a5277a11
1 changed files with 23 additions and 6 deletions
29
src/main.rs
29
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<f32> {
|
||||
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<nalgebra::Vector2<f32>> = 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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue