计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
math-foundation【效率与协作】Expert game engine math foundation — SIMD vectors/matrices, quaternions, transforms, interpolation, collision primitives, curves and numerical stability under real-time budgets.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Every frame pushes millions of floats through vectors, matrices, quaternions, and collision tests. Math is the substrate: correct under 16.6 ms, deterministic for netcode, and SIMD-friendly or it dies. This skill covers the core math library every engine needs.
| Type | Represents | Memory |
|---|---|---|
float2/3/4 | position, uv, color, dir | 8/12/16 B |
quat | orientation | 16 B |
mat3/mat4 | transforms, projections | 36/64 B |
aabb, sphere, plane, frustum | volume queries | 24/16/16/64 B |
ray | intersection tests | 32 B |
transform (pos, rot, scale) | rigid + scale | 48 B (hot tier) |
Rules:
struct (not class), no vtables — SIMD can then reinterpret.float4 maps to __m128 (SSE), float4x4 to rows of __m128. Use intrinsic headers you control (or DirectXMath/Eigen/SIMD eigen) rather than accidental scalar loops.
struct alignas(16) float4 { float x, y, z, w; };
// SSE: _mm_add_ps(a, b) etc.
inline float4 FMA(float4 a, float4 b, float4 c) {
return _mm_fmadd_ps(a, b, c); // a*b + c — 1 instr, no rounding split
}
alignas(16) on every SIMD type (avoid memcpy warnings, use aligned_alloc(16, …)).-ffast-math unless you know: it breaks NaN propagation (netcode determinism).Matrix×Vector, quaternion×vector, AABB test packs, and any tight loop with uniform ops. At 100k transforms: SIMD mat×vec = ~4–8x over scalar.
Gimbal lock (order-dependent), non-unique representations, bad interpolation. Quaternions: 4 floats, no gimbal lock, slerp smooth, compose cheaply.
quat mul(q1, q2); // compose: apply q2 first, then q1. ~8 mults via SIMD
vec3 rotate(q, v); // q v q^-1 → t=2 cross(q.xyz,v); v + q.w*t + cross(q.xyz,t)
quat slerp(a, b, t); // shortest arc, accounting for sign (a·b<0 → negate b)
quat nlerp(a, b, t); // cheap approx (normalize lerp); fine for small arcs
quat fromAngleAxis(angle, axis); // normalized
quat fromMat4(m);/mat4 toMat4(q); // round-trip must preserve axis/angle
q and -q represent the same rotation. Correct slerp/nlerp: if dot(a,b) < 0, flip b sign first (else the path follows the long way). Meets every engine bug report: "model spins 360° on t=0..1".
Composing 1k quats drifts norm → normalize periodically (per-frame per-transform), cheap with rsqrt.
World transform = T(parent) * R * S (translation × rotation × scale, parent-first). Store as Transform { pos, rot, scale }; combine for children.
Convention decision (critical, document it):
import-and-cook).T * R^-1 * S^-1 without building a full inverse matrix.a+b+c != (a+b)+c (float non-associativity).0.1f is not 0.1.double for accumulation templates (ray-tracing accumulators, physics) — speed cost acceptable there.t relative to start, not absolute.epsilon comparisons with std::numeric_limits<T>::epsilon() * errorScale, never a == b.-ffast-math, no FMA being used in some builds but not others, same float ops per platform.determinism.md).sqrt/sin implementations cross-compiler (see multiplayer netcode determinism references).| Shape | Store | Tests |
|---|---|---|
| Sphere | center r | sphere-sphere ( |
| AABB | min max | overlap, point-in, ray-slab |
| OBB | center, axes(3 rot), halfsizes | SAT |
| Ray | o, d(t) | sphere, aabb (slab), triangle (Möller–Trumbore) |
| Plane | n·p = d | side, distance |
| Frustum | 6 planes | tight test: sphere + box + point |
Collision/streaming queries:
bool RayAABB(Ray r, AABB b, float& tmin, float& tmax) {
float t0 = DBL_MIN, t1 = DBL_MAX;
for (int a = 0; a < 3; ++a) {
float inv = 1.0f / r.d[a];
float tNear = (b.min[a] - r.o[a]) * inv;
float tFar = (b.max[a] - r.o[a]) * inv;
std::swap if inverted...
t0 = max(t0, tNear); t1 = min(t1, tFar);
if (t0 > t1) return false;
}
return true;
}
The 3-iteration slab test beats general AABB intersection on hot paths.
| Curve | Use | Notes |
|---|---|---|
lerp | positions, colors, alpha | scalar/vector |
slerp/nlerp | rotations | sign-correct |
smoothstep | easing | t*t*(3-2t) cheap |
| Catmull-Rom | camera splines, paths | C1 continuous through points |
| Cubic Beziers | UI/easing/curves | de Casteljau |
Linear → use EaseInOut for UI, camera; avoid linear for non-linear feel. Include easeOutBack(s), easeInOutCubic, etc. Table-driven t function + cheap.
==).Ship a math_self_test executable run in CI.
references/simd.md — SIMD intrinsics, alignment, SoA, FMA/fast-math, specialized mat×vecreferences/quaternion.md — algebra, composition, slerp/nlerp, axis-angle, drift fix, matrix round-tripreferences/transforms.md — conventions, handedness, T·R·S, world/local inverse, AABB from transformreferences/numerical-stability.md — FP facts, cancellation, determinism, epsilon disciplinereferences/collision-primitives.md — sphere/aabb/obb/ray/plane/frustum tests, slab ray-AABB, SATreferences/curves-and-interp.md — lerp/slerp, easing family, Catmull-Rom, Beziers, frame-rate independent