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