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,134 @@
|
||||
extends TestCase
|
||||
|
||||
const ASPECT: float = 16.0 / 9.0
|
||||
|
||||
|
||||
func test_open_ground_places_camera_behind_and_right() -> void:
|
||||
var rig: CameraRig = CameraRig.new()
|
||||
var feet: Vector3 = Vector3(100.0, 0.0, 100.0)
|
||||
var view: Transform3D = rig.solve(feet, 0.0, 0.0, false, SimGround.flat())
|
||||
var pivot: Vector3 = CameraRig.pivot(feet, false)
|
||||
# Yaw 0 faces +z, so the camera sits on -z, and screen-right is -x.
|
||||
var expected: Vector3 = pivot + Vector3(-CameraRig.SHOULDER, 0.0, -CameraRig.DISTANCE)
|
||||
assert_true(view.origin.distance_to(expected) < 0.001, "camera at %s" % view.origin)
|
||||
assert_true((-view.basis.z).distance_to(Vector3(0.0, 0.0, 1.0)) < 0.001, "camera must look along +z")
|
||||
|
||||
|
||||
func test_strafing_right_moves_towards_screen_right() -> void:
|
||||
for yaw: float in [0.0, 1.0, 2.5, 4.0, 5.5]:
|
||||
var view: Transform3D = CameraRig.new().solve(Vector3(500.0, 0.0, 500.0), yaw, 0.0, false, SimGround.flat())
|
||||
var player: SimEntity = SimEntity.new(1, SimEntity.ARCHETYPE_PLAYER)
|
||||
player.position = Vector3(500.0, 0.0, 500.0)
|
||||
player.on_ground = true
|
||||
Movement.step(player, PlayerCommand.from_input(0, Vector2(1.0, 0.0), yaw, 0.0), SimGround.flat())
|
||||
var moved: Vector3 = (player.position - Vector3(500.0, 0.0, 500.0)).normalized()
|
||||
assert_true(moved.dot(view.basis.x) > 0.99, "yaw %f: strafe right went %s" % [yaw, moved])
|
||||
# And walking forward goes where the camera looks.
|
||||
var walker: SimEntity = SimEntity.new(2, SimEntity.ARCHETYPE_PLAYER)
|
||||
walker.position = Vector3(500.0, 0.0, 500.0)
|
||||
walker.on_ground = true
|
||||
Movement.step(walker, PlayerCommand.from_input(0, Vector2(0.0, 1.0), yaw, 0.0), SimGround.flat())
|
||||
var ahead: Vector3 = (walker.position - Vector3(500.0, 0.0, 500.0)).normalized()
|
||||
assert_true(ahead.dot(-view.basis.z) > 0.99, "yaw %f: forward went %s" % [yaw, ahead])
|
||||
|
||||
|
||||
func test_aiming_pulls_in_and_narrows() -> void:
|
||||
var rig: CameraRig = CameraRig.new()
|
||||
var feet: Vector3 = Vector3(100.0, 0.0, 100.0)
|
||||
var hip: Transform3D = rig.solve(feet, 0.0, 0.0, false, SimGround.flat())
|
||||
assert_eq(rig.fov(), CameraRig.FOV)
|
||||
for _index: int in 60:
|
||||
rig.update_aim(true, 1.0 / 60.0)
|
||||
assert_eq(rig.aim_blend, 1.0)
|
||||
var aimed: Transform3D = rig.solve(feet, 0.0, 0.0, false, SimGround.flat())
|
||||
var pivot: Vector3 = CameraRig.pivot(feet, false)
|
||||
assert_true(aimed.origin.distance_to(pivot) < hip.origin.distance_to(pivot))
|
||||
assert_eq(rig.fov(), CameraRig.AIM_FOV)
|
||||
rig.update_aim(false, 0.05)
|
||||
assert_true(rig.aim_blend > 0.0 and rig.aim_blend < 1.0, "aim should ease, not snap")
|
||||
|
||||
|
||||
func test_wall_behind_pulls_the_camera_in() -> void:
|
||||
var ground: SimGround = SimGround.flat()
|
||||
ground.add_solid(AABB(Vector3(90.0, 0.0, 98.0), Vector3(20.0, 4.0, 1.0)))
|
||||
var view: Transform3D = CameraRig.new().solve(Vector3(100.0, 0.0, 100.0), 0.0, 0.0, false, ground)
|
||||
assert_true(view.origin.z > 99.0 + CameraRig.CLEARANCE - 0.001, "camera at z %f is in the wall" % view.origin.z)
|
||||
|
||||
|
||||
func test_scripted_sweep_never_clips() -> void:
|
||||
var checked: int = 0
|
||||
for arena: Array in _arenas():
|
||||
var ground: SimGround = arena[0]
|
||||
var feet: Vector3 = arena[1]
|
||||
for crouching: bool in [false, true]:
|
||||
for blend: float in [0.0, 1.0]:
|
||||
var rig: CameraRig = CameraRig.new()
|
||||
rig.aim_blend = blend
|
||||
for yaw_step: int in 72:
|
||||
for pitch_step: int in 17:
|
||||
var yaw: float = yaw_step * TAU / 72.0
|
||||
var pitch: float = deg_to_rad(-80.0 + pitch_step * 10.0)
|
||||
var view: Transform3D = rig.solve(feet, yaw, pitch, crouching, ground)
|
||||
var problem: String = _clipping(ground, view, rig.fov(), CameraRig.pivot(feet, crouching))
|
||||
checked += 1
|
||||
if not problem.is_empty():
|
||||
fail("%s at yaw %d° pitch %d° crouch %s aim %s: %s" % [
|
||||
arena[2], roundi(rad_to_deg(yaw)), roundi(rad_to_deg(pitch)),
|
||||
crouching, blend, problem,
|
||||
])
|
||||
return
|
||||
assert_eq(checked, 5 * 2 * 2 * 72 * 17)
|
||||
|
||||
|
||||
## Returns why a view clips, or "" if it doesn't: the eye or a near-plane corner is
|
||||
## inside geometry, or the line from the pivot to the eye passes through it.
|
||||
func _clipping(ground: SimGround, view: Transform3D, fov_degrees: float, pivot: Vector3) -> String:
|
||||
if ground.point_inside(view.origin):
|
||||
return "eye inside geometry at %s" % view.origin
|
||||
var half_height: float = CameraRig.NEAR * tan(deg_to_rad(fov_degrees) * 0.5)
|
||||
var half_width: float = half_height * ASPECT
|
||||
var centre: Vector3 = view.origin - view.basis.z * CameraRig.NEAR
|
||||
for sx: float in [-1.0, 1.0]:
|
||||
for sy: float in [-1.0, 1.0]:
|
||||
var corner: Vector3 = centre + view.basis.x * half_width * sx + view.basis.y * half_height * sy
|
||||
if ground.point_inside(corner):
|
||||
return "near-plane corner inside geometry at %s" % corner
|
||||
if ground.raycast(pivot, view.origin) < 1.0:
|
||||
return "line of sight from the pivot is blocked"
|
||||
return ""
|
||||
|
||||
|
||||
## [ground, feet, name] for each place the sweep is run.
|
||||
func _arenas() -> Array[Array]:
|
||||
var arenas: Array[Array] = []
|
||||
|
||||
var wall: SimGround = SimGround.flat()
|
||||
wall.add_solid(AABB(Vector3(90.0, 0.0, 100.0 + Tuning.PLAYER_RADIUS), Vector3(20.0, 4.0, 0.5)))
|
||||
arenas.append([wall, Vector3(100.0, 0.0, 100.0), "against a wall"])
|
||||
|
||||
var corridor: SimGround = SimGround.flat()
|
||||
corridor.add_solid(AABB(Vector3(98.0, 0.0, 80.0), Vector3(1.5, 4.0, 40.0)))
|
||||
corridor.add_solid(AABB(Vector3(100.5, 0.0, 80.0), Vector3(1.5, 4.0, 40.0)))
|
||||
arenas.append([corridor, Vector3(100.0, 0.0, 100.0), "in a narrow corridor"])
|
||||
|
||||
var roof: SimGround = SimGround.flat()
|
||||
roof.add_solid(AABB(Vector3(90.0, 2.2, 90.0), Vector3(20.0, 0.3, 20.0)))
|
||||
arenas.append([roof, Vector3(100.0, 0.0, 100.0), "under a low roof"])
|
||||
|
||||
var corner: SimGround = SimGround.flat()
|
||||
corner.add_solid(AABB(Vector3(100.5, 0.0, 90.0), Vector3(3.0, 4.0, 20.0)))
|
||||
corner.add_solid(AABB(Vector3(90.0, 0.0, 100.5), Vector3(20.0, 4.0, 3.0)))
|
||||
arenas.append([corner, Vector3(100.0, 0.0, 100.0), "in a corner"])
|
||||
|
||||
var side: int = 64
|
||||
var values: PackedFloat32Array = PackedFloat32Array()
|
||||
values.resize(side * side)
|
||||
for z: int in side:
|
||||
for x: int in side:
|
||||
# A steep bank rising to the east, with ripples.
|
||||
values[z * side + x] = maxf(float(x) - 30.0, 0.0) * 1.8 + 0.4 * sin(z * 0.9)
|
||||
var hill: SimGround = SimGround.from_heights(values, side, 1.0)
|
||||
var foot: Vector3 = Vector3(29.6, 0.0, 32.0)
|
||||
foot.y = hill.terrain_height(foot.x, foot.z)
|
||||
arenas.append([hill, foot, "at the foot of a steep bank"])
|
||||
return arenas
|
||||
Reference in New Issue
Block a user