Not really an improvement, but at least not significantly worse...

master
CptCaptain 4 years ago
parent 4c5677b369
commit d9e568491c
  1. 56
      src/main.rs

@ -126,13 +126,7 @@ impl Object {
.map( .map(
|obj_pos| |obj_pos|
{ {
obj_pos Object::calc_xy_distances(&this_pos, obj_pos)
.iter()
.zip(&this_pos)
.map(
|(obj, this)|
{ obj - this }
).collect()
} }
).collect(); ).collect();
for dist in distances { for dist in distances {
@ -143,18 +137,48 @@ impl Object {
} }
} }
fn calc_xy_distances(this_pos: &Vec<f64>, obj_pos: &Vec<f64>) -> Vec<f64> {
obj_pos
.iter()
.zip(this_pos)
.map(
|(obj, this)|
{ obj - this }
).collect()
}
fn repel(&mut self, obj_pos: &mut Vec<Vec<f64>>) { fn repel(&mut self, obj_pos: &mut Vec<Vec<f64>>) {
let this_pos: Vec<f64> = self.get_pos(); let this_pos: Vec<f64> = self.get_pos();
for pos in obj_pos { obj_pos
let mut dist_vec: Vec<f64> = Vec::new(); .iter()
for i in 0..2 { .for_each(
dist_vec.push(this_pos[i] - pos[i]); |pos|
} {
let amount = calc_amount(&dist_vec); let dist_vec: Vec<f64> = this_pos.iter().zip(pos).map(|(t, p)| {t - p}).collect();
// let mut dist_vec: Vec<f64> = Vec::new();
// for i in 0..2 {
// dist_vec.push(this_pos[i] - pos[i]);
// }
let amount = calc_amount(&dist_vec);
self.apply_force_to_self(&dist_vec, amount)
}
)
// for pos in obj_pos {
// let mut dist_vec: Vec<f64> = Vec::new();
// for i in 0..2 {
// dist_vec.push(this_pos[i] - pos[i]);
// }
// let amount = calc_amount(&dist_vec);
//
// self.apply_force_to_self(&mut dist_vec, amount)
// }
}
for (i, d) in dist_vec.iter().enumerate() { fn apply_force_to_self(&mut self, dist_vec: &Vec<f64>, amount: f64) {
self.vel[i] += 1.5 * d.min(150.0).max(-150.0) / (amount).max(0.01); for (i, d) in dist_vec.iter().enumerate() {
} let repel_force = 1.5 * d.min(150.0).max(-150.0) / (amount).max(0.01);
self.vel[i] += repel_force;
} }
} }

Loading…
Cancel
Save