many balls

This commit is contained in:
Zynh0722 2024-02-16 23:34:02 -08:00
parent 76758fa2a8
commit ab92ac9430

View file

@ -7,32 +7,36 @@ use engine::{PhysicsEngine, PhysicsState};
use nalgebra::vector; use nalgebra::vector;
use nannou::prelude::*; use nannou::prelude::*;
use particle::Particle;
use rapier2d::{ use rapier2d::{
dynamics::{RigidBodyBuilder, RigidBodyHandle}, dynamics::{RigidBodyBuilder, RigidBodySet},
geometry::ColliderBuilder, geometry::{ColliderBuilder, ColliderSet},
}; };
const WINDOW_WIDTH: u32 = 512;
const WINDOW_HEIGHT: u32 = WINDOW_WIDTH;
const PARTICLE_COUNT: u32 = 200; const PARTICLE_COUNT: u32 = 200;
const PARTICLE_SIZE: f32 = 10.0; const PARTICLE_SIZE: f32 = 10.0;
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,
} }
fn fill_particles(app: &App, particles: &mut Vec<Particle>) { fn fill_particles(_app: &App, colliders: &mut ColliderSet, bodies: &mut RigidBodySet) {
let boundary = app.window_rect(); let boundary = _app.window_rect();
for _ in 0..PARTICLE_COUNT { for _ in 0..PARTICLE_COUNT {
let x = random_range(boundary.left(), boundary.right()); let x = random_range(-100., 100.);
let y = random_range(boundary.top(), boundary.bottom()); let y = random_range(-150., boundary.top());
let particle = Particle::new(Vec2::new(x, y)); /* Create the bouncing ball. */
let rigid_body = RigidBodyBuilder::dynamic()
particles.push(particle); .translation(vector![x, y])
.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);
} }
} }
@ -44,7 +48,7 @@ fn model(app: &App) -> Model {
// 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(512, 512) .size(WINDOW_WIDTH, WINDOW_HEIGHT)
.title("nannou") .title("nannou")
.view(view) // The function that will be called for presenting graphics to a frame. .view(view) // The function that will be called for presenting graphics to a frame.
.event(event) // The function that will be called when the window receives events. .event(event) // The function that will be called when the window receives events.
@ -65,41 +69,31 @@ fn model(app: &App) -> Model {
.build(); .build();
engine.state.colliders.insert(collider); engine.state.colliders.insert(collider);
/* Create the bouncing ball. */ // let mut particles = Vec::new();
let rigid_body = RigidBodyBuilder::dynamic()
.translation(vector![0.0, 10.0])
.build();
let collider = ColliderBuilder::ball(10.0).restitution(0.7).build();
let ball_body_handle = engine.state.bodies.insert(rigid_body);
engine
.state
.colliders
.insert_with_parent(collider, ball_body_handle, &mut engine.state.bodies);
let mut particles = Vec::new(); // fill_particles(app, &mut particles);
fill_particles(app, &mut engine.state.colliders, &mut engine.state.bodies);
fill_particles(app, &mut particles);
Model { Model {
_window, _window,
particles, // particles,
engine, 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
fn event(app: &App, model: &mut Model, event: WindowEvent) { fn event(app: &App, _model: &mut Model, event: WindowEvent) {
if let KeyReleased(Key::Escape) = event { if let KeyReleased(Key::Escape) = event {
app.quit() app.quit()
} }
if let Resized(_) = event { // if let Resized(_) = event {
model.particles.clear(); // model.particles.clear();
//
// fill_particles(app, &mut model.particles);
// }
fill_particles(app, &mut model.particles); // println!("{:?}", event);
}
println!("{:?}", event);
} }
// Update the state of your application here. By default, this gets called right before `view`. // Update the state of your application here. By default, this gets called right before `view`.