birds can now travel trough sides and added a crappy separation method

This commit is contained in:
2022-05-11 10:34:44 +02:00
parent d60330182e
commit 4c70120eaf
+62 -43
View File
@@ -19,15 +19,15 @@ use rand::Rng;
const WINDOW_HEIGHT:i32 = 600; const WINDOW_HEIGHT:i32 = 600;
const WINDOW_WIDTH:i32 = 800; const WINDOW_WIDTH:i32 = 800;
const BIRD_SPEED:i32 = 2; const BIRD_SPEED:i32 = 2;
const MAX_BIRD_SPEED:i32 = 10; const MAX_BIRD_SPEED:i32 = 15;
const BIRDS_COUNT:i32 = 500; const BIRDS_COUNT:i32 = 500;
const BIRD_SIZE:i32 = 15; const BIRD_SIZE:i32 = 15;
const VISION_RANGE:i32 = 100; const VISION_RANGE:i32 = 100;
const COHERENCE_RATE:i32 = 1; const COHERENCE_RATE:i32 = 3;
const SEPRATION_RATE:i32 = 5; const SEPRATION_RATE:i32 = 0;
const ALIGNEMENT_RATE:i32 = 5; const ALIGNEMENT_RATE:i32 = 5;
const NEIGHBOUR_TRESHOLD:i32 = 50; const NEIGHBOUR_TRESHOLD:i32 = 10;
pub struct Bird{ pub struct Bird{
shape:Rect, shape:Rect,
@@ -107,36 +107,63 @@ impl Simulation{
} }
pub fn apply_separation(&mut self){ pub fn apply_separation(&mut self){
/* // first we calculate all the averages for every birds
// we need to check who is "close" form each bird let mut averages:Vec<Point> = Vec::with_capacity(BIRDS_COUNT as usize);
let mut neighbours:Vec<Point> = Vec::new(); for i in 0..BIRDS_COUNT{
for i in 0..BIRDS_COUNT{ let mut sum = Point::new(0,0);
let target = &self.birds[i as usize]; let target = &self.birds[i as usize];
let mut xdistance:i32; let mut x_offset:i32;
let mut ydistance: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();
for bird in &self.birds{ if x_offset < NEIGHBOUR_TRESHOLD && y_offset < NEIGHBOUR_TRESHOLD{
xdistance = (target.shape.x - bird.shape.x).abs(); sum.x += bird.shape.x;
ydistance = (target.shape.y - bird.shape.y).abs(); 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);
}
if xdistance <= NEIGHBOUR_TRESHOLD && ydistance <= NEIGHBOUR_TRESHOLD{ for i in 0..BIRDS_COUNT{
// //now we need to steer torwards it
//we need let bird = &mut self.birds[i as usize];
neighbours.push(bird); let posx = bird.shape.x;
} let posy = bird.shape.y;
} let average = averages[i as usize];
}
let mut averages if posx > average.x{
let mut sum:Point = Point::new(0,0); if bird.velocity.x + SEPRATION_RATE > MAX_BIRD_SPEED{
for b in &neighbours{ bird.velocity.x = MAX_BIRD_SPEED;
sum.x += b.shape.x; }else{
sum.y += b.shape.y; 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;
}
}
if posy > average.y{
// Then we just need to steer away from them 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){ pub fn apply_alignement(&mut self){
@@ -154,25 +181,17 @@ impl Simulation{
let bottom_wall = WINDOW_HEIGHT- height; let bottom_wall = WINDOW_HEIGHT- height;
if posx + bird.velocity.x > WINDOW_WIDTH - width{ if posx + bird.velocity.x > WINDOW_WIDTH - width{
let offset = left_wall - (posx + bird.velocity.x); posx = 0;
posx = left_wall - offset;
bird.velocity.x = -bird.velocity.x;
} }
if posx + bird.velocity.x < 0{ if posx + bird.velocity.x < 0{
let offset = posx + bird.velocity.x; posx = WINDOW_WIDTH - bird.shape.width() as i32;
posx = 0 + offset;
bird.velocity.x = -bird.velocity.x;
} }
if posy + bird.velocity.y > WINDOW_HEIGHT - height{ if posy + bird.velocity.y > WINDOW_HEIGHT - height{
let offset = bottom_wall - (posy + bird.velocity.y); posy = 0;
posy = bottom_wall - offset;
bird.velocity.y = -bird.velocity.y;
} }
if posy + bird.velocity.y < 0{ if posy + bird.velocity.y < 0{
let offset = posy + bird.velocity.y; posy = WINDOW_HEIGHT - bird.shape.height() as i32;
posy = 0 + offset;
bird.velocity.y = -bird.velocity.y;
} }
posx += bird.velocity.x; posx += bird.velocity.x;
@@ -209,7 +228,7 @@ pub fn generate_birds() -> Vec<Bird>{
let mut birds = Vec::with_capacity(BIRDS_COUNT as usize); let mut birds = Vec::with_capacity(BIRDS_COUNT as usize);
for bird in 0..BIRDS_COUNT{ 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 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}); birds.push(Bird{shape:rectangle,velocity:velocity});
} }
return birds; return birds;