Compare commits

..

2 Commits

Author SHA1 Message Date
Maxluli dd49731ea8 Added vectors indicators 2022-05-11 13:04:20 +02:00
Maxluli e27180a4b3 Changed the border behaviour 2022-05-11 12:43:30 +02:00
+58 -20
View File
@@ -20,13 +20,13 @@ 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 = 15; const MAX_BIRD_SPEED:i32 = 15;
const BIRDS_COUNT:i32 = 500; const BIRDS_COUNT:i32 = 200;
const BIRD_SIZE:i32 = 15; const BIRD_SIZE:i32 = 15;
const VISION_RANGE:i32 = 150; const VISION_RANGE:i32 = 100;
const COHERENCE_RATE:i32 = 1; const COHERENCE_RATE:i32 = 1;
const SEPRATION_RATE:i32 = 1; const SEPRATION_RATE:i32 = 1;
const ALIGNEMENT_RATE:i32 = 2; const ALIGNEMENT_RATE:i32 = 1;
const NEIGHBOUR_TRESHOLD:i32 = 10; const NEIGHBOUR_TRESHOLD:i32 = 10;
pub struct Bird{ pub struct Bird{
@@ -41,9 +41,14 @@ impl Simulation{
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>){ pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>){
ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64)); ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64));
ctx.clear(); ctx.clear();
ctx.set_draw_color(Color::RGB(0x33, 0x33, 0x33));
for bird in self.birds.iter(){ for bird in self.birds.iter(){
ctx.set_draw_color(Color::RGB(0x33, 0x33, 0x33));
ctx.fill_rect(bird.shape).expect("Rusty Boids"); ctx.fill_rect(bird.shape).expect("Rusty Boids");
ctx.set_draw_color(Color::RGB(0xe7, 0x4c, 0x3c));
let start_pos = Point::new(bird.shape.x + bird.shape.width() as i32 / 2,bird.shape.y + bird.shape.height() as i32 /2);
let end_pos = Point::new(start_pos.x + bird.velocity.x * 2,start_pos.y + bird.velocity.y * 2);
ctx.draw_line(start_pos,end_pos).expect("Could nor draw vector line");
} }
} }
pub fn apply_coherence(&mut self){ pub fn apply_coherence(&mut self){
@@ -177,29 +182,62 @@ impl Simulation{
let mut posy = bird.shape.y; let mut posy = bird.shape.y;
let width:i32 = bird.shape.width() as i32; let width:i32 = bird.shape.width() as i32;
let height:i32 = bird.shape.height() as i32; let height:i32 = bird.shape.height() as i32;
//let left_wall = WINDOW_WIDTH - width;
//let bottom_wall = WINDOW_HEIGHT- height;
if posx + bird.velocity.x > WINDOW_WIDTH - width{ let push_back = 1;
posx = 0; let soft_border_margin = 30;
//bird.velocity.x -= bird.velocity.x / 2; let left_soft_border = 0 + soft_border_margin;
let right_soft_border = WINDOW_WIDTH - soft_border_margin;
let top_soft_border = 0 + soft_border_margin;
let bottom_soft_border = WINDOW_HEIGHT -soft_border_margin;
if posx + bird.velocity.x > WINDOW_WIDTH - width || posx + bird.velocity.x < 0{
bird.velocity.x = 0;
} }
if posx + bird.velocity.x < 0{ if posy + bird.velocity.y > WINDOW_HEIGHT - height || posy + bird.velocity.y < 0{
posx = WINDOW_WIDTH - bird.shape.width() as i32; bird.velocity.y = 0;
//bird.velocity.x += bird.velocity.x / 2; }
if posx + bird.velocity.x > right_soft_border && bird.velocity.x <= 0{
//the bird is coming from the border so we accelerate it
bird.velocity.x -= push_back;
}else{
bird.velocity.x += push_back;
}
if posx - bird.velocity.x < left_soft_border && bird.velocity.x >= 0{
//the bird is coming from the border so we accelerate it
bird.velocity.x += push_back;
}else{
bird.velocity.x -= push_back;
} }
if posy + bird.velocity.y > WINDOW_HEIGHT - height{ if posy + bird.velocity.y > bottom_soft_border && bird.velocity.y <= 0{
posy = 0; //the bird is coming from the border so we accelerate it
//bird.velocity.y -= bird.velocity.y / 2; bird.velocity.y -= push_back;
}else{
bird.velocity.y += push_back;
} }
if posy + bird.velocity.y < 0{ if posy - bird.velocity.y < top_soft_border && bird.velocity.y >= 0{
posy = WINDOW_HEIGHT - bird.shape.height() as i32; //the bird is coming from the border so we accelerate it
//bird.velocity.x += bird.velocity.x / 2; bird.velocity.y += push_back;
}else{
bird.velocity.y -= push_back;
} }
posx += bird.velocity.x; if bird.velocity.y > MAX_BIRD_SPEED{
posy += bird.velocity.y; bird.velocity.y = MAX_BIRD_SPEED;
}else if bird.velocity.y < -MAX_BIRD_SPEED{
bird.velocity.y = -MAX_BIRD_SPEED;
}
if bird.velocity.x > MAX_BIRD_SPEED{
bird.velocity.x = MAX_BIRD_SPEED;
}else if bird.velocity.x < -MAX_BIRD_SPEED{
bird.velocity.x = -MAX_BIRD_SPEED;
}
posx += bird.velocity.x / 2;
posy += bird.velocity.y / 2;
bird.shape.set_x(posx); bird.shape.set_x(posx);
bird.shape.set_y(posy); bird.shape.set_y(posy);
} }