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:
@@ -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)
|
||||
Reference in New Issue
Block a user