From 16d9c5479e2091658ba254911e274dc16746a521 Mon Sep 17 00:00:00 2001 From: Nefrace Date: Wed, 17 Apr 2024 00:08:24 +0300 Subject: [PATCH] Bobbing --- src/main.odin | 19 +++---------------- src/player.odin | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/main.odin b/src/main.odin index 9b8a274..b7af8dd 100644 --- a/src/main.odin +++ b/src/main.odin @@ -56,23 +56,10 @@ main :: proc() { ClearBackground(PAL[0]) rlPushMatrix() rlRotatef(45, 0, 1, 0) - DrawMesh(plane, mat, rl.MatrixIdentity()) - rlPopMatrix() - - - rlPushMatrix() - rlTranslatef(player.position.x, player.position.y, player.position.z) - rlTranslatef(player.cameraOffset.x, player.cameraOffset.y, player.cameraOffset.z) - rlRotatef(math.to_degrees(player.direction), 0, 1, 0) - rlRotatef(math.to_degrees(-player.yaw), 1, 0, 0) - rlTranslatef(-1, -0.3, 1) - rlScalef(0.5, 0.5, 0.5) - DrawCube({0, 0, 0}, 1,1,1, PAL[4]) - rlTranslatef(0, 0, 1) - rlScalef(0.4, 0.4, 1) - DrawCube({0, 0, 0}, 1,1,1, PAL[4]) - + DrawMesh(plane, mat, rl.Matrix(1)) rlPopMatrix() + + player_draw(&player) EndMode3D() EndDrawing() diff --git a/src/player.odin b/src/player.odin index 862f02e..c936cca 100644 --- a/src/player.odin +++ b/src/player.odin @@ -8,6 +8,9 @@ Player :: struct { cameraOffset: Vec3, direction: f32, yaw: f32, + bob_timer: f64, + bob_position: Vec3, + bob_factor: f32, camera: rl.Camera3D, } @@ -26,6 +29,7 @@ player_create :: proc() -> Player { player_update :: proc(using player: ^Player, delta: f32) { using rl + bob_timer += f64(delta) mouseDelta := GetMouseDelta() direction -= mouseDelta.x * 0.003 @@ -44,15 +48,41 @@ player_update :: proc(using player: ^Player, delta: f32) { if IsKeyDown(KeyboardKey.A) {motion += right * 0.1} if IsKeyDown(KeyboardKey.D) {motion -= right * 0.1} motion = rl.Vector3ClampValue(motion, 0, 0.2) - velocity.xz = motion.xz - position += velocity + if Vector3Length(motion) != 0 { + bob_factor = min(bob_factor + delta * 3, 1) + } else { + bob_factor = max(bob_factor - delta * 5, 0) + } + velocity.xz = Vector3MoveTowards(velocity, motion * 100, 100 * delta).xz + position += velocity * delta - camera.position = position + cameraOffset - + bob_position = Vec3{math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI/4)), math.sin_f32(f32(bob_timer*math.PI * 4)), 0} * bob_factor * 0.3 + bob_position = Vector3RotateByAxisAngle(bob_position, Vec3{0,1,0}, direction) + + camera.position = position + cameraOffset + bob_position target := Vec3{ math.sin(direction) * math.cos(yaw), math.sin(yaw), math.cos(direction) * math.cos(yaw), } - camera.target = position + cameraOffset + target + camera.target = position + cameraOffset + target + bob_position +} + +player_draw :: proc(using player: ^Player) { + using rl + + + rlPushMatrix() + offset := position + cameraOffset + bob_position + rlTranslatef(offset.x, offset.y, offset.z) + rlRotatef(math.to_degrees(player.direction), 0, 1, 0) + rlRotatef(math.to_degrees(-player.yaw), 1, 0, 0) + rlTranslatef(-1, -0.3, 1) + rlScalef(0.5, 0.5, 0.5) + DrawCube({0, 0, 0}, 1,1,1, PAL[4]) + rlTranslatef(0, 0, 1) + rlScalef(0.4, 0.4, 1) + DrawCube({0, 0, 0}, 1,1,1, PAL[4]) + + rlPopMatrix() }