W1: Third-person character controller and camera

Simulation (src/sim/):
- SimGround holds the terrain heightfield (bilinear heights, normals)
  and axis-aligned box solids, with support, body-overlap, point and
  ray queries. The island generator and building will fill it.
- Movement steps a player per tick: walk, sprint (forward only),
  crouch (lower body, slower, no jump), jump with exact constant-gravity
  integration, air control, step-up onto ledges up to STEP_HEIGHT,
  ground snapping on downhill walks, slopes over MAX_SLOPE_DEGREES that
  block walking and jumping uphill and make players slide (speed capped),
  ceiling bumps, sliding along walls, and fall damage from impact speed.
- Gravity now applies on ticks with no command. on_ground and crouching
  are part of the state hash. New movement numbers are in Tuning.
- Fixed the strafe direction: right is forward x up in Godot's frame,
  (-cos yaw, 0, sin yaw). Documented the look convention.

Client (src/client/):
- CameraRig places an over-the-shoulder camera with an eased
  aim-down-sights blend (closer, narrower FOV), casting against the same
  SimGround: pivot to shoulder, shoulder back, then a straight line from
  the pivot, with a final clearance pull-in so the near plane never
  enters geometry.
- InputSettings: mouse sensitivity, aim sensitivity scale and invert-Y,
  clamped and saved to user://settings.cfg. PlayerCamera applies both.

Tests: 46 new across movement, ground queries, camera and settings,
including a 24,480-view scripted camera sweep through five arenas (wall,
corridor, low roof, corner, steep bank) that checks the eye and every
near-plane corner stay out of geometry with a clear line to the pivot.
The hash test now flips a high stick bit, because a 1/127 nudge to an
airborne player moves them less than the hash's millimetre.
Capture scenarios camera, camera_wall and camera_aim.

