From 493af1e71cd27a026e0a18f82e7862c86b15ee74 Mon Sep 17 00:00:00 2001 From: Raptorox <70806316+Raptorox@users.noreply.github.com> Date: Fri, 1 Aug 2025 18:18:31 +0200 Subject: [PATCH] simplify particle vectors --- src/main.rs | 8 +++----- src/particle.rs | 7 ++++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index bbc996f..89472c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use link::Link; const WIN_WIDTH: u32 = 800; const WIN_HEIGHT: u32 = 600; -const GRAVITY: f32 = 100.; +const GRAVITY: Vector2f = Vector2f::new(0., 100.); fn populate_particles(particles: &mut Vec>>, num: u32, cols: u32) { for i in 0..num { @@ -24,7 +24,7 @@ fn populate_particles(particles: &mut Vec>>, num: u32, cols let x_pos = (WIN_WIDTH/2 - cols/2*OFFSET + OFFSET * (i % cols)) as f32; let y_pos = (WIN_HEIGHT/2 - (num/cols)/2*OFFSET + OFFSET * (i / cols)) as f32; let immovable = i < cols; - let particle = Particle::new(Vector2f::new(x_pos, y_pos), immovable); + let particle = Particle::new((x_pos, y_pos), immovable); particles.push(Rc::new(RefCell::new(particle))); } } @@ -73,7 +73,7 @@ fn populate_links( fn apply_forces(particles: &Vec>>) { for particle in particles { let mut borrowed = particle.borrow_mut(); - borrowed.apply_force(Vector2f::new(0., GRAVITY)); + borrowed.apply_force(GRAVITY); } } @@ -167,8 +167,6 @@ fn main() -> SfResult<()> { let dt = clock.restart().as_seconds(); - //particle.apply_force(Vector2f::new(0., GRAVITY)); - apply_forces(&particles); update_particles(&particles, dt); diff --git a/src/particle.rs b/src/particle.rs index f82c886..e7faa0c 100644 --- a/src/particle.rs +++ b/src/particle.rs @@ -9,7 +9,8 @@ pub struct Particle { } impl Particle { - pub fn new(pos: Vector2f, immovable: bool) -> Self { + pub fn new>(pos: P, immovable: bool) -> Self { + let pos = pos.into(); Particle { pos, prev_pos: pos, @@ -19,11 +20,11 @@ impl Particle { } } - pub fn apply_force(&mut self, force: Vector2f) { + pub fn apply_force>(&mut self, force: F) { if self.immovable { return; } - self.accel += force; + self.accel += force.into(); } pub fn update(&mut self, dt: f32) {