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