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 nannou::prelude::*;
use particle::Particle;
use rapier2d::{
dynamics::{RigidBodyBuilder, RigidBodyHandle},
geometry::ColliderBuilder,
dynamics::{RigidBodyBuilder, RigidBodySet},
geometry::{ColliderBuilder, ColliderSet},
};
const WINDOW_WIDTH: u32 = 512;
const WINDOW_HEIGHT: u32 = WINDOW_WIDTH;
const PARTICLE_COUNT: u32 = 200;
const PARTICLE_SIZE: f32 = 10.0;
struct Model {
// Store the window ID so we can refer to this specific window later if needed.
_window: WindowId,
particles: Vec<Particle>,
// particles: Vec<Particle>,
engine: PhysicsEngine,
}
fn fill_particles(app: &App, particles: &mut Vec<Particle>) {
let boundary = app.window_rect();
fn fill_particles(_app: &App, colliders: &mut ColliderSet, bodies: &mut RigidBodySet) {
let boundary = _app.window_rect();
for _ in 0..PARTICLE_COUNT {
let x = random_range(boundary.left(), boundary.right());
let y = random_range(boundary.top(), boundary.bottom());
let x = random_range(-100., 100.);
let y = random_range(-150., boundary.top());
let particle = Particle::new(Vec2::new(x, y));
particles.push(particle);
/* Create the bouncing ball. */
let rigid_body = RigidBodyBuilder::dynamic()
.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.
let _window = app
.new_window()
.size(512, 512)
.size(WINDOW_WIDTH, WINDOW_HEIGHT)
.title("nannou")
.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.
@ -65,41 +69,31 @@ fn model(app: &App) -> Model {
.build();
engine.state.colliders.insert(collider);
/* Create the bouncing ball. */
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();
let mut particles = Vec::new();
fill_particles(app, &mut particles);
// fill_particles(app, &mut particles);
fill_particles(app, &mut engine.state.colliders, &mut engine.state.bodies);
Model {
_window,
particles,
// particles,
engine,
}
}
// 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 {
app.quit()
}
if let Resized(_) = event {
model.particles.clear();
// if let Resized(_) = event {
// 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`.