birds can now travel trough sides and added a crappy separation method
This commit is contained in:
107
src/main.rs
107
src/main.rs
@@ -19,15 +19,15 @@ use rand::Rng;
|
||||
const WINDOW_HEIGHT:i32 = 600;
|
||||
const WINDOW_WIDTH:i32 = 800;
|
||||
const BIRD_SPEED:i32 = 2;
|
||||
const MAX_BIRD_SPEED:i32 = 10;
|
||||
const MAX_BIRD_SPEED:i32 = 15;
|
||||
const BIRDS_COUNT:i32 = 500;
|
||||
const BIRD_SIZE:i32 = 15;
|
||||
|
||||
const VISION_RANGE:i32 = 100;
|
||||
const COHERENCE_RATE:i32 = 1;
|
||||
const SEPRATION_RATE:i32 = 5;
|
||||
const COHERENCE_RATE:i32 = 3;
|
||||
const SEPRATION_RATE:i32 = 0;
|
||||
const ALIGNEMENT_RATE:i32 = 5;
|
||||
const NEIGHBOUR_TRESHOLD:i32 = 50;
|
||||
const NEIGHBOUR_TRESHOLD:i32 = 10;
|
||||
|
||||
pub struct Bird{
|
||||
shape:Rect,
|
||||
@@ -107,36 +107,63 @@ impl Simulation{
|
||||
|
||||
}
|
||||
pub fn apply_separation(&mut self){
|
||||
/*
|
||||
// we need to check who is "close" form each bird
|
||||
let mut neighbours:Vec<Point> = Vec::new();
|
||||
for i in 0..BIRDS_COUNT{
|
||||
let target = &self.birds[i as usize];
|
||||
let mut xdistance:i32;
|
||||
let mut ydistance:i32;
|
||||
|
||||
for bird in &self.birds{
|
||||
xdistance = (target.shape.x - bird.shape.x).abs();
|
||||
ydistance = (target.shape.y - bird.shape.y).abs();
|
||||
// first we calculate all the averages for every birds
|
||||
let mut averages:Vec<Point> = Vec::with_capacity(BIRDS_COUNT as usize);
|
||||
for i in 0..BIRDS_COUNT{
|
||||
let mut sum = Point::new(0,0);
|
||||
let target = &self.birds[i as usize];
|
||||
let mut x_offset:i32;
|
||||
let mut y_offset:i32;
|
||||
let mut neighbours_count = 0;
|
||||
for bird in &self.birds{
|
||||
x_offset = (target.shape.x - bird.shape.x).abs();
|
||||
y_offset = (target.shape.y - bird.shape.y).abs();
|
||||
|
||||
if xdistance <= NEIGHBOUR_TRESHOLD && ydistance <= NEIGHBOUR_TRESHOLD{
|
||||
//
|
||||
//we need
|
||||
neighbours.push(bird);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut averages
|
||||
let mut sum:Point = Point::new(0,0);
|
||||
for b in &neighbours{
|
||||
sum.x += b.shape.x;
|
||||
sum.y += b.shape.y;
|
||||
}
|
||||
if x_offset < NEIGHBOUR_TRESHOLD && y_offset < NEIGHBOUR_TRESHOLD{
|
||||
sum.x += bird.shape.x;
|
||||
sum.y += bird.shape.y;
|
||||
neighbours_count += 1;
|
||||
}
|
||||
}
|
||||
let average = Point::new(sum.x / neighbours_count as i32,sum.y / neighbours_count as i32);
|
||||
averages.push(average);
|
||||
}
|
||||
|
||||
for i in 0..BIRDS_COUNT{
|
||||
//now we need to steer torwards it
|
||||
let bird = &mut self.birds[i as usize];
|
||||
let posx = bird.shape.x;
|
||||
let posy = bird.shape.y;
|
||||
let average = averages[i as usize];
|
||||
|
||||
if posx > average.x{
|
||||
if bird.velocity.x + SEPRATION_RATE > MAX_BIRD_SPEED{
|
||||
bird.velocity.x = MAX_BIRD_SPEED;
|
||||
}else{
|
||||
bird.velocity.x += SEPRATION_RATE;
|
||||
}
|
||||
}else{
|
||||
if bird.velocity.x - SEPRATION_RATE < -MAX_BIRD_SPEED{
|
||||
bird.velocity.x = -MAX_BIRD_SPEED;
|
||||
}else{
|
||||
bird.velocity.x -= SEPRATION_RATE;
|
||||
}
|
||||
}
|
||||
|
||||
// Then we just need to steer away from them
|
||||
*/
|
||||
if posy > average.y{
|
||||
if bird.velocity.y + SEPRATION_RATE > MAX_BIRD_SPEED{
|
||||
bird.velocity.y = MAX_BIRD_SPEED;
|
||||
}else{
|
||||
bird.velocity.y += SEPRATION_RATE;
|
||||
}
|
||||
}else{
|
||||
if bird.velocity.y - SEPRATION_RATE < -MAX_BIRD_SPEED{
|
||||
bird.velocity.y = -MAX_BIRD_SPEED;
|
||||
}else{
|
||||
bird.velocity.y -= SEPRATION_RATE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn apply_alignement(&mut self){
|
||||
|
||||
@@ -154,25 +181,17 @@ impl Simulation{
|
||||
let bottom_wall = WINDOW_HEIGHT- height;
|
||||
|
||||
if posx + bird.velocity.x > WINDOW_WIDTH - width{
|
||||
let offset = left_wall - (posx + bird.velocity.x);
|
||||
posx = left_wall - offset;
|
||||
bird.velocity.x = -bird.velocity.x;
|
||||
posx = 0;
|
||||
}
|
||||
if posx + bird.velocity.x < 0{
|
||||
let offset = posx + bird.velocity.x;
|
||||
posx = 0 + offset;
|
||||
bird.velocity.x = -bird.velocity.x;
|
||||
posx = WINDOW_WIDTH - bird.shape.width() as i32;
|
||||
}
|
||||
|
||||
if posy + bird.velocity.y > WINDOW_HEIGHT - height{
|
||||
let offset = bottom_wall - (posy + bird.velocity.y);
|
||||
posy = bottom_wall - offset;
|
||||
bird.velocity.y = -bird.velocity.y;
|
||||
posy = 0;
|
||||
}
|
||||
if posy + bird.velocity.y < 0{
|
||||
let offset = posy + bird.velocity.y;
|
||||
posy = 0 + offset;
|
||||
bird.velocity.y = -bird.velocity.y;
|
||||
posy = WINDOW_HEIGHT - bird.shape.height() as i32;
|
||||
}
|
||||
|
||||
posx += bird.velocity.x;
|
||||
@@ -209,7 +228,7 @@ pub fn generate_birds() -> Vec<Bird>{
|
||||
let mut birds = Vec::with_capacity(BIRDS_COUNT as usize);
|
||||
for bird in 0..BIRDS_COUNT{
|
||||
let rectangle = Rect::new(rng.gen_range(0..WINDOW_WIDTH - BIRD_SIZE),rng.gen_range(0..WINDOW_HEIGHT - BIRD_SIZE),BIRD_SIZE as u32, BIRD_SIZE as u32);
|
||||
let velocity = Point::new(rng.gen_range(0..MAX_BIRD_SPEED), rng.gen_range(0..MAX_BIRD_SPEED));
|
||||
let velocity = Point::new(rng.gen_range(-MAX_BIRD_SPEED..MAX_BIRD_SPEED), rng.gen_range(-MAX_BIRD_SPEED..MAX_BIRD_SPEED));
|
||||
birds.push(Bird{shape:rectangle,velocity:velocity});
|
||||
}
|
||||
return birds;
|
||||
|
||||
Reference in New Issue
Block a user