From 036f8075f5f62736cc967a520bfbcbf26a9ed644 Mon Sep 17 00:00:00 2001 From: paulaboks Date: Mon, 28 Sep 2026 12:01:51 -0300 Subject: [PATCH] Movement copied from the minecraft wiki :3 --- client/camera.ts | 5 +- client/entity/entity.ts | 18 +++++- client/entity/local_player.ts | 66 +++++++++++++++++++-- server/game/game_server.ts | 4 +- tests/local_player_test.ts | 108 +++++++++++++++++++++++++++++++++- 5 files changed, 189 insertions(+), 12 deletions(-) diff --git a/client/camera.ts b/client/camera.ts index 634b5c5..65d1125 100644 --- a/client/camera.ts +++ b/client/camera.ts @@ -10,7 +10,9 @@ export class Camera { yaw = 0; roll = 0; - fov = Math.PI / 3; + // vertical, minecraft's default 70 degrees. fov is that, widened by the entity it's on + base_fov = 70 * Math.PI / 180; + fov = this.base_fov; near = 0.1; far = 1000; @@ -21,5 +23,6 @@ export class Camera { this.z = z; this.yaw = entity.yaw; this.pitch = entity.pitch; + this.fov = this.base_fov * entity.render_fov_scale(partial_tick); } } diff --git a/client/entity/entity.ts b/client/entity/entity.ts index 03f2da5..40c5546 100644 --- a/client/entity/entity.ts +++ b/client/entity/entity.ts @@ -82,20 +82,32 @@ export abstract class Entity { return this.eye_height; } + // how much wider the view is from its eyes, like minecraft widening it while sprinting + render_fov_scale(_partial_tick: number) { + return 1; + } + // one step of the game, TICK_DELTA seconds abstract tick(): void; - // falls and moves by its velocity for one tick (see move_body), then slows down from drag + // what's left of its sideways speed after each tick, from where it was at the start of the tick like minecraft, + // so the tick it jumps still has the ground's friction + horizontal_friction() { + return this.drag; + } + + // falls and moves by its velocity for one tick (see move_body), then slows down from drag and friction move() { const is_solid = (x: number, y: number, z: number) => this.level.has_collision(x, y, z); const can_step = this.#air_ticks <= this.step_grace_ticks && this.vy <= 0; + const friction = this.horizontal_friction(); const collisions = move_body(this, this.gravity, is_solid, this.step_height, can_step); this.colliding_x = collisions.x; this.colliding_y = collisions.y; this.colliding_z = collisions.z; this.#air_ticks = this.on_ground ? 0 : this.#air_ticks + 1; - this.vx *= this.drag; + this.vx *= friction; this.vy *= this.drag; - this.vz *= this.drag; + this.vz *= friction; } } diff --git a/client/entity/local_player.ts b/client/entity/local_player.ts index 1ce99bc..96a191a 100644 --- a/client/entity/local_player.ts +++ b/client/entity/local_player.ts @@ -11,6 +11,28 @@ const SEND_POSITION_TICKS = 2; // ticks after a jump before holding jump can jump again, like minecraft's noJumpDelay. a jump is longer than this in // the air, so holding it jumps again as soon as it lands const JUMP_DELAY_TICKS = 10; +// like minecraft, walking isn't capped: each tick the keys add this much speed, it moves, then friction takes some +// away, so it settles where they even out, at acceleration / (1 - friction). on the ground that's 0.098 blocks per +// tick and 0.546 friction, 4.317 blocks a second. in the air it's 0.0196 and 0.91, so it keeps its speed from the +// ground but can't steer much +const GROUND_ACCELERATION = 0.098 / TICK_DELTA; +const GROUND_FRICTION = 0.546; +const AIR_ACCELERATION = 0.0196 / TICK_DELTA; +const AIR_FRICTION = 0.91; +// slower than this (0.003 blocks a tick) it stops, instead of creeping forever +const MIN_SPEED = 0.003 / TICK_DELTA; +// sprinting is 30% faster than walking, 5.612 blocks a second +const SPRINT_SPEED = 1.3; +// jumping while sprinting adds 0.2 blocks a tick forward, which averages about 7.1 blocks a second +const SPRINT_JUMP_BOOST = 0.2 / TICK_DELTA; +// like minecraft, it can't start sprinting with this much food or less, and stops when it gets there +const SPRINT_MIN_FOOD = 6; +// running into a wall stops sprinting, unless it's looking less than this far into it (sin of 8 degrees), so +// sprinting along a wall keeps going +const SPRINT_WALL_ANGLE = Math.sin(8 * Math.PI / 180); +// the fov while sprinting, halfway between walking and the speed, eased there by half each tick like minecraft +const SPRINT_FOV = (1 + SPRINT_SPEED) / 2; +const FOV_EASE = 0.5; // like minecraft: sneaking is 30% of walking speed and lowers the eyes 0.35, which ease there by half each tick const SNEAK_SPEED = 0.3; const SNEAK_EYE_HEIGHT = PLAYER_EYE_HEIGHT - 0.35; @@ -39,7 +61,6 @@ export class LocalPlayer extends Player { food = 20; saturation = 5; - move_speed = 4; // blocks per second, peaks about 1.25 blocks up with the entity's gravity and drag jump_force = 9.23; @@ -47,6 +68,10 @@ export class LocalPlayer extends Player { // counts down every tick, letting go of jump clears it #jump_delay = 0; sneaking = false; + // stays on after letting go of the key, see #update_sprinting + sprinting = false; + #fov_scale = 1; + #prev_fov_scale = 1; #prev_eye_height = PLAYER_EYE_HEIGHT; constructor(client: Client, id: string, name: string) { @@ -59,6 +84,8 @@ export class LocalPlayer extends Player { this.#prev_eye_height = this.eye_height; const eye_height = this.sneaking ? SNEAK_EYE_HEIGHT : PLAYER_EYE_HEIGHT; this.eye_height += (eye_height - this.eye_height) * EYE_EASE; + this.#prev_fov_scale = this.#fov_scale; + this.#fov_scale += ((this.sprinting ? SPRINT_FOV : 1) - this.#fov_scale) * FOV_EASE; const was_on_ground = this.on_ground; // not while jumping, so it can still jump off a ledge @@ -75,6 +102,8 @@ export class LocalPlayer extends Player { this.vz = dz / TICK_DELTA; } this.move(); + if (Math.abs(this.vx) < MIN_SPEED) this.vx = 0; + if (Math.abs(this.vz) < MIN_SPEED) this.vz = 0; this.#send_position(this.on_ground && !was_on_ground); } @@ -87,6 +116,15 @@ export class LocalPlayer extends Player { this.pitch = Math.max(-limit, Math.min(limit, this.pitch)); } + override horizontal_friction() { + return this.on_ground ? GROUND_FRICTION : AIR_FRICTION; + } + + // how much wider the view is than the fov option, between the last tick and this one + override render_fov_scale(partial_tick: number) { + return this.#prev_fov_scale + (this.#fov_scale - this.#prev_fov_scale) * partial_tick; + } + // the eyes between the last tick and this one, for the camera override render_eye_height(partial_tick: number) { return this.#prev_eye_height + (this.eye_height - this.#prev_eye_height) * partial_tick; @@ -116,10 +154,12 @@ export class LocalPlayer extends Player { this.sneaking = input.sneak; const sin = Math.sin(this.yaw); const cos = Math.cos(this.yaw); - const speed = this.move_speed * (input.sneak ? SNEAK_SPEED : input.sprint ? 1.75 : 1); + this.#update_sprinting(input, sin, cos); + const acceleration = (this.on_ground ? GROUND_ACCELERATION : AIR_ACCELERATION) * + (input.sneak ? SNEAK_SPEED : this.sprinting ? SPRINT_SPEED : 1); - this.vx = (sin * input_z + cos * input_x) * speed; - this.vz = (cos * input_z - sin * input_x) * speed; + this.vx += (sin * input_z + cos * input_x) * acceleration; + this.vz += (cos * input_z - sin * input_x) * acceleration; this.#jump_delay = Math.max(0, this.#jump_delay - 1); if (!input.jump) { @@ -127,6 +167,24 @@ export class LocalPlayer extends Player { } else if (this.on_ground && this.#jump_delay === 0) { this.vy += this.jump_force; this.#jump_delay = JUMP_DELAY_TICKS; + // like minecraft, a sprinting jump pushes it forward, the way it's looking + if (this.sprinting) { + this.vx -= sin * SPRINT_JUMP_BOOST; + this.vz -= cos * SPRINT_JUMP_BOOST; + } + } + } + + // like minecraft's LocalPlayer.aiStep: the key starts sprinting while moving forward, and it keeps sprinting + // until it stops moving forward, sneaks, gets hungry or runs into a wall, even after letting go of the key + #update_sprinting(input: MoveInput, sin: number, cos: number) { + const can_sprint = input.z < 0 && !input.sneak && this.food > SPRINT_MIN_FOOD; + // how far it's looking into what it hit in the last move, the way it's facing is -sin, -cos + const into_wall = sin * this.colliding_x + cos * this.colliding_z; + if (!can_sprint || into_wall > SPRINT_WALL_ANGLE) { + this.sprinting = false; + } else if (input.sprint) { + this.sprinting = true; } } diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 1481f43..f76e33e 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -69,8 +69,8 @@ const MAX_GIVE = 64 * 36; const MAX_SUGGESTIONS = 100; // falls up to this many blocks don't hurt, like minecraft const SAFE_FALL = 4; -// faster than walking (4 blocks a second) and slower than sprinting (7) -const SPRINT_SPEED = 5.5; +// faster than walking (4.3 blocks a second) and slower than sprinting (5.6) +const SPRINT_SPEED = 5; // moving further than this at once is a teleport, not a fall const TELEPORT_DISTANCE = 20; // how close feet have to be to a block's top to stand on it diff --git a/tests/local_player_test.ts b/tests/local_player_test.ts index 42b7a6e..5b9539f 100644 --- a/tests/local_player_test.ts +++ b/tests/local_player_test.ts @@ -1,4 +1,4 @@ -import { assert, assertEquals } from "@std/assert"; +import { assert, assertAlmostEquals, assertEquals } from "@std/assert"; import { LocalPlayer } from "$/client/entity/local_player.ts"; import { InputManager } from "$/client/input_manager.ts"; import { Options } from "$/client/options.ts"; @@ -92,13 +92,117 @@ Deno.test("the position is sent on the tick it lands, so the server sees it touc assert(landings >= 5, `landed ${landings} times`); }); +Deno.test("walking speeds up until friction evens it out, at minecraft's speeds", () => { + for (const [keys, speed] of [[[], 4.317], [["ControlLeft"], 5.612], [["ShiftLeft"], 1.295]] as const) { + const { player } = setup(); + run(player, 1, () => false); + InputManager.keys_down.add("KeyW"); + for (const key of keys) InputManager.keys_down.add(key); + player.tick(); + assert(-player.vz < speed / 2, "doesn't start at full speed"); + for (let tick = 0; tick < 40; tick++) player.tick(); + const z = player.z; + player.tick(); + InputManager.keys_down.clear(); + assertAlmostEquals((z - player.z) * 20, speed, 0.01); + } +}); + +Deno.test("jumping while sprinting is faster, and turning while sprinting is slower", () => { + // average speed over 200 ticks holding w and ctrl, and maybe space, turning this much each tick + const average = (jump: boolean, turn: number) => { + const { player } = setup(); + run(player, 1, () => false); + InputManager.keys_down.add("KeyW"); + InputManager.keys_down.add("ControlLeft"); + if (jump) InputManager.keys_down.add("Space"); + let distance = 0; + for (let tick = 0; tick < 240; tick++) { + player.yaw += turn; + const { x, z } = player; + player.tick(); + if (tick >= 40) distance += Math.hypot(player.x - x, player.z - z); + } + InputManager.keys_down.clear(); + return distance / 10; + }; + const sprinting = average(false, 0); + const jumping = average(true, 0); + assertAlmostEquals(sprinting, 5.612, 0.01); + assertAlmostEquals(jumping, 7.127, 0.05); + assert(average(false, 0.1) < sprinting - 0.05, "turning slows it down"); +}); + +Deno.test("sprinting starts going forward and keeps going until it stops, sneaks, gets hungry or hits a wall", () => { + const keys = (...keys: string[]) => { + InputManager.keys_down.clear(); + for (const key of keys) InputManager.keys_down.add(key); + }; + const { player } = setup(); + run(player, 1, () => false); + + // not backwards or sideways + keys("KeyS", "ControlLeft"); + player.tick(); + keys("KeyA", "ControlLeft"); + player.tick(); + assert(!player.sprinting, "sprinting without going forward"); + + // letting go of the key keeps it, letting go of forward stops it + keys("KeyW", "ControlLeft"); + player.tick(); + keys("KeyW"); + player.tick(); + assert(player.sprinting, "letting go of sprint stopped it"); + keys(); + player.tick(); + assert(!player.sprinting, "still sprinting after stopping"); + + // sneaking stops it + keys("KeyW", "ControlLeft"); + player.tick(); + keys("KeyW", "ControlLeft", "ShiftLeft"); + player.tick(); + assert(!player.sprinting, "sprinting while sneaking"); + + // too hungry to start, and getting hungry stops it + player.food = 6; + keys("KeyW", "ControlLeft"); + player.tick(); + assert(!player.sprinting, "sprinting while hungry"); + player.food = 20; + player.tick(); + assert(player.sprinting); + player.food = 6; + player.tick(); + assert(!player.sprinting, "kept sprinting after getting hungry"); + InputManager.keys_down.clear(); + + // running into a wall stops it, running along one doesn't + for (const [yaw, stops] of [[0, true], [Math.PI / 2, false]] as const) { + const { player } = setup(); + const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean }; + // a wall on the -z side + level.has_collision = (_x, y, z) => y < 0 || z < -2; + player.set_position(0.5, 0, -1); + player.yaw = yaw; + run(player, 1, () => false); + keys("KeyW", "ControlLeft"); + if (yaw !== 0) keys("KeyW", "ControlLeft", "KeyD"); + for (let tick = 0; tick < 20; tick++) player.tick(); + InputManager.keys_down.clear(); + assertEquals(player.sprinting, !stops, `yaw ${yaw}`); + } +}); + Deno.test("opening a screen while walking stops the player", () => { const { player } = setup(); const client = player.client as unknown as { screen: unknown }; InputManager.keys_down.add("KeyW"); for (let tick = 0; tick < 5; tick++) player.tick(); client.screen = {}; - player.tick(); + // it slides a little before friction stops it + for (let tick = 0; tick < 20; tick++) player.tick(); const z = player.z; for (let tick = 0; tick < 20; tick++) player.tick(); InputManager.keys_down.clear();