Skip to content

Commit ac58410

Browse files
add reset functionality
1 parent 882d75e commit ac58410

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/app.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ impl AppState {
297297
self.particle_system.paused = !self.particle_system.paused;
298298
}
299299

300+
if ui.button("Reset").clicked() {
301+
self.particle_system.reset(&self.queue);
302+
}
303+
300304
ui.separator();
301305
ui.heading("Mouse Interaction");
302306
ui.label(format!(
@@ -484,6 +488,9 @@ impl ApplicationHandler for App {
484488
winit::keyboard::PhysicalKey::Code(KeyCode::KeyP) => {
485489
state.particle_system.paused = !state.particle_system.paused;
486490
}
491+
winit::keyboard::PhysicalKey::Code(KeyCode::KeyR) => {
492+
state.particle_system.reset(&state.queue);
493+
}
487494
winit::keyboard::PhysicalKey::Code(KeyCode::F11) => {
488495
state.fullscreen = !state.fullscreen;
489496
let window = self.window.as_ref().unwrap();

src/particle_system.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,37 @@ impl ParticleSystem {
199199
}
200200
}
201201

202+
pub fn reset(&mut self, queue: &egui_wgpu::wgpu::Queue) {
203+
let mut particles = Vec::with_capacity(self.max_particles as usize);
204+
for _ in 0..self.max_particles {
205+
let r = rand::random::<f32>();
206+
let g = rand::random::<f32>();
207+
let b = rand::random::<f32>();
208+
209+
let phi = rand::random::<f32>() * std::f32::consts::PI * 2.0;
210+
let theta = (rand::random::<f32>() - 0.5) * std::f32::consts::PI;
211+
let radius = rand::random::<f32>() * 50.0;
212+
213+
let pos = Vec3::new(
214+
radius * phi.cos() * theta.cos(),
215+
radius * theta.sin(),
216+
radius * phi.sin() * theta.cos(),
217+
);
218+
219+
let vel = Vec3::new(
220+
(rand::random::<f32>() - 0.5) * 0.1,
221+
(rand::random::<f32>() - 0.5) * 0.1,
222+
(rand::random::<f32>() - 0.5) * 0.1,
223+
);
224+
225+
particles.push(Particle::new(pos, vel, Vec4::new(r, g, b, 1.0)));
226+
}
227+
228+
queue.write_buffer(&self.particle_buffer, 0, bytemuck::cast_slice(&particles));
229+
230+
self.gravity = 0.0;
231+
}
232+
202233
pub fn update(
203234
&mut self,
204235
queue: &egui_wgpu::wgpu::Queue,

0 commit comments

Comments
 (0)