|
|
|
@ -50,7 +50,7 @@ impl App { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn calc_amount(vec: &mut Vec<f64>) -> f64 { |
|
|
|
|
fn calc_amount(vec: &Vec<f64>) -> f64 { |
|
|
|
|
(vec[0].powi(2) + vec[1].powi(2)).sqrt() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -111,8 +111,8 @@ impl Object { |
|
|
|
|
|
|
|
|
|
fn mouse_attract(&mut self, mouse_pos: [f64; 2]) { |
|
|
|
|
let pos: Vec<f64> = self.get_pos(); |
|
|
|
|
let mut dist: Vec<f64> = vec![pos[0] - mouse_pos[0], pos[1] - mouse_pos[1]]; |
|
|
|
|
let amount = calc_amount(&mut dist); |
|
|
|
|
let dist: Vec<f64> = vec![pos[0] - mouse_pos[0], pos[1] - mouse_pos[1]]; |
|
|
|
|
let amount = calc_amount(&dist); |
|
|
|
|
for (i, d) in dist.iter().enumerate() { |
|
|
|
|
self.vel[i] -= 0.2 * d / amount.max(0.1); |
|
|
|
|
self.vel[i] = self.vel[i].min(10.0).max(-10.0); |
|
|
|
@ -121,16 +121,22 @@ impl Object { |
|
|
|
|
|
|
|
|
|
fn attract(&mut self, objs: &mut Vec<Vec<f64>>) { |
|
|
|
|
let this_pos: Vec<f64> = self.get_pos(); |
|
|
|
|
let mut distances: Vec<Vec<f64>> = Vec::new(); |
|
|
|
|
for obj_pos in objs { |
|
|
|
|
let mut dist_vec: Vec<f64> = Vec::new(); |
|
|
|
|
for (i, op) in obj_pos.iter().enumerate() { |
|
|
|
|
dist_vec.push(op - this_pos[i]); |
|
|
|
|
} |
|
|
|
|
distances.push(dist_vec); |
|
|
|
|
} |
|
|
|
|
for mut dist in distances { |
|
|
|
|
let amount = calc_amount(&mut dist); |
|
|
|
|
let distances: Vec<Vec<f64>> = objs |
|
|
|
|
.iter() |
|
|
|
|
.map( |
|
|
|
|
|obj_pos| |
|
|
|
|
{ |
|
|
|
|
obj_pos |
|
|
|
|
.iter() |
|
|
|
|
.zip(&this_pos) |
|
|
|
|
.map( |
|
|
|
|
|(obj, this)| |
|
|
|
|
{ obj - this } |
|
|
|
|
).collect() |
|
|
|
|
} |
|
|
|
|
).collect(); |
|
|
|
|
for dist in distances { |
|
|
|
|
let amount = calc_amount(&dist); |
|
|
|
|
for (i, d) in dist.iter().enumerate() { |
|
|
|
|
self.vel[i] += 1.0 * (d / amount.max(0.01)); |
|
|
|
|
} |
|
|
|
@ -144,7 +150,7 @@ impl Object { |
|
|
|
|
for i in 0..2 { |
|
|
|
|
dist_vec.push(this_pos[i] - pos[i]); |
|
|
|
|
} |
|
|
|
|
let amount = calc_amount(&mut dist_vec); |
|
|
|
|
let amount = calc_amount(&dist_vec); |
|
|
|
|
|
|
|
|
|
for (i, d) in dist_vec.iter().enumerate() { |
|
|
|
|
self.vel[i] += 1.5 * d.min(150.0).max(-150.0) / (amount).max(0.01); |
|
|
|
@ -170,7 +176,7 @@ impl Object { |
|
|
|
|
for (i, d) in new_vel.iter().enumerate() { |
|
|
|
|
self.vel[i] = ((self.vel[i] + d) / 2.0).max(-10.0).min(10.0); |
|
|
|
|
} |
|
|
|
|
let amount = calc_amount(&mut self.vel); |
|
|
|
|
let amount = calc_amount(&self.vel); |
|
|
|
|
for i in 0..2 { |
|
|
|
|
self.vel[i] *= 10.0 / amount; |
|
|
|
|
} |
|
|
|
@ -191,7 +197,7 @@ impl Object { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn calc_direction(&mut self) -> f64 { |
|
|
|
|
let mut dir = (self.vel[0] / calc_amount(&mut self.vel)).acos(); |
|
|
|
|
let mut dir = (self.vel[0] / calc_amount(&self.vel)).acos(); |
|
|
|
|
if self.vel[1].signum() < 0.0 { |
|
|
|
|
dir = 2.0 * PI - dir; |
|
|
|
|
} |
|
|
|
@ -223,7 +229,7 @@ fn main() { |
|
|
|
|
|
|
|
|
|
let opengl = OpenGL::V3_2; |
|
|
|
|
|
|
|
|
|
let num_objs = 12; |
|
|
|
|
let num_objs = 512; |
|
|
|
|
|
|
|
|
|
let mut window: Window = WindowSettings::new("Test", [200, 200]) |
|
|
|
|
.opengl(opengl) |
|
|
|
@ -243,16 +249,15 @@ fn main() { |
|
|
|
|
.expect("Color failure!"), |
|
|
|
|
)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let mut positions: Vec<Vec<f64>> = Vec::new(); |
|
|
|
|
let mut positions: Vec<Vec<f64>> = vec![vec![0.0; 2]; objects.len()]; |
|
|
|
|
|
|
|
|
|
let mut events = Events::new(EventSettings::new()); |
|
|
|
|
while let Some(e) = events.next(&mut window) { |
|
|
|
|
if let Some(r) = e.render_args() { |
|
|
|
|
for obj in &mut objects { |
|
|
|
|
obj.update_border_dist(&r); |
|
|
|
|
obj.update(); |
|
|
|
|
positions.push(obj.get_pos()); |
|
|
|
|
for i in 0..objects.len() { |
|
|
|
|
objects[i].update_border_dist(&r); |
|
|
|
|
objects[i].update(); |
|
|
|
|
positions[i] = objects[i].get_pos(); |
|
|
|
|
} |
|
|
|
|
app.render(&objects, &r); |
|
|
|
|
} |
|
|
|
@ -262,7 +267,7 @@ fn main() { |
|
|
|
|
obj.attract(&mut positions); |
|
|
|
|
obj.repel(&mut positions); |
|
|
|
|
} |
|
|
|
|
positions.clear() |
|
|
|
|
// positions.clear()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let Some(p) = e.press_args() { |
|
|
|
|