Closes #8

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 20:24:02 -05:00
co-authored by Claude Opus 5.5
parent 68635d5fba
commit 70e88cec8a
29 changed files with 1213 additions and 23 deletions
+153
View File
@@ -0,0 +1,153 @@
## Command-driven player movement: walk, sprint, crouch, jump, gravity, slopes,
## step-up, sliding and fall damage. Deterministic: it depends only on the entity, the
## command and the ground.
class_name Movement
extends RefCounted
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
## Advances one player by one tick. `command` may be null, meaning no input this tick:
## the player keeps looking the same way and stops trying to move, but still falls.
static func step(player: SimEntity, command: PlayerCommand, ground: SimGround) -> void:
var wish: Vector3 = Vector3.ZERO
var wants_jump: bool = false
player.crouching = false
if command != null:
player.yaw = command.yaw
player.pitch = command.pitch
player.crouching = command.is_pressed(PlayerCommand.BUTTON_CROUCH)
wants_jump = command.is_pressed(PlayerCommand.BUTTON_JUMP) and not player.crouching
wish = _wish_velocity(command, player.crouching)
var velocity: Vector3 = player.velocity
if player.on_ground:
velocity.x = wish.x
velocity.z = wish.z
velocity.y = 0.0
if wants_jump:
velocity.y = Tuning.JUMP_VELOCITY
player.on_ground = false
else:
velocity.x += (wish.x - velocity.x) * Tuning.AIR_CONTROL
velocity.z += (wish.z - velocity.z) * Tuning.AIR_CONTROL
velocity = _move_horizontally(player, velocity, ground)
_move_vertically(player, velocity, ground)
## Height of the player's body, which shrinks while crouching.
static func body_height(player: SimEntity) -> float:
return Tuning.CROUCH_HEIGHT if player.crouching else Tuning.PLAYER_HEIGHT
static func _wish_velocity(command: PlayerCommand, crouching: bool) -> Vector3:
var move: Vector2 = command.move_vector()
if move.length() > 1.0:
move = move.normalized()
var angle: float = command.yaw_radians()
var forward: Vector3 = Vector3(sin(angle), 0.0, cos(angle))
# Right is forward x up in Godot's right-handed, y-up frame.
var right: Vector3 = Vector3(-cos(angle), 0.0, sin(angle))
var speed: float = Tuning.WALK_SPEED
if crouching:
speed = Tuning.CROUCH_SPEED
elif command.is_pressed(PlayerCommand.BUTTON_SPRINT) and move.y > 0.0:
speed = Tuning.SPRINT_SPEED
return (right * move.x + forward * move.y) * speed
## Moves along x and z, sliding along whatever blocks the way. Returns the velocity with
## blocked components removed.
static func _move_horizontally(player: SimEntity, velocity: Vector3, ground: SimGround) -> Vector3:
var delta: Vector3 = Vector3(velocity.x, 0.0, velocity.z) * DT
if delta.is_zero_approx():
return velocity
var result: Vector3 = velocity
if _can_move_to(player, player.position + delta, ground):
player.position += delta
elif _can_move_to(player, player.position + Vector3(delta.x, 0.0, 0.0), ground):
player.position.x += delta.x
result.z = 0.0
elif _can_move_to(player, player.position + Vector3(0.0, 0.0, delta.z), ground):
player.position.z += delta.z
result.x = 0.0
else:
result.x = 0.0
result.z = 0.0
var size: float = Tuning.WORLD_SIZE_METRES
player.position.x = clampf(player.position.x, 0.0, size)
player.position.z = clampf(player.position.z, 0.0, size)
return result
static func _can_move_to(player: SimEntity, target: Vector3, ground: SimGround) -> bool:
var step: float = Tuning.STEP_HEIGHT if player.on_ground else 0.0
if ground.body_blocked(target, Tuning.PLAYER_RADIUS, body_height(player), step):
return false
# Moving uphill into terrain that's too steep is blocked, on foot or in the air, so
# nobody can climb a cliff by pushing into it. Downhill is always allowed.
var rise: float = ground.terrain_height(target.x, target.z) - player.position.y
if rise > 0.0 and not ground.walkable_terrain(target.x, target.z):
if not ground.support_is_solid(target.x, target.z, player.position.y + step):
return false
return true
static func _move_vertically(player: SimEntity, velocity: Vector3, ground: SimGround) -> void:
var feet: Vector3 = player.position
if player.on_ground:
var reach: float = feet.y + Tuning.STEP_HEIGHT
var support: float = ground.support_height(feet.x, feet.z, reach)
if support >= feet.y - Tuning.GROUND_SNAP:
player.position.y = support
player.velocity = Vector3(velocity.x, 0.0, velocity.z)
if not _walkable_support(ground, player.position):
player.on_ground = false
return
# Walked off a ledge: start falling from here.
player.on_ground = false
# Airborne. Exact integration under constant gravity.
var new_y: float = feet.y + velocity.y * DT - 0.5 * Tuning.GRAVITY * DT * DT
velocity.y -= Tuning.GRAVITY * DT
var head_room: Vector3 = Vector3(feet.x, new_y, feet.z)
if velocity.y > 0.0 and ground.body_blocked(head_room, Tuning.PLAYER_RADIUS, body_height(player), 0.0):
# Bumped a ceiling.
new_y = feet.y
velocity.y = 0.0
player.position.y = new_y
var support: float = ground.support_height(feet.x, feet.z, maxf(feet.y, new_y))
if new_y > support:
player.velocity = velocity
return
player.position.y = support
if _walkable_support(ground, player.position):
_land(player, -velocity.y)
velocity.y = 0.0
player.on_ground = true
else:
# Too steep to stand on: keep only the motion along the slope.
var normal: Vector3 = ground.terrain_normal(feet.x, feet.z)
var into: float = velocity.dot(normal)
if into < 0.0:
velocity -= normal * into
velocity = velocity.limit_length(Tuning.SLIDE_MAX_SPEED)
player.velocity = velocity
static func _walkable_support(ground: SimGround, feet: Vector3) -> bool:
if ground.support_is_solid(feet.x, feet.z, feet.y + 0.001):
return true
return ground.walkable_terrain(feet.x, feet.z)
## Applies fall damage for landing at `impact_speed` metres per second, downward.
static func _land(player: SimEntity, impact_speed: float) -> void:
if impact_speed <= 0.0:
return
var drop: float = impact_speed * impact_speed / (2.0 * Tuning.GRAVITY)
var excess: float = drop - Tuning.FALL_DAMAGE_SAFE_METRES
if excess > 0.0:
player.health = maxi(player.health - roundi(excess * Tuning.FALL_DAMAGE_PER_METRE), 0)
+1
View File
@@ -0,0 +1 @@
uid://b4wtn2is3k3td
+3
View File
@@ -11,6 +11,9 @@ var velocity: Vector3 = Vector3.ZERO
var yaw: int = 0
var pitch: int = 0
var health: int = 0
## Standing on walkable ground. False while jumping, falling or sliding.
var on_ground: bool = false
var crouching: bool = false
func _init(entity_id: int, entity_archetype: int) -> void:
+195
View File
@@ -0,0 +1,195 @@
## Everything solid that players stand on, walk into and see through: a terrain
## heightfield plus axis-aligned box solids (structures, rocks). The island generator
## fills the heightfield; building adds and removes solids.
##
## All queries are pure functions of the stored data, so the server, client prediction
## and the camera all agree.
class_name SimGround
extends RefCounted
## Ray-marching step for terrain, in metres. Small enough for camera-length rays.
const RAY_STEP: float = 0.05
const RAY_REFINE_STEPS: int = 10
const _MARGIN_OFFSETS: Array[Vector2] = [
Vector2(1.0, 0.0), Vector2(-1.0, 0.0), Vector2(0.0, 1.0), Vector2(0.0, -1.0),
Vector2(0.7071, 0.7071), Vector2(0.7071, -0.7071), Vector2(-0.7071, 0.7071), Vector2(-0.7071, -0.7071),
]
## Heights in row-major order: index = z * samples + x, `cell` metres apart, starting
## at the world origin. Outside the grid the nearest edge sample is used.
var heights: PackedFloat32Array = PackedFloat32Array()
var samples: int = 0
var cell: float = 1.0
var solids: Array[AABB] = []
## A flat world at `height`.
static func flat(height: float = 0.0) -> SimGround:
var ground: SimGround = SimGround.new()
ground.samples = 2
ground.cell = Tuning.WORLD_SIZE_METRES
ground.heights = PackedFloat32Array([height, height, height, height])
return ground
## A heightfield of `side` x `side` samples spaced `spacing` metres apart.
static func from_heights(values: PackedFloat32Array, side: int, spacing: float) -> SimGround:
assert(side >= 2 and values.size() == side * side and spacing > 0.0)
var ground: SimGround = SimGround.new()
ground.heights = values
ground.samples = side
ground.cell = spacing
return ground
func add_solid(box: AABB) -> void:
solids.append(box.abs())
## Terrain height at (x, z), bilinearly interpolated.
func terrain_height(x: float, z: float) -> float:
var limit: float = float(samples - 1)
var gx: float = clampf(x / cell, 0.0, limit)
var gz: float = clampf(z / cell, 0.0, limit)
var x0: int = mini(floori(gx), samples - 2)
var z0: int = mini(floori(gz), samples - 2)
var fx: float = gx - x0
var fz: float = gz - z0
var row: int = z0 * samples
var h00: float = heights[row + x0]
var h10: float = heights[row + x0 + 1]
var h01: float = heights[row + samples + x0]
var h11: float = heights[row + samples + x0 + 1]
return lerpf(lerpf(h00, h10, fx), lerpf(h01, h11, fx), fz)
## Upward terrain normal at (x, z), from central differences.
func terrain_normal(x: float, z: float) -> Vector3:
var d: float = minf(cell * 0.5, 0.5)
var dx: float = terrain_height(x + d, z) - terrain_height(x - d, z)
var dz: float = terrain_height(x, z + d) - terrain_height(x, z - d)
return Vector3(-dx, 2.0 * d, -dz).normalized()
## True if the terrain at (x, z) is gentle enough to stand on.
func walkable_terrain(x: float, z: float) -> bool:
return terrain_normal(x, z).y >= cos(deg_to_rad(Tuning.MAX_SLOPE_DEGREES))
## The highest surface under (x, z) that is no higher than `max_y`: the terrain, or the
## top of a solid. Solid tops are always walkable.
func support_height(x: float, z: float, max_y: float) -> float:
var best: float = terrain_height(x, z)
for box: AABB in solids:
var top: float = box.end.y
if top > best and top <= max_y and _inside_footprint(box, x, z, 0.0):
best = top
return best
## True when the best support under (x, z) up to `max_y` is the top of a solid.
func support_is_solid(x: float, z: float, max_y: float) -> bool:
var terrain: float = terrain_height(x, z)
for box: AABB in solids:
var top: float = box.end.y
if top > terrain and top <= max_y and _inside_footprint(box, x, z, 0.0):
return true
return false
## True if a player body at `feet` overlaps a solid. Solids whose top is within
## `step` of the feet don't count, so they can be stepped onto.
func body_blocked(feet: Vector3, radius: float, height: float, step: float) -> bool:
for box: AABB in solids:
if box.end.y <= feet.y + step or box.position.y >= feet.y + height:
continue
if _inside_footprint(box, feet.x, feet.z, radius):
return true
return false
## Casts from `from` to `to` against terrain and solids. Returns the fraction of the way
## to the first hit, or 1.0 if nothing is hit. A ray starting inside something returns 0.
func raycast(from: Vector3, to: Vector3) -> float:
var nearest: float = 1.0
for box: AABB in solids:
nearest = minf(nearest, _ray_box(from, to, box))
return minf(nearest, _ray_terrain(from, to, nearest))
## True if `point` is inside the terrain or a solid, or within `margin` of one. For the
## terrain, heights are also checked `margin` away on each side, so steep ground counts
## its sideways distance and not just the height under the point.
func point_inside(point: Vector3, margin: float = 0.0) -> bool:
if point.y < terrain_height(point.x, point.z) + margin:
return true
if margin > 0.0:
for offset: Vector2 in _MARGIN_OFFSETS:
var probe: Vector2 = offset * margin
if point.y < terrain_height(point.x + probe.x, point.z + probe.y):
return true
for box: AABB in solids:
if box.grow(margin).has_point(point):
return true
return false
func _inside_footprint(box: AABB, x: float, z: float, radius: float) -> bool:
return (
x > box.position.x - radius and x < box.end.x + radius
and z > box.position.z - radius and z < box.end.z + radius
)
## Slab test. Returns the entry fraction in [0, 1], or 1.0 for a miss.
func _ray_box(from: Vector3, to: Vector3, box: AABB) -> float:
var direction: Vector3 = to - from
var enter: float = 0.0
var leave: float = 1.0
for axis: int in 3:
var start: float = from[axis]
var span: float = direction[axis]
var low: float = box.position[axis]
var high: float = box.end[axis]
if absf(span) < 0.000001:
if start < low or start > high:
return 1.0
continue
var t1: float = (low - start) / span
var t2: float = (high - start) / span
enter = maxf(enter, minf(t1, t2))
leave = minf(leave, maxf(t1, t2))
if enter > leave:
return 1.0
return enter
## Marches the ray up to `limit`, then refines the first crossing below the terrain.
func _ray_terrain(from: Vector3, to: Vector3, limit: float) -> float:
var length: float = from.distance_to(to)
if length <= 0.0:
return 0.0 if _below_terrain(from) else 1.0
if _below_terrain(from):
return 0.0
var step: float = RAY_STEP / length
var previous: float = 0.0
var t: float = step
while previous < limit:
t = minf(t, limit)
if _below_terrain(from.lerp(to, t)):
var low: float = previous
var high: float = t
for _index: int in RAY_REFINE_STEPS:
var middle: float = (low + high) * 0.5
if _below_terrain(from.lerp(to, middle)):
high = middle
else:
low = middle
return low
previous = t
t += step
return 1.0
func _below_terrain(point: Vector3) -> bool:
return point.y < terrain_height(point.x, point.z)
+1
View File
@@ -0,0 +1 @@
uid://oxycp26a8vmr
+9 -21
View File
@@ -1,7 +1,7 @@
## The authoritative fixed-tick game state. It takes commands and a seed and nothing
## else: no scene tree, no wall clock, no unseeded randomness.
##
## Movement here is a flat placeholder until the character controller lands in v0.2.
## Movement rules live in Movement; the world's solid shape lives in `ground`.
class_name Simulation
extends RefCounted
@@ -10,6 +10,8 @@ const SECONDS_PER_TICK: float = 1.0 / Tuning.TICK_RATE_HZ
var tick: int = 0
var entities: EntityRegistry = EntityRegistry.new()
var rng: SimRng
## Terrain and solids. Flat until the island generator provides a heightfield.
var ground: SimGround = SimGround.flat(0.0)
## Player entity ID -> { command tick -> PlayerCommand }
var _pending: Dictionary = {}
@@ -26,7 +28,10 @@ func add_player() -> int:
var player: SimEntity = entities.create(SimEntity.ARCHETYPE_PLAYER)
var spawn: RandomNumberGenerator = rng.stream("spawn")
var size: float = Tuning.WORLD_SIZE_METRES
player.position = Vector3(spawn.randf() * size, 0.0, spawn.randf() * size)
var x: float = spawn.randf() * size
var z: float = spawn.randf() * size
player.position = Vector3(x, ground.terrain_height(x, z), z)
player.on_ground = true
player.health = Tuning.PLAYER_MAX_HEALTH
_players.append(player.id)
_pending[player.id] = {}
@@ -63,25 +68,7 @@ func step() -> void:
func _apply_command(player: SimEntity, command: PlayerCommand) -> void:
# With no command this tick, the player keeps looking the same way and stands still.
if command == null:
return
player.yaw = command.yaw
player.pitch = command.pitch
var move: Vector2 = command.move_vector()
if move.length() > 1.0:
move = move.normalized()
var angle: float = command.yaw_radians()
var forward: Vector3 = Vector3(sin(angle), 0.0, cos(angle))
var right: Vector3 = Vector3(cos(angle), 0.0, -sin(angle))
var speed: float = (
Tuning.SPRINT_SPEED if command.is_pressed(PlayerCommand.BUTTON_SPRINT)
else Tuning.WALK_SPEED
)
var delta: Vector3 = (right * move.x + forward * move.y) * speed * SECONDS_PER_TICK
var size: float = Tuning.WORLD_SIZE_METRES
player.position = (player.position + delta).clamp(Vector3.ZERO, Vector3(size, 0.0, size))
Movement.step(player, command, ground)
## SHA-256 hex digest of a canonical encoding of the whole state.
@@ -101,6 +88,7 @@ func state_hash() -> String:
buffer.put_32(entity.health)
buffer.put_32(entity.yaw)
buffer.put_32(entity.pitch)
buffer.put_u8((1 if entity.on_ground else 0) | (2 if entity.crouching else 0))
for value: float in [entity.position.x, entity.position.y, entity.position.z]:
buffer.put_64(roundi(value * 1000.0))
for value: float in [entity.velocity.x, entity.velocity.y, entity.velocity.z]:
+18
View File
@@ -30,6 +30,24 @@ const SWIM_SPEED: float = 2.0
const CROUCH_SPEED: float = 2.0
const JUMP_VELOCITY: float = 7.5
const GRAVITY: float = 20.0
## Fraction of the gap between current and wanted horizontal velocity closed per tick
## while airborne.
const AIR_CONTROL: float = 0.08
## Ledges up to this height are stepped onto without jumping.
const STEP_HEIGHT: float = 0.45
## Walking keeps the feet on ground that drops away by up to this much per tick.
const GROUND_SNAP: float = 0.35
## Steeper ground can't be walked up or stood on; players slide down it.
const MAX_SLOPE_DEGREES: float = 46.0
## Top speed sliding down ground too steep to stand on.
const SLIDE_MAX_SPEED: float = 8.0
const PLAYER_RADIUS: float = 0.4
const PLAYER_HEIGHT: float = 1.8
const CROUCH_HEIGHT: float = 1.2
## Landing from a drop of this many metres or less does no damage. Measured from impact
## speed, so sliding down a slope is gentler than falling the same height.
const FALL_DAMAGE_SAFE_METRES: float = 6.0
const FALL_DAMAGE_PER_METRE: float = 8.0
# The tide. `hold` and `rise` are seconds, `level` is metres above the starting sea,
# `drown_dps` is damage per second underwater once breath runs out. The last level