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 {
pos: r,
vel: 0.1 * -r
vel: 0.001 * -r
+ vec2(
gen_range(20., 60.) * 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. },
gen_range(0.3, 1.) * if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
),
size: size,
sides: sides,
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.,
alive: true,
}

View File

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

View File

@ -56,29 +56,39 @@ impl Player {
pub fn update(&mut self) {
let mut mag = 0.;
if is_key_down(KeyCode::Right) {
self.rot += 5.;
let mut keys = vec![false, false, false];
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());
}
if is_key_down(KeyCode::Left) {
self.rot -= 5.;
if is_key_down(KeyCode::Left) || keys[1] {
self.rot -= 0.1;
self.dir = vec2(self.rot.cos(), self.rot.sin());
}
if is_key_down(KeyCode::Up) {
mag = 360.;
if is_key_down(KeyCode::Up) || keys[2] {
mag = 0.14;
}
if is_key_down(KeyCode::Space) {
if self.shot_interval + self.last_shot < get_time() as f32 {
self.last_shot = get_time() as f32;
self.bullets.push(Bullet {
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,
});
}
}
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;
if self.pos.x.abs() > screen_width() / 2. + 10. {
self.pos.x *= -1.;

View File

@ -40,7 +40,7 @@ impl World {
self.score += 1;
match asteroid.size {
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(
asteroid.pos,
asteroid.vel + rand,
@ -53,7 +53,7 @@ impl World {
));
}
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(
asteroid.pos,
asteroid.vel + rand,