Edited the coherence method to implement a vision range

This commit is contained in:
2022-05-11 09:41:32 +02:00
parent 9b1c282d51
commit d60330182e

View File

@@ -20,12 +20,14 @@ const WINDOW_HEIGHT:i32 = 600;
const WINDOW_WIDTH:i32 = 800;
const BIRD_SPEED:i32 = 2;
const MAX_BIRD_SPEED:i32 = 10;
const BIRDS_COUNT:i32 = 50;
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 ALIGNEMENT_RATE:i32 = 5;
const NEIGHBOUR_TRESHOLD:i32 = 50;
pub struct Bird{
shape:Rect,
@@ -45,16 +47,25 @@ impl Simulation{
}
}
pub fn apply_coherence(&mut self){
// 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{
sum.x += bird.shape.x;
sum.y += bird.shape.y;
x_offset = (target.shape.x - bird.shape.x).abs();
y_offset = (target.shape.y - bird.shape.y).abs();
if x_offset < VISION_RANGE && y_offset < VISION_RANGE{
sum.x += bird.shape.x;
sum.y += bird.shape.y;
neighbours_count += 1;
}
}
let average = Point::new(sum.x / BIRDS_COUNT as i32,sum.y / BIRDS_COUNT as i32);
let average = Point::new(sum.x / neighbours_count as i32,sum.y / neighbours_count as i32);
averages.push(average);
}
@@ -96,7 +107,36 @@ 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();
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;
}
// Then we just need to steer away from them
*/
}
pub fn apply_alignement(&mut self){