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,92 @@
|
||||
## Over-the-shoulder third-person camera placement, with collision and aim-down-sights.
|
||||
## Pure logic: it reads the same SimGround the simulation uses, so the camera can't see
|
||||
## through anything the player can't walk through. PlayerCamera applies the result.
|
||||
class_name CameraRig
|
||||
extends RefCounted
|
||||
|
||||
const PIVOT_HEIGHT: float = 1.6
|
||||
const CROUCH_PIVOT_HEIGHT: float = 1.05
|
||||
const SHOULDER: float = 0.6
|
||||
const AIM_SHOULDER: float = 0.8
|
||||
const DISTANCE: float = 3.4
|
||||
const AIM_DISTANCE: float = 1.8
|
||||
const FOV: float = 72.0
|
||||
const AIM_FOV: float = 48.0
|
||||
## Clearance kept between the camera and any surface, larger than the near plane's
|
||||
## half-extent so the near plane never cuts into geometry.
|
||||
const MARGIN: float = 0.25
|
||||
const NEAR: float = 0.05
|
||||
## Minimum distance from any surface, enough to keep the whole near plane outside it.
|
||||
const CLEARANCE: float = 0.1
|
||||
const CLEAR_STEPS: int = 16
|
||||
## Seconds to move most of the way between hip and aim.
|
||||
const AIM_BLEND_SECONDS: float = 0.12
|
||||
|
||||
## 0 at the hip, 1 fully aimed. Advanced by `update_aim`.
|
||||
var aim_blend: float = 0.0
|
||||
|
||||
|
||||
## Eases aim_blend towards aiming or not over `delta` seconds.
|
||||
func update_aim(aiming: bool, delta: float) -> void:
|
||||
var target: float = 1.0 if aiming else 0.0
|
||||
var rate: float = 1.0 - exp(-delta * 3.0 / AIM_BLEND_SECONDS)
|
||||
aim_blend = lerpf(aim_blend, target, rate)
|
||||
if absf(aim_blend - target) < 0.001:
|
||||
aim_blend = target
|
||||
|
||||
|
||||
## The look direction for a yaw and pitch, matching the simulation's convention that
|
||||
## forward at yaw 0 is +z and positive pitch looks up.
|
||||
static func look_direction(yaw: float, pitch: float) -> Vector3:
|
||||
return Vector3(sin(yaw) * cos(pitch), sin(pitch), cos(yaw) * cos(pitch))
|
||||
|
||||
|
||||
## The point the camera orbits, above the player's feet.
|
||||
static func pivot(feet: Vector3, crouching: bool) -> Vector3:
|
||||
return feet + Vector3.UP * (CROUCH_PIVOT_HEIGHT if crouching else PIVOT_HEIGHT)
|
||||
|
||||
|
||||
## Solves the camera transform for a player. The camera looks along the aim direction
|
||||
## from behind and to the right of the pivot, pulled in where geometry is in the way.
|
||||
func solve(feet: Vector3, yaw: float, pitch: float, crouching: bool, ground: SimGround) -> Transform3D:
|
||||
var look: Vector3 = look_direction(yaw, pitch)
|
||||
var right: Vector3 = Vector3(-cos(yaw), 0.0, sin(yaw))
|
||||
var shoulder: float = lerpf(SHOULDER, AIM_SHOULDER, aim_blend)
|
||||
var distance: float = lerpf(DISTANCE, AIM_DISTANCE, aim_blend)
|
||||
|
||||
var origin: Vector3 = pivot(feet, crouching)
|
||||
var beside: Vector3 = _cast(ground, origin, origin + right * shoulder)
|
||||
var eye: Vector3 = _cast(ground, beside, beside - look * distance)
|
||||
# The two legs can each be clear while the camera ends up over a wall top, where the
|
||||
# wall hides the player. Keep a clear straight line back to the pivot as well.
|
||||
eye = _cast(ground, origin, eye)
|
||||
eye = _clear(ground, eye, origin)
|
||||
var basis: Basis = Basis.looking_at(look, Vector3.UP if absf(look.y) < 0.999 else Vector3.FORWARD)
|
||||
return Transform3D(basis, eye)
|
||||
|
||||
|
||||
func fov() -> float:
|
||||
return lerpf(FOV, AIM_FOV, aim_blend)
|
||||
|
||||
|
||||
## Moves from `from` towards `to`, stopping MARGIN short of the first surface.
|
||||
static func _cast(ground: SimGround, from: Vector3, to: Vector3) -> Vector3:
|
||||
var length: float = from.distance_to(to)
|
||||
if length <= 0.0:
|
||||
return from
|
||||
var direction: Vector3 = (to - from) / length
|
||||
# Cast MARGIN past the end, so an end point just short of a wall is pulled in too.
|
||||
var reach: float = length + MARGIN
|
||||
var hit: float = ground.raycast(from, from + direction * reach)
|
||||
return from + direction * clampf(hit * reach - MARGIN, 0.0, length)
|
||||
|
||||
|
||||
## A ray can graze a surface it never hits. If the eye ended up within the near plane's
|
||||
## clearance of anything, slide it back towards `safe` until it's clear.
|
||||
static func _clear(ground: SimGround, eye: Vector3, safe: Vector3) -> Vector3:
|
||||
var point: Vector3 = eye
|
||||
for step: int in CLEAR_STEPS:
|
||||
if not ground.point_inside(point, CLEARANCE):
|
||||
return point
|
||||
point = eye.lerp(safe, float(step + 1) / CLEAR_STEPS)
|
||||
return safe
|
||||
@@ -0,0 +1 @@
|
||||
uid://bffma7fh66ygi
|
||||
@@ -0,0 +1,60 @@
|
||||
## Player-adjustable look settings, saved in the user's config file.
|
||||
class_name InputSettings
|
||||
extends RefCounted
|
||||
|
||||
const DEFAULT_PATH: String = "user://settings.cfg"
|
||||
const SECTION: String = "input"
|
||||
## Radians of turn per pixel of mouse motion at sensitivity 1.0.
|
||||
const RADIANS_PER_PIXEL: float = 0.0025
|
||||
const MIN_SENSITIVITY: float = 0.05
|
||||
const MAX_SENSITIVITY: float = 10.0
|
||||
## Looking straight up or down flips the camera basis, so stop just short.
|
||||
const PITCH_LIMIT: float = deg_to_rad(85.0)
|
||||
|
||||
var mouse_sensitivity: float = 1.0:
|
||||
set(value):
|
||||
mouse_sensitivity = clampf(value, MIN_SENSITIVITY, MAX_SENSITIVITY)
|
||||
## Multiplies the sensitivity while aiming down sights.
|
||||
var aim_sensitivity_scale: float = 0.6:
|
||||
set(value):
|
||||
aim_sensitivity_scale = clampf(value, 0.1, 2.0)
|
||||
var invert_y: bool = false
|
||||
|
||||
|
||||
## Applies a mouse motion to a look direction. Returns Vector2(yaw, pitch) in radians.
|
||||
## Moving the mouse right turns right; moving it up looks up, unless invert_y is set.
|
||||
func apply_mouse(relative: Vector2, yaw: float, pitch: float, aiming: bool) -> Vector2:
|
||||
var scale: float = RADIANS_PER_PIXEL * mouse_sensitivity
|
||||
if aiming:
|
||||
scale *= aim_sensitivity_scale
|
||||
var vertical: float = relative.y if invert_y else -relative.y
|
||||
var new_yaw: float = fposmod(yaw - relative.x * scale, TAU)
|
||||
var new_pitch: float = clampf(pitch + vertical * scale, -PITCH_LIMIT, PITCH_LIMIT)
|
||||
return Vector2(new_yaw, new_pitch)
|
||||
|
||||
|
||||
func save(path: String = DEFAULT_PATH) -> Error:
|
||||
var config: ConfigFile = ConfigFile.new()
|
||||
config.load(path) # Keep other sections; a missing file is fine.
|
||||
config.set_value(SECTION, "mouse_sensitivity", mouse_sensitivity)
|
||||
config.set_value(SECTION, "aim_sensitivity_scale", aim_sensitivity_scale)
|
||||
config.set_value(SECTION, "invert_y", invert_y)
|
||||
return config.save(path)
|
||||
|
||||
|
||||
## Loads saved settings. Missing or malformed values keep their defaults.
|
||||
static func load_from(path: String = DEFAULT_PATH) -> InputSettings:
|
||||
var settings: InputSettings = InputSettings.new()
|
||||
var config: ConfigFile = ConfigFile.new()
|
||||
if config.load(path) != OK:
|
||||
return settings
|
||||
var sensitivity: Variant = config.get_value(SECTION, "mouse_sensitivity", 1.0)
|
||||
if sensitivity is float or sensitivity is int:
|
||||
settings.mouse_sensitivity = float(sensitivity)
|
||||
var aim_scale: Variant = config.get_value(SECTION, "aim_sensitivity_scale", 0.6)
|
||||
if aim_scale is float or aim_scale is int:
|
||||
settings.aim_sensitivity_scale = float(aim_scale)
|
||||
var invert: Variant = config.get_value(SECTION, "invert_y", false)
|
||||
if invert is bool:
|
||||
settings.invert_y = invert
|
||||
return settings
|
||||
@@ -0,0 +1 @@
|
||||
uid://bn4kidspsub2m
|
||||
@@ -0,0 +1,42 @@
|
||||
## The local player's third-person camera. Turns mouse motion into a look direction using
|
||||
## InputSettings, and places itself each frame with CameraRig, so it never clips through
|
||||
## terrain or structures. The input layer reads `yaw` and `pitch` when it builds the
|
||||
## player's commands.
|
||||
class_name PlayerCamera
|
||||
extends Camera3D
|
||||
|
||||
var settings: InputSettings = InputSettings.new()
|
||||
var rig: CameraRig = CameraRig.new()
|
||||
var ground: SimGround = SimGround.flat()
|
||||
## Radians, in the simulation's convention (see docs/ARCHITECTURE.md).
|
||||
var yaw: float = 0.0
|
||||
var pitch: float = 0.0
|
||||
var aiming: bool = false
|
||||
## Where the followed player's feet are, and whether they're crouching. Set by whoever
|
||||
## owns the player's presentation, from interpolated snapshots.
|
||||
var target_feet: Vector3 = Vector3.ZERO
|
||||
var target_crouching: bool = false
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
near = CameraRig.NEAR
|
||||
fov = CameraRig.FOV
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
var motion: InputEventMouseMotion = event as InputEventMouseMotion
|
||||
if motion != null and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
|
||||
var look: Vector2 = settings.apply_mouse(motion.relative, yaw, pitch, aiming)
|
||||
yaw = look.x
|
||||
pitch = look.y
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
rig.update_aim(aiming, delta)
|
||||
snap()
|
||||
|
||||
|
||||
## Places the camera for the current target and look direction immediately.
|
||||
func snap() -> void:
|
||||
global_transform = rig.solve(target_feet, yaw, pitch, target_crouching, ground)
|
||||
fov = rig.fov()
|
||||
@@ -0,0 +1 @@
|
||||
uid://bkvavm0kwaxr5
|
||||
@@ -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)
|
||||
@@ -0,0 +1 @@
|
||||
uid://b4wtn2is3k3td
|
||||
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
@@ -0,0 +1 @@
|
||||
uid://oxycp26a8vmr
|
||||
+9
-21
@@ -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]:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user