## 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