simplify particle vectors
This commit is contained in:
parent
1eaa48d511
commit
493af1e71c
2 changed files with 7 additions and 8 deletions
|
|
@ -16,7 +16,7 @@ use link::Link;
|
||||||
const WIN_WIDTH: u32 = 800;
|
const WIN_WIDTH: u32 = 800;
|
||||||
const WIN_HEIGHT: u32 = 600;
|
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) {
|
fn populate_particles(particles: &mut Vec<Rc<RefCell<Particle>>>, num: u32, cols: u32) {
|
||||||
for i in 0..num {
|
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 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 y_pos = (WIN_HEIGHT/2 - (num/cols)/2*OFFSET + OFFSET * (i / cols)) as f32;
|
||||||
let immovable = i < cols;
|
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)));
|
particles.push(Rc::new(RefCell::new(particle)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -73,7 +73,7 @@ fn populate_links(
|
||||||
fn apply_forces(particles: &Vec<Rc<RefCell<Particle>>>) {
|
fn apply_forces(particles: &Vec<Rc<RefCell<Particle>>>) {
|
||||||
for particle in particles {
|
for particle in particles {
|
||||||
let mut borrowed = particle.borrow_mut();
|
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();
|
let dt = clock.restart().as_seconds();
|
||||||
|
|
||||||
//particle.apply_force(Vector2f::new(0., GRAVITY));
|
|
||||||
|
|
||||||
|
|
||||||
apply_forces(&particles);
|
apply_forces(&particles);
|
||||||
update_particles(&particles, dt);
|
update_particles(&particles, dt);
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,8 @@ pub struct Particle {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
Particle {
|
||||||
pos,
|
pos,
|
||||||
prev_pos: 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 {
|
if self.immovable {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.accel += force;
|
self.accel += force.into();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, dt: f32) {
|
pub fn update(&mut self, dt: f32) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue