MassBattleFixedPoint.h
Source/MassBattle/Public/Math/MassBattleFixedPoint.h
概述 Overview
Q31.32 定点数学库(改编自 BEPUphysics 的定点设计,Apache 2.0)。为锁步确定性而生:浮点运算在异构硬件/编译器上可能产生跨平台发散,定点整数运算则完全确定。核心类型 FFix64(Q31.32 定点标量,1 raw unit ≈ 2.33e-10)之上有 FFixVector3 / FFixVector2(RVO2/ORCA 平面)/ FFixQuat / FFixPlane 与独立几何辅助、计时器辅助。关键设计:fragment 数据类型保持 UE 原生类型(FVector/FQuat/float)不变,定点转换在 Processor 内部透明完成(经 MassBattleMath.h 的 TMassMath 双路径抽象);溢出安全——点积/长度运算带阈值门控,大值自动走预缩放慢路径;三角函数用 2048 条目 LUT 线性插值(确定性);Mul64 用 128 位中间、Div64 对除零与商溢出饱和而非崩溃。
Q31.32 fixed-point math library (adapted from BEPUphysics' fixed-point design, Apache 2.0). Built for lockstep determinism: floating-point math can diverge across platforms/compilers, while fixed-point integer math is fully deterministic. The core type FFix64 (Q31.32 scalar, 1 raw unit ≈ 2.33e-10) supports FFixVector3 / FFixVector2 (RVO2/ORCA plane) / FFixQuat / FFixPlane, plus standalone geometry helpers and timer helpers. Key designs: fragment data types stay native UE types (FVector/FQuat/float) — fixed-point conversion is transparent inside Processors (via the TMassMath dual-path abstraction in MassBattleMath.h); overflow safety — dot/length ops are threshold-gated with automatic pre-scaled slow paths for large values; trig uses a 2048-entry linear-interpolated LUT (deterministic); Mul64 uses a 128-bit intermediate and Div64 saturates on divide-by-zero and quotient overflow instead of crashing.
FFix64 Q31.32 定点标量
Q31.32 定点标量:值以 int64 RawValue 存储(默认成员初始化为 0——默认构造的 FFix64/FFixVector 保持清零,草稿结构可能读前未写;聚合身份保留,{raw} 花括号初始化仍可用),1 表示 ONE_RAW = 1 << 32,范围 ±2.147e9,精度 ~2.33e-10。比较直接比较 RawValue(等价于数值比较);乘法经 Mul64(128 位中间),除法经 Div64(溢出饱和)。
Q31.32 fixed-point scalar: stored as an int64 RawValue (default-initialized to 0 — default-constructed FFix64/FFixVector stay zeroed since scratch structs may be read before written; aggregate status preserved so {raw} brace-init still works), with 1 = ONE_RAW = 1 << 32, range ±2.147e9, precision ~2.33e-10. Comparisons compare RawValue directly (equivalent to numeric comparison); multiplication goes through Mul64 (128-bit intermediate), division through Div64 (overflow-saturating).
常量 Constants
| 名称 Name | 值 Value | 说明 Description |
|---|---|---|
ONE_RAW | 1 << 32 | 数值 1 的原始表示。Raw representation of 1. |
SAFE_DOT3D_COMPONENT | 26754 | 3D 点积求和(3×V²)的 int64 溢出安全界:3×V²×2³² ≤ INT64_MAX → V ≤ 26754。大于此值的向量必须走慢路径(预缩放)。3-component dot-sum int64 safety bound: 3×V²×2³² ≤ INT64_MAX → V ≤ 26754. Vectors above this must take the pre-scaled slow path. |
SAFE_DOT2D_COMPONENT | 32000 | 2D 点积求和界:2×V²×2³² ≤ INT64_MAX → V ≤ 32768;阈值取 32000 留余量。2D 长度/平方长度快路径的门控值。2-component dot-sum bound: 2×V²×2³² ≤ INT64_MAX → V ≤ 32768; the threshold is kept at 32000 for margin. The gate value of the 2D length/length-squared fast paths. |
Zero() / One() / MinusOne() | 0 / 2³² / -2³² | 常量构造。Constant constructors. |
MaxValue() | INT64_MAX | 安全上界 ~2.147e9——替代会溢出的 FromFloat(FLT_MAX)。Safe upper bound ~2.147e9 — use instead of FromFloat(FLT_MAX) which overflows. |
Pi() / HalfPi() / TwoPi() | Q31.32 编码 | π 常量(原始值 0x3243F6A88 等)。π constants (raw 0x3243F6A88 etc.). |
Epsilon() | 1 | 最小可表示增量(1 raw unit ≈ 2.33e-10)。Smallest representable increment (1 raw unit ≈ 2.33e-10). |
方法 Methods
转换族。FromFloat/FromDouble(非内联,ensureMsgf 只编译一次):数值 × 2³² 后检查 ±2³²·2³¹(=2^63)边界——超出即 ensureMsgf 警告并饱和到 MaxValue/MinValue((double)INT64_MAX 会舍入到 2^63,直接比较会把 Scaled==2^63 误判为合法,故用精确的 2^63 常量比较);FromInt 内联直乘;ToInt 向零截断。
Conversion family. FromFloat/FromDouble (non-inline so ensureMsgf compiles once): scales by 2³² and checks the ±2^63 boundary — out of range triggers an ensureMsgf warning and saturates to MaxValue/MinValue (since (double)INT64_MAX rounds UP to 2^63, the comparison uses the exact 2^63 constant to avoid accepting the UB cast); FromInt is an inline multiply; ToInt truncates toward zero.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
V | float / double / int32 | 源数值。Source value. |
Output
返回转换结果;溢出时饱和并触发 ensureMsgf 警告。
Returns the conversion; saturates with an ensureMsgf warning on overflow.
算术族:加减直接操作 RawValue;乘除经 Mul64 / Div64;% 复用 Fmod 的除零保护。
Arithmetic family: add/sub operate on RawValue directly; mul/div go through Mul64 / Div64; % reuses Fmod's divide-by-zero guard.
Input
操作数 O(FFix64)。
Operand O (FFix64).
Output
返回运算结果;除零饱和到 MaxValue/MinValue。
Returns the result; divide-by-zero saturates to MaxValue/MinValue.
跨平台 64×64→128→64 乘法:MSVC 用 _mul128 取高 64 位并移位(先转 uint64 再移位,避免有符号右移符号扩展);其它平台用 __int128 中间值右移 32。
Cross-platform 64×64→128→64 multiply: MSVC uses _mul128 with the high 64 bits shifted (cast to uint64 before shifting to avoid sign-extension); other platforms use a __int128 intermediate shifted right by 32.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
A / B | int64 | 原始 Q31.32 值。Raw Q31.32 values. |
Output
返回 Q31.32 乘积(截断)。
Returns the Q31.32 product (truncated).
跨平台 64/64→128/64→64 除法,带商溢出保护:MSVC 的 _div128 在商超出 int64 时触发 #DE 硬件异常(进程崩溃)——溢出 ⇔ trunc(|A|/|B|) ≥ 2³¹,用纯 64 位比较判定(|B|×2³¹ 自身会溢出);溢出与除零一律饱和到 MaxValue/MinValue,替代崩溃/UB。128 位中间保持原始位模式(含符号),商与旧实现完全一致。
Cross-platform 64/64→128/64→64 divide with quotient-overflow guard: MSVC's _div128 raises a #DE hardware exception (process crash) when the quotient exceeds int64 — overflow ⇔ trunc(|A|/|B|) ≥ 2³¹, checked with pure 64-bit compares (|B|×2³¹ itself would overflow); overflow and divide-by-zero both saturate to MaxValue/MinValue instead of crashing/UB. The 128-bit intermediate keeps the original bit pattern (sign included), so the quotient matches the old implementation exactly.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
A / B | int64 | 被除数/除数(原始值)。Dividend/divisor (raw). |
Output
返回 Q31.32 商;除零或商溢出饱和到 ±MaxValue。
Returns the Q31.32 quotient; divide-by-zero or quotient overflow saturates to ±MaxValue.
Sqrt:牛顿-拉弗森法开方——double sqrt 做初始猜测,8 次迭代,收敛提前退出;≤0 返回 0。InvSqrt:牛顿迭代(仅 Mul64,无除法);种子守卫——y > 46340 时 y² 溢出 Q31.32(V < ~4.66e-10),直接回退 1/sqrt(V)(回退除数 ≥ 65536 raw,Div64 恒安全)。
Sqrt: Newton-Raphson square root — double sqrt seeds the guess, up to 8 iterations with early exit on convergence; ≤0 returns 0. InvSqrt: Newton iteration (Mul64 only, no division); seed guard — when y > 46340, y² overflows Q31.32 (V < ~4.66e-10), falling straight back to 1/sqrt(V) (the fallback divisor ≥ 65536 raw keeps Div64 safe).
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
V | FFix64 | 输入值(InvSqrt 用)。Input value (InvSqrt). |
Output
返回平方根 / 倒数平方根;≤0 返回 0。
Returns the square root / inverse square root; 0 for ≤0 input.
Floor:清低 32 位(向 -∞ 取整);Fraction:取低 32 位(小数部分);Abs:绝对值——注意 Abs(INT64_MIN) 回绕为自身(负值,与普通 int64 一致),能见到 INT64_MIN 的热点守卫(Atan/Atan2/Asin)已显式特判,此处加饱和会让每次 Length() 守卫多 3 个分支(实测向量长度吞吐 -34%);Sign:符号(-1/0/1)。
Floor: clears the low 32 bits (floor toward -inf); Fraction: keeps the low 32 bits (fractional part); Abs: absolute value — note Abs(INT64_MIN) wraps to itself (negative, same as plain int64); hot guards that can see INT64_MIN (Atan/Atan2/Asin) special-case it explicitly, and adding saturation here would cost 3 extra branches in every Length() guard (measured -34% vector-length throughput); Sign: sign (-1/0/1).
Input
无参数(成员)。
No parameters (member).
Output
返回对应结果。
Returns the result.
三角函数(查表,确定性):2048 条目正弦表 + 正切表(引擎启动时 InitializeLUT 生成一次;±π/2 处 tan 钳制到最大可表示值)。角度先归一化到 [0, 2π),索引线性插值(128 位中间避免余数×ONE_RAW 溢出)。Cos = Sin(x + π/2) 移相;SinCos 同时输出两者。
Trig functions (LUT-based, deterministic): 2048-entry sine and tangent tables (generated once by InitializeLUT at engine startup; tan clamps to the max representable value near ±π/2). Angles normalize to [0, 2π) first, then index with linear interpolation (a 128-bit intermediate avoids remainder×ONE_RAW overflow). Cos = Sin(x + π/2) phase shift; SinCos outputs both.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
Rad | FFix64 | 弧度角。Angle in radians. |
Output
返回三角函数值;SinCos 经输出引用返回。
Returns the trig value; SinCos returns via out-references.
反三角函数(确定性级数):Atan 用欧拉级数(|X|>1 时 π/2 - atan(1/x) 变换,12 项收敛);Atan2 按象限组合;X=0 时 ±π/2,atan2(0,0)=0;Asin = atan(x/√(1-x²)),|x|>1 返回 0,分母为零时 ±π/2;Acos = π/2 - asin。
Inverse trig (deterministic series): Atan uses the Euler series (π/2 - atan(1/x) transform for |X|>1, up to 12 terms); Atan2 composes quadrants; X=0 gives ±π/2, atan2(0,0)=0; Asin = atan(x/√(1-x²)), returns 0 for |x|>1, ±π/2 when the denominator is zero; Acos = π/2 - asin.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
X(及 Y) | FFix64 | 输入值。Input value(s). |
Output
返回弧度角。
Returns the angle in radians.
Fmod:C 风格取模——结果符号与 A 相同、向零截断;除零返回 0。Exp:e^x 泰勒级数——|x|>2 时先折半(e^x = e^(x/2)²)再平方还原,16 项收敛,项 < Epsilon 提前退出。
Fmod: C-style modulo — same sign as A, truncated toward zero; divide-by-zero returns 0. Exp: e^x Taylor series — halves the argument for |x|>2 (e^x = e^(x/2)²) and squares back, up to 16 terms with early exit when a term < Epsilon.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
A / B(Fmod) | FFix64 | 被模数/模数。Dividend/divisor. |
Output
返回取模结果 / e^x。
Returns the modulo / e^x.
LWC 瓦片原点对齐:返回不大于 WorldCoord 的最大瓦片对齐值(向 -∞ 取整)。C++ 整数除法向 0 截断,负值修正确保始终对齐到下方瓦片边界。瓦片大小应为 Q31.32 可表示的正倍数(如 1000.0)。
LWC tile-origin snapping: returns the greatest tile-aligned value ≤ WorldCoord (floor toward -inf). C++ integer division truncates toward 0, so negative values are adjusted to always snap to the tile boundary below. Tile size should be a positive Q31.32-representable multiple (e.g. 1000.0).
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
WorldCoord | FFix64 | 世界坐标。World coordinate. |
TileSize | FFix64 | 瓦片大小。Tile size. |
Output
返回瓦片对齐原点。
Returns the tile-aligned origin.
引擎启动时调用一次的查表初始化(幂等):按 2π/2048 步进预计算正弦表与正切表(±π/2 处 tan 用最大可表示值)。三角函数的调用会惰性触发它。
One-time idempotent LUT initialization at engine startup: precomputes the sine and tangent tables at 2π/2048 steps (tan clamps to the max representable value at ±π/2). Trig calls trigger it lazily.
Input
无参数。
No parameters.
Output
无返回值。副作用:填充两个静态 LUT。
No return value. Side effect: fills the two static LUTs.
FFixVector3 3D 定点向量
三分量 FFix64 向量。与 UE 类型互转(FromFVector 用 double 精度、FromFVector3f 用 float 精度、ToFVector2f 丢弃 Z)。溢出安全:LengthSquared / Length 带阈值门控——最大分量 ≤ SAFE_DOT3D_COMPONENT(26754)时走快路径,否则走 *Slow 预缩放慢路径(逐次右移 1 位缩放,结果左移 2×Shifts 还原)。ProjectOnToNormal 语义与 UE 一致:返回 V 沿法线的分量 = (V·N)·N(假定 N 已归一化)。
Three-FFix64-component vector. Interops with UE types (FromFVector at double precision, FromFVector3f at float precision, ToFVector2f drops Z). Overflow safety: LengthSquared / Length are threshold-gated — the fast path when the max component ≤ SAFE_DOT3D_COMPONENT (26754), otherwise the *Slow pre-scaled slow path (successive 1-bit right shifts, restored by shifting left 2×Shifts). ProjectOnToNormal matches UE semantics: returns the component of V along Normal = (V·N)·N (assuming N is unit).
方法 Methods
| 方法 Method | 返回 Return | 说明 Description |
|---|---|---|
Zero / One / Up / Forward / Right | static FFixVector3 | 常量向量。Constant vectors. |
FromFVector / FromFVector3f / FromFVector2fZ0 / ToFVector / ToFVector3f / ToFVector2f | 互转 | UE 类型互转(2D 输入填充 Z=0;输出丢弃 Z)。UE type interop (2D input fills Z=0; output drops Z). |
operator+ - * / (标量与分量)operator+ - * / (scalar and component-wise) | FFixVector3 | 算术:标量乘除与分量乘除。Arithmetic: scalar and componentwise mul/div. |
operator[] | FFix64& | 索引访问(0=X, 1=Y, 2=Z;越界回落到 Z)。Index access (0=X, 1=Y, 2=Z; out of range falls back to Z). |
Dot / Cross | FFix64 / FFixVector3 | 点积与叉积。Dot and cross products. |
LengthSquared / Length / LengthSquared2D / Length2D | FFix64 | 长度(3D/2D)——阈值门控 + 预缩放慢路径防溢出。Lengths (3D/2D) — threshold-gated with pre-scaled slow paths. |
Dist / DistSquared / Dist2D / DistSquared2D | FFix64 | 与另一向量的距离。Distance to another vector. |
Normalized / Normalized2D | FFixVector3 | 归一化;零长度返回 Zero。Normalization; zero length returns Zero. |
GetSafeNormal2D(Tolerance) / (Tolerance, Fallback) | FFixVector3 | 带容差的 2D 安全归一化;长度 ≤ 容差返回 Zero 或 Fallback。Toleranced 2D safe normalize; length ≤ tolerance returns Zero or Fallback. |
IsNearlyZero / AllComponentsEqual / GetAbs / GetClampedToMaxSize | bool / FFixVector3 | 近零判定 / 分量容差相等 / 绝对值 / 长度钳制。Near-zero check / component tolerance equality / abs / length clamp. |
ProjectOnToNormal / PerpendicularVector | FFixVector3 | 投影到法线 / 最优轴垂直向量(选最小模长轴叉乘,与 float 路径算法一致)。Project onto normal / best-axis perpendicular (smallest-magnitude axis cross, matching the float path). |
Lerp / Max / Min / Abs | static | 静态辅助。Static helpers. |
TileOrigin | static FFixVector3 | 每轴独立瓦片对齐(向 -∞ 取整)。Per-axis tile snapping (floor toward -inf). |
FFixVector2 2D 定点向量(RVO2/ORCA 平面)
RVO2/ORCA 避障平面的 2D 定点向量。提供 operator|(点积)与 operator^(2D 标量叉积)的 FVector2f 兼容运算符,让 RVO2/ORCA 代码原样迁移;Perpendicular() 返回 (-Y, X)(逆时针 90°)。长度运算同样阈值门控(阈值 32000,对应 2D 安全界 32768)。
2D fixed-point vector for the RVO2/ORCA avoidance plane. Provides operator| (dot) and operator^ (2D scalar cross) FVector2f-compatible operators so RVO2/ORCA code transfers verbatim; Perpendicular() returns (-Y, X) (90° CCW). Length ops are threshold-gated too (threshold 32000, matching the 2D safety bound of 32768).
方法 Methods
| 方法 Method | 返回 Return | 说明 Description |
|---|---|---|
Zero / One / Right / Up | static FFixVector2 | 常量向量。Constant vectors. |
FromFVector2f / FromFVector2D / ToFVector2f / ToFVector2D | 互转 | UE 2D 类型互转。UE 2D type interop. |
operator+ - * / (标量与分量)operator+ - * / (scalar and component-wise) | FFixVector2 | 算术。Arithmetic. |
operator| / operator^ | FFix64 | 点积 / 2D 标量叉积(FVector2f 兼容)。Dot / 2D scalar cross (FVector2f compatible). |
Dot / Cross | FFix64 | 点积与 2D 叉积。Dot and 2D cross. |
LengthSquared / Length | FFix64 | 长度(阈值门控 + 慢路径)。Length (threshold-gated with slow path). |
Normalized / Perpendicular | FFixVector2 | 归一化 / 逆时针 90° 垂直向量。Normalize / 90° CCW perpendicular. |
GetSafeNormal(Tolerance) / (Tolerance, Fallback) | FFixVector2 | 带容差安全归一化。Toleranced safe normalize. |
GetClampedToMaxSize / IsNearlyZero / GetAbs | FFixVector2 / bool | 长度钳制 / 近零 / 绝对值。Length clamp / near-zero / abs. |
Max / Min / Lerp 及 operator*(S, V) | static / 自由函数static / free functions | 静态辅助与标量×向量对称。Static helpers and scalar×vector symmetry. |
FFixQuat 定点四元数
四分量 FFix64 四元数。与 FQuat / FQuat4f 互转;operator*(Vec3) 为 Q·V 旋转语法糖(优化版 q·v·q⁻¹);EnforceShortestArcWith 保证最短弧(点积 < 0 取反);欧拉角提取经 Asin/Atan2(GetPitch 绕 Y / GetRoll 绕 X / GetYaw 绕 Z,弧度),Rotator() 转 FRotator(度,边界类型)。
Four-FFix64-component quaternion. Interops with FQuat / FQuat4f; operator*(Vec3) is the Q·V rotation syntax sugar (optimized q·v·q⁻¹); EnforceShortestArcWith enforces the shortest arc (negates when the dot < 0); euler extraction uses Asin/Atan2 (GetPitch around Y / GetRoll around X / GetYaw around Z, radians), Rotator() converts to FRotator (degrees, boundary type).
方法 Methods
| 方法 Method | 返回 Return | 说明 Description |
|---|---|---|
Identity / FromFQuat / FromFQuat4f / ToFQuat | static FFixQuat / FQuat | 常量与 UE 互转。Constant and UE interop. |
operator+ - * (四元数/标量) / operator*(Vec3)operator+ - * (quaternion/scalar) / operator*(Vec3) | FFixQuat / FFixVector3 | 四元数算术与旋转语法糖。Quaternion arithmetic and rotation sugar. |
Rotate / Inverse / Dot / LengthSquared / Length / Normalized | 标准 | 旋转 / 求逆 / 点积 / 长度 / 归一化(零长度返回 Identity)。Rotate / inverse / dot / length / normalize (Identity on zero length). |
IsNormalized / EnforceShortestArcWith | bool / void | 归一化判定 / 最短弧强制。Normalization check / shortest-arc enforcement. |
GetForwardVector / GetRightVector / GetUpVector / GetAxisX/Y/Z | FFixVector3 | 基向量访问(经 Rotate)。Basis accessors (via Rotate). |
FromAxisAngle / FromPitchRoll | static FFixQuat | 轴角构造;FromPitchRoll 直接实现 UE 的 FRotator→四元数公式(避免 FromAxisAngle 组合的符号不匹配),匹配 FQuat(FRotator(Pitch, 0, Roll))。Axis-angle construction; FromPitchRoll directly implements UE's FRotator→quaternion formula (avoiding the sign mismatch of FromAxisAngle composition), matching FQuat(FRotator(Pitch, 0, Roll)). |
Slerp | static FFixQuat | 球面线性插值:先强制最短弧(点积 < 0 取反 B);夹角极小(cos > 1-ε)或 SinOmega 为零(定点截断/LUT 精度导致)时回退 lerp;结果归一化防定点精度漂移。Spherical linear interpolation: enforces the shortest arc first (negates B when the dot < 0); falls back to lerp for near-zero angles (cos > 1-ε) or zero SinOmega (fixed-point truncation / LUT precision); normalizes the result against precision drift. |
MakeFromXZ | static FFixQuat | 从基向量构造:Z 主轴正交归一化(先归一化 Z 再正交化 X)——与 X 主轴的 FRotationMatrix::MakeFromXZ 优先级不同,然后矩阵转四元数。Constructs from basis vectors: Z-primary orthonormalization (normalize Z first, then orthogonalize X) — differing in axis priority from the X-primary FRotationMatrix::MakeFromXZ; then matrix-to-quaternion. |
FindBetween | static FFixQuat | 从两向量构造(半角公式 q = (axis·sin(θ/2), cos(θ/2)));接近 180°(点积 < -1+1e-4)时轴取 PerpendicularVector 并绕轴转 π。Constructs from two vectors (half-angle formula q = (axis·sin(θ/2), cos(θ/2))); near 180° (dot < -1+1e-4) uses PerpendicularVector as the axis with a π rotation. |
GetPitch / GetRoll / GetYaw / Rotator | FFix64 / FRotator | 欧拉角提取(弧度)与 FRotator 转换(度)。Euler extraction (radians) and FRotator conversion (degrees). |
平面与独立几何辅助 Plane & Standalone Geometry Helpers
定点平面(方程 A·x + B·y + C·z + D = 0)。配套 MakePlaneFromPointAndNormal_Fix(点法式构造,D = -N·P)与 PlaneDot_Fix(平面方程求值)。
Fixed-point plane (A·x + B·y + C·z + D = 0). Paired with MakePlaneFromPointAndNormal_Fix (point-normal construction, D = -N·P) and PlaneDot_Fix (plane evaluation).
FMath::GetMappedRangeValueClamped 的 FFix64 替代:输入区间 [InA, InB] 映射到输出区间 [OutA, OutB],Alpha 钳制到 [0,1];InB == InA 时返回 OutA。
FFix64 replacement for FMath::GetMappedRangeValueClamped: maps [InA, InB] to [OutA, OutB] with Alpha clamped to [0,1]; returns OutA when InB == InA.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
V / InA / InB / OutA / OutB | FFix64 | 输入值与区间端点。Input value and range endpoints. |
Output
返回映射并钳制的值。
Returns the mapped, clamped value.
FMath::ClosestPointOnSegment 的 FFixVector3 替代:P 在线段 A→B 上的投影,t 钳制到 [0,1];零长线段返回 A。
FFixVector3 replacement for FMath::ClosestPointOnSegment: projection of P onto segment A→B with t clamped to [0,1]; returns A for a zero-length segment.
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
P / A / B | FFixVector3 | 点与线段端点。Point and segment endpoints. |
Output
返回线段上最近点。
Returns the closest point on the segment.
FMath::SegmentDistToSegmentSafe 的 FFixVector3 替代:标准线段-线段最近点算法(Real-Time Collision Detection §5.1.9),退化情况(点线段 / 平行)逐分支处理。
FFixVector3 replacement for FMath::SegmentDistToSegmentSafe: the standard segment-segment closest-point algorithm (Real-Time Collision Detection §5.1.9) with per-branch degenerate cases (point segments / parallel).
Input
| 参数 Param | 类型 Type | 说明 Description |
|---|---|---|
A1 / A2 / B1 / B2 | FFixVector3 | 两条线段端点。Two segment endpoints. |
OutA / OutB | FFixVector3& | 输出:两线段上的最近点。Outputs: closest points on the segments. |
Output
无返回值;填充两个输出点。
No return value; fills the two output points.
计时器辅助 Timer Helpers
对 float 存储的计时器做定点运算的工具函数族。fragment 中计时器值以 float 存储(兼容 UPROPERTY,快照安全);所有 tick/compare/reset 操作经 FFix64 算术,消除锁步确定性中的跨平台浮点发散。
Utility family running fixed-point arithmetic on float-stored timers. Fragments store timer values as float (UPROPERTY-compatible, snapshot-safe); all tick/compare/reset operations route through FFix64 arithmetic to eliminate cross-platform float divergence in lockstep determinism.
| 函数 Function | 返回 Return | 说明 Description |
|---|---|---|
TickTimerCountdown(Timer, DT) | float | 倒计时(允许负值)。Countdown tick (may go negative). |
TickTimerCountdownClamped(Timer, DT) | float | 倒计时,钳制到 [0, +∞)。Countdown tick clamped to [0, +inf). |
TickTimerElapsed(Timer, DT) | float | 累计计时器。Elapsed timer tick. |
TickTimerElapsedClamped(Timer, DT, Max) | float | 累计计时器,钳制到 [0, Max]。Elapsed timer clamped to [0, Max]. |
IsCountdownExpired(Timer) | bool | 倒计时是否到期(≤ 0)。Whether the countdown expired (≤ 0). |
IsElapsedFinished(Elapsed, Duration) | bool | 累计是否达到阈值。Whether the elapsed reached the threshold. |
IsTimeGE / GT / LE / LT(A, B) | bool | 时间值比较(经定点转换)。Time comparisons (via fixed-point conversion). |
InitCountdown / InitElapsed / ResetTimerTo | float | 计时器初始化/重置。Timer init / reset. |
管线角色 Pipeline Role
本文件是纯数学库,不直接出现在模拟管线中——经 MassBattleMath.h 的 TMassMath<true> 路径被 MoveProcessorFP 等定点处理器使用(定点数学由 SetSimConfig 的 bInFixPointMath 开启)。LUT 由 InitializeLUT 幂等初始化(三角函数惰性触发)。测试库 MassBattleFixMathTestLib / MassBattleFixOverflowTestLib 提供 BP 可调用的验证入口。
This file is a pure math library, not directly present in the simulation pipeline — it is consumed via the TMassMath<true> path of MassBattleMath.h by fixed-point processors like MoveProcessorFP (fixed-point math is enabled by SetSimConfig's bInFixPointMath). The LUT is initialized idempotently by InitializeLUT (triggered lazily by trig calls). The test libraries MassBattleFixMathTestLib / MassBattleFixOverflowTestLib provide BP-callable validation entries.