simplify particle vectors

This commit is contained in:
Raptorox 2025-08-01 18:18:31 +02:00
parent 1eaa48d511
commit 493af1e71c
No known key found for this signature in database
GPG key ID: 8B3556FC3ED1F6D8
2 changed files with 7 additions and 8 deletions

View file

@ -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<Rc<RefCell<Particle>>>, num: u32, cols: u32) {
for i in 0..num {
@ -24,7 +24,7 @@ fn populate_particles(particles: &mut Vec<Rc<RefCell<Particle>>>, 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<Rc<RefCell<Particle>>>) {
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);

View file

@ -9,7 +9,8 @@ pub struct Particle {
}
impl Particle {
pub fn new(pos: Vector2f, immovable: bool) -> Self {
pub fn new<P: Into<Vector2f>>(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<F: Into<Vector2f>>(&mut self, force: F) {
if self.immovable {
return;
}
self.accel += force;
self.accel += force.into();
}
pub fn update(&mut self, dt: f32) {