From e08b836ffcc98c79158f493c48caf34c23a1fd3d Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Fri, 1 May 2026 04:47:33 +0200 Subject: Smooth walking up the stairs --- player.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'player.c') diff --git a/player.c b/player.c index 52788f1..88b0e6c 100644 --- a/player.c +++ b/player.c @@ -47,7 +47,9 @@ static void MoveNormal(float dt) { closestHit.distance = FLT_MAX; bool hitFound = false; - float heights[] = { -eyeHeight + 10.0f, -eyeHeight / 2.0f, 0.0f }; + // Cast rays at different heights to check for obstacles. + // The bottom ray is raised slightly to allow stepping over small ledges/stairs. + float heights[] = { -eyeHeight + 18.0f, -eyeHeight / 2.0f, 0.0f }; Vector3 currentPos = game.player.pos; for (int i = 0; i < 3; i++) { @@ -91,6 +93,7 @@ static void MoveNormal(float dt) { float verticalMove = game.player.velocity.y * dt; Vector3 vStart = game.player.pos; + float oldY = game.player.pos.y; // Record Y for stair smoothing snap detection if (verticalMove < 0) { Vector3 start = vStart; @@ -129,6 +132,13 @@ static void MoveNormal(float dt) { game.player.is_grounded = false; } } + + // Stair smoothing: detect if the player position snapped vertically (e.g. stepping up a stair) + // and apply a counter-offset to the camera to keep the movement visually smooth. + float deltaY = game.player.pos.y - oldY; + if (game.player.is_grounded && deltaY != 0) { + game.player.camera_offset_y -= deltaY; + } } static void MoveFly(float dt) { @@ -164,6 +174,10 @@ void UpdatePlayer(void) { float dt = GetFrameTime(); Vector3 oldPos = game.player.pos; + // Decay the stair smoothing offset over time + game.player.camera_offset_y = Lerp(game.player.camera_offset_y, 0.0f, 15.0f * dt); + if (fabsf(game.player.camera_offset_y) < 0.001f) game.player.camera_offset_y = 0.0f; + if (IsKeyPressed(KEY_F)) { game.player.move_mode = (game.player.move_mode == MOVE_NORMAL) ? MOVE_FLY : MOVE_NORMAL; } @@ -206,6 +220,7 @@ void UpdatePlayer(void) { Vector3 pivot = Vector3Subtract(game.player.pos, (Vector3){ 0, pivotDist, 0 }); game.camera.position = Vector3Add(pivot, rotatedOffset); + game.camera.position.y += game.player.camera_offset_y; Vector3 forward = { cosf(game.player.pitch) * sinf(game.player.yaw), -- cgit v1.2.3