frame independent -> dependent, nn

This commit is contained in:
sparshg 2022-10-10 15:43:06 +05:30
parent 6e1b0f9d3b
commit e66d8ff9d4
4 changed files with 31 additions and 21 deletions

View File

@ -36,15 +36,15 @@ impl Asteroid {
); );
Self { Self {
pos: r, pos: r,
vel: 0.1 * -r vel: 0.001 * -r
+ vec2( + vec2(
gen_range(20., 60.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, gen_range(0.3, 1.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
gen_range(20., 60.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, gen_range(0.3, 1.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
), ),
size: size, size: size,
sides: sides, sides: sides,
radius: radius, radius: radius,
omega: gen_range(50., 200.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. }, omega: gen_range(0.8, 3.5) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
rot: 0., rot: 0.,
alive: true, alive: true,
} }

View File

@ -16,15 +16,15 @@ async fn main() {
..Default::default() ..Default::default()
}; };
set_camera(&cam); set_camera(&cam);
// let mut world = World::new(); let mut world = World::new();
let mut nn = NN::new(vec![1, 2, 1]); // let mut nn = NN::new(vec![1, 2, 1]);
loop { loop {
// clear_background(BLACK); clear_background(BLACK);
// if !world.over { if !world.over {
// world.update(); world.update();
// } }
// world.draw(); world.draw();
next_frame().await next_frame().await
} }
} }

View File

@ -56,29 +56,39 @@ impl Player {
pub fn update(&mut self) { pub fn update(&mut self) {
let mut mag = 0.; let mut mag = 0.;
if is_key_down(KeyCode::Right) { let mut keys = vec![false, false, false];
self.rot += 5.; if let Some(brain) = &self.brain {
keys = brain
.feed_forward(vec![
self.pos.x, self.pos.y, self.vel.x, self.vel.y, self.rot,
])
.iter()
.map(|&x| if x > 0. { true } else { false })
.collect();
}
if is_key_down(KeyCode::Right) || keys[0] {
self.rot += 0.1;
self.dir = vec2(self.rot.cos(), self.rot.sin()); self.dir = vec2(self.rot.cos(), self.rot.sin());
} }
if is_key_down(KeyCode::Left) { if is_key_down(KeyCode::Left) || keys[1] {
self.rot -= 5.; self.rot -= 0.1;
self.dir = vec2(self.rot.cos(), self.rot.sin()); self.dir = vec2(self.rot.cos(), self.rot.sin());
} }
if is_key_down(KeyCode::Up) { if is_key_down(KeyCode::Up) || keys[2] {
mag = 360.; mag = 0.14;
} }
if is_key_down(KeyCode::Space) { if is_key_down(KeyCode::Space) {
if self.shot_interval + self.last_shot < get_time() as f32 { if self.shot_interval + self.last_shot < get_time() as f32 {
self.last_shot = get_time() as f32; self.last_shot = get_time() as f32;
self.bullets.push(Bullet { self.bullets.push(Bullet {
pos: self.pos + self.dir.rotate(vec2(20., 0.)), pos: self.pos + self.dir.rotate(vec2(20., 0.)),
vel: self.dir.rotate(vec2(500., 0.)) + self.vel, vel: self.dir.rotate(vec2(8.5, 0.)) + self.vel,
alive: true, alive: true,
}); });
} }
} }
self.vel += (mag * self.dir - self.drag * self.vel.length() * self.vel); self.vel += mag * self.dir - self.drag * self.vel.length() * self.vel;
self.pos += self.vel; self.pos += self.vel;
if self.pos.x.abs() > screen_width() / 2. + 10. { if self.pos.x.abs() > screen_width() / 2. + 10. {
self.pos.x *= -1.; self.pos.x *= -1.;

View File

@ -40,7 +40,7 @@ impl World {
self.score += 1; self.score += 1;
match asteroid.size { match asteroid.size {
AsteroidSize::Large => { AsteroidSize::Large => {
let rand = vec2(gen_range(-50., 50.), gen_range(-50., 50.)); let rand = vec2(gen_range(-0.8, 0.8), gen_range(-0.8, 0.8));
to_add.push(Asteroid::new_from( to_add.push(Asteroid::new_from(
asteroid.pos, asteroid.pos,
asteroid.vel + rand, asteroid.vel + rand,
@ -53,7 +53,7 @@ impl World {
)); ));
} }
AsteroidSize::Medium => { AsteroidSize::Medium => {
let rand = vec2(gen_range(-40., 40.), gen_range(-40., 40.)); let rand = vec2(gen_range(-0.6, 0.6), gen_range(-0.6, 0.6));
to_add.push(Asteroid::new_from( to_add.push(Asteroid::new_from(
asteroid.pos, asteroid.pos,
asteroid.vel + rand, asteroid.vel + rand,