Skip to content

FacingMath ​

Inherits: RefCounted

Pure math helpers for entity facing (yaw) and mouse-look turning. No scene or node access, so everything here can be unit-tested headless (see res://tests/run_tests.gd).

Description ​

Yaw convention: radians about +Y, Godot's -Z is "forward". Turning right (mouse moves right, +relative.x) DEcreases yaw.

Methods ​

floatframe_scale( delta: float ) static
floatsmoothing_weight( per_frame_weight: float, delta: float ) static
floatdecay_factor( per_frame_factor: float, delta: float ) static
floatwrap_angle( angle: float ) static
floatangle_diff( from: float, to: float ) static
floatclamped_step( diff: float, max_step: float ) static
floatlook_radians_per_pixel( sensitivity_setting: float ) static
Vector2look_delta_radians( relative: Vector2, sensitivity: float, scale_x: float, scale_y: float, invert_x: bool, invert_y: bool ) static
Vector3forward_from_yaw( yaw: float ) static
floatyaw_from_direction( direction: Vector3 ) static
floatpitch_from_offset( height_difference: float, flat_distance: float ) static
Vector3camera_relative_direction( input: Vector2, view_yaw: float ) static
Vector2local_input_for_facing( world_direction: Vector3, body_yaw: float ) static
floatyaw_from_look( look_delta_x: float ) static
floatturn_animation_from_look( look_delta_x: float ) static
boolis_recent( now_msec: int, last_msec: int, hold_msec: int ) static
boolauto_face_allowed( lease_active: bool, lease_blocks_auto: bool ) static
boolmouse_turn_allowed( lease_active: bool, lease_blocks_auto: bool, auto_turn_in_progress: bool ) static
boolturn_animation_needs_refresh( current: float, new_value: float ) static

Constants ​

  • float TURN_SNAP_EPSILON = 0.01 - Angular distance below which a smooth turn snaps to its target and finishes
  • float TURN_ANIMATION_GAIN = 100.0 - Scale from (mouse pixels * sensitivity) to the -1..1 turn-animation blend position
  • int DIRECT_TURN_HOLD_MSEC = 150 - How long (ms) after the last mouse-look turn Entity.is_turning() stays true. Bridges the gaps between mouse events / physics ticks so the turn-in-place state does not flap.
  • float TURN_ANIMATION_REFRESH_STEP = 0.3 - Minimum change in the turn-animation blend position that is worth re-sending to the animation tree
  • float REFERENCE_FPS = 60.0 - Frame rate the per-frame camera constants (smoothing weights, speeds, damping) were tuned at
  • float MAX_FRAME_SCALE = 4.0 - Cap on frame_scale so a long hitch does not make the camera lurch
  • float LOOK_DEGREES_PER_PIXEL_AT_MAX = 0.9 - Degrees of look rotation per mouse pixel when the sensitivity setting is at its maximum (1.0). The default setting (SettingsConfig.mouse_*_axis_sensitivity = 0.22) therefore gives ~0.2 deg/px.

Method descriptions ​

float frame_scale( delta: float ) ​

How many reference frames (1/60 s) this frame is worth. Multiply a per-frame step by it to make the step frame-rate independent: identical to the old behaviour at 60 FPS.

float smoothing_weight( per_frame_weight: float, delta: float ) ​

A per-frame lerp weight (tuned at 60 FPS) converted for this frame's delta, so smoothing takes the same real time at any frame rate: applying it N times at 60 FPS equals applying the converted weight M times at any other rate over the same elapsed time.

float decay_factor( per_frame_factor: float, delta: float ) ​

A per-frame multiplicative decay factor (tuned at 60 FPS) converted for this frame's delta

float wrap_angle( angle: float ) ​

Wraps an angle into [-PI, PI)

float angle_diff( from: float, to: float ) ​

Shortest signed angle from from to to, in [-PI, PI]

float clamped_step( diff: float, max_step: float ) ​

Signed step toward a target: moves along diff by at most max_step, never overshooting

float look_radians_per_pixel( sensitivity_setting: float ) ​

Radians of look rotation per mouse pixel for a sensitivity setting (0..1)

Vector2 look_delta_radians( relative: Vector2, sensitivity: float, scale_x: float, scale_y: float, invert_x: bool, invert_y: bool ) ​

Converts raw mouse motion (pixels) to look radians using the player's sensitivity and invert settings. Screen space: +x = mouse right, +y = mouse down. Inversion flips the sign, so with no inversion consumers apply it as: yaw = yaw_from_look(delta.x); pitch down = +delta.y. sensitivity is the master setting (0..1, SettingsConfig.mouse_sensitivity); the per-axis scales are relative to it (1.0 = same as the master), so an axis runs at master x scale.

Vector3 forward_from_yaw( yaw: float ) ​

Flat forward direction (-Z rotated by yaw): yaw 0 faces -Z, +90 deg faces -X (yaw + = turning left)

float yaw_from_direction( direction: Vector3 ) ​

Yaw whose forward direction points along direction (the inverse of forward_from_yaw; Y is ignored)

float pitch_from_offset( height_difference: float, flat_distance: float ) ​

Pitch (radians, + = looking up) from a height difference and the flat distance to a point

Vector3 camera_relative_direction( input: Vector2, view_yaw: float ) ​

World direction for a movement input (x = right, y = forward) relative to a view yaw. The right vector uses the same rule as NavigationController.set_direct_movement_input.

Vector2 local_input_for_facing( world_direction: Vector3, body_yaw: float ) ​

A world movement direction expressed as (right, forward) relative to a body that faces body_yaw. Used to drive the locomotion animation blend when the body does not face the way it moves.

float yaw_from_look( look_delta_x: float ) ​

Yaw change for a horizontal look delta (screen right => negative yaw, since yaw + = turning left)

float turn_animation_from_look( look_delta_x: float ) ​

Turn-animation blend position (-1..1) for a horizontal look delta. Matches the TurnBlendSpace layout: negative = turn-left clips, positive = turn-right clips, so mouse right (a right turn) is positive - the opposite sign of yaw_from_look.

bool is_recent( now_msec: int, last_msec: int, hold_msec: int ) ​

True if last_msec happened within hold_msec before now_msec. A negative last_msec means "never".

bool auto_face_allowed( lease_active: bool, lease_blocks_auto: bool ) ​

Whether an automatic facing request (navigation, ability, script) may change the body's yaw. lease_blocks_auto is only meaningful while a mouse-look lease is active.

bool mouse_turn_allowed( lease_active: bool, lease_blocks_auto: bool, auto_turn_in_progress: bool ) ​

Whether mouse-look may turn the body right now. When the lease lets auto-face win (lease_blocks_auto == false), mouse yaw yields until the in-progress auto-face turn completes.

bool turn_animation_needs_refresh( current: float, new_value: float ) ​

Whether the turn animation's blend position changed enough (or flipped side) to be re-sent