Added coherence law
This commit is contained in:
+75
-5
@@ -1,3 +1,11 @@
|
|||||||
|
/// file: main.rs
|
||||||
|
/// author: Rohmer Maxime <maxluligames@gmail.com>
|
||||||
|
/// date: 10/05/2022
|
||||||
|
/// version: 0.1.0
|
||||||
|
/// Sources:
|
||||||
|
/// https://eater.net/boids
|
||||||
|
/// https://docs.rs/sdl2/latest/sdl2/
|
||||||
|
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
use sdl2::event::Event;
|
use sdl2::event::Event;
|
||||||
use sdl2::keyboard::Keycode;
|
use sdl2::keyboard::Keycode;
|
||||||
@@ -13,8 +21,11 @@ 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 = 10;
|
||||||
const BIRDS_COUNT:i32 = 50;
|
const BIRDS_COUNT:i32 = 50;
|
||||||
const MIN_BIRD_SIZE:i32 = 20;
|
const BIRD_SIZE:i32 = 15;
|
||||||
const MAX_BIRD_SIZE:i32 = 30;
|
|
||||||
|
const COHERENCE_RATE:i32 = 1;
|
||||||
|
const SEPRATION_RATE:i32 = 5;
|
||||||
|
const ALIGNEMENT_RATE:i32 = 5;
|
||||||
|
|
||||||
pub struct Bird{
|
pub struct Bird{
|
||||||
shape:Rect,
|
shape:Rect,
|
||||||
@@ -32,8 +43,68 @@ impl Simulation{
|
|||||||
for bird in self.birds.iter(){
|
for bird in self.birds.iter(){
|
||||||
ctx.fill_rect(bird.shape).expect("Rusty Boids");
|
ctx.fill_rect(bird.shape).expect("Rusty Boids");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
for bird in &self.birds{
|
||||||
|
sum.x += bird.shape.x;
|
||||||
|
sum.y += bird.shape.y;
|
||||||
|
}
|
||||||
|
let average = Point::new(sum.x / BIRDS_COUNT as i32,sum.y / BIRDS_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 - COHERENCE_RATE < -MAX_BIRD_SPEED{
|
||||||
|
bird.velocity.x = -MAX_BIRD_SPEED;
|
||||||
|
}else{
|
||||||
|
bird.velocity.x -= COHERENCE_RATE;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
if bird.velocity.x + COHERENCE_RATE > MAX_BIRD_SPEED{
|
||||||
|
bird.velocity.x = MAX_BIRD_SPEED;
|
||||||
|
}else{
|
||||||
|
bird.velocity.x += COHERENCE_RATE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if posy > average.y{
|
||||||
|
if bird.velocity.y - COHERENCE_RATE < -MAX_BIRD_SPEED{
|
||||||
|
bird.velocity.y = -MAX_BIRD_SPEED;
|
||||||
|
}else{
|
||||||
|
bird.velocity.y -= COHERENCE_RATE;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
if bird.velocity.y + COHERENCE_RATE > MAX_BIRD_SPEED{
|
||||||
|
bird.velocity.y = MAX_BIRD_SPEED;
|
||||||
|
}else{
|
||||||
|
bird.velocity.y += COHERENCE_RATE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
pub fn apply_separation(&mut self){
|
||||||
|
|
||||||
|
}
|
||||||
|
pub fn apply_alignement(&mut self){
|
||||||
|
|
||||||
}
|
}
|
||||||
pub fn update(&mut self){
|
pub fn update(&mut self){
|
||||||
|
self.apply_coherence();
|
||||||
|
self.apply_separation();
|
||||||
|
self.apply_alignement();
|
||||||
for bird in &mut self.birds{
|
for bird in &mut self.birds{
|
||||||
let mut posx = bird.shape.x;
|
let mut posx = bird.shape.x;
|
||||||
let mut posy = bird.shape.y;
|
let mut posy = bird.shape.y;
|
||||||
@@ -97,8 +168,7 @@ pub fn generate_birds() -> Vec<Bird>{
|
|||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
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 size = rng.gen_range(MIN_BIRD_SIZE as u32..MAX_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 rectangle = Rect::new(rng.gen_range(0..WINDOW_WIDTH - MAX_BIRD_SIZE),rng.gen_range(0..WINDOW_HEIGHT - MAX_BIRD_SIZE),size, size);
|
|
||||||
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(0..MAX_BIRD_SPEED), rng.gen_range(0..MAX_BIRD_SPEED));
|
||||||
birds.push(Bird{shape:rectangle,velocity:velocity});
|
birds.push(Bird{shape:rectangle,velocity:velocity});
|
||||||
}
|
}
|
||||||
@@ -108,7 +178,7 @@ pub fn main() {
|
|||||||
let sdl_context = sdl2::init().unwrap();
|
let sdl_context = sdl2::init().unwrap();
|
||||||
let video_subsystem = sdl_context.video().unwrap();
|
let video_subsystem = sdl_context.video().unwrap();
|
||||||
|
|
||||||
let window = video_subsystem.window("Test SDL Rust", WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32)
|
let window = video_subsystem.window("Rust Boids Simulation", WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32)
|
||||||
.position_centered()
|
.position_centered()
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user