diff --git a/all.h b/all.h index b0dde0c46612b16b68f9bce5aab210766bc7620d..d4025cdc1032b2f462a88f0100666398eeb2d8b7 100644 --- a/all.h +++ b/all.h @@ -114,6 +114,7 @@ float pitch; float lean_amount; float crouch_amount; float horizontal_speed; + float camera_offset_y; // Vertical offset for stair smoothing } PlayerState; void UpdatePlayer(void); diff --git a/player.c b/player.c index 52788f107cf43eee2cac609e4ce81eb3f2a0a7f8..88b0e6c5459acd069166d16e2e98e7105160127a 100644 --- a/player.c +++ b/player.c @@ -47,7 +47,9 @@ RayCollision closestHit = { 0 }; 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 @@ } 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 @@ } else { 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) { @@ -163,6 +173,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 @@ Vector3 rotatedOffset = Vector3RotateByAxisAngle(neckToEye, bodyForward, leanAngle); 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),