From d89f038b7a5eb42dd48d729d8808747d5cbc182b Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Sun, 12 Sep 2021 21:40:58 -0400 Subject: Walk bob and collision detection --- src/main.zig | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig index c44b5e6..ec47f86 100644 --- a/src/main.zig +++ b/src/main.zig @@ -133,24 +133,33 @@ pub fn main() !void { if (isKeyPressed(.R)) player.ang += rotation; if (isKeyPressed(.T)) player.ang -= rotation; + const cos = std.math.cos(player.ang); + const sin = std.math.sin(player.ang); + player.acc_x = 0; player.acc_y = 0; - const acc_mag = 50; + const acc_mag = 30; if (isKeyPressed(.Right)) { - player.acc_x += std.math.cos(player.ang - PI / 2.0) * acc_mag; - player.acc_y += std.math.sin(player.ang - PI / 2.0) * acc_mag; + player.acc_x += sin; + player.acc_y -= cos; } if (isKeyPressed(.Left)) { - player.acc_x -= std.math.cos(player.ang - PI / 2.0) * acc_mag; - player.acc_y -= std.math.sin(player.ang - PI / 2.0) * acc_mag; + player.acc_x -= sin; + player.acc_y += cos; } if (isKeyPressed(.Up) or isKeyPressed(.F)) { - player.acc_x += std.math.cos(player.ang) * acc_mag; - player.acc_y += std.math.sin(player.ang) * acc_mag; + player.acc_x += cos; + player.acc_y += sin; } if (isKeyPressed(.Down) or isKeyPressed(.S)) { - player.acc_x -= std.math.cos(player.ang) * acc_mag; - player.acc_y -= std.math.sin(player.ang) * acc_mag; + player.acc_x -= cos; + player.acc_y -= sin; + } + // normalise + const a = std.math.sqrt(player.acc_x * player.acc_x + player.acc_y * player.acc_y); + if (a > 0) { + player.acc_x *= acc_mag / a; + player.acc_y *= acc_mag / a; } // First the background @@ -177,6 +186,6 @@ pub fn main() !void { window.display(); - player.tick(); + player.tick(map); } } -- cgit v1.3.1