math-foundation
Use when the user asks about game math, quaternions, SIMD vector math, transform hierarchies, frustum culling, collision primitives, interpolation or floating…
by j4flmao★ 47 GitHub stars
Install math-foundation in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
Math Foundation — Deep Engineering Guide
Purpose
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.
Agent Protocol
Trigger
"game math", "quaternion", "SIMD math", "transform hierarchy", "frustum culling", "collision primitives", "interpolation", "floating point precision".
Input Context
- Language and SIMD targets (SSE/AVX, NEON)
- Coordinate conventions (handedness, row/column-major)
- Precision issue or feature needed
Output Artifact
Math code with conventions stated explicitly. No file unless requested.
Response Format
Conventions: {handedness, matrix layout, angle units}
Code: {snippet}
Edge cases: {normalisation, epsilon, degeneracy}
1. The Canonical Type Set
| 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/96 B (6 planes) |
ray |
intersection tests | 32 B |
transform (pos, rot, scale) |
rigid + scale | 48 B (hot tier) |
Rules:
- Value types,
struct(not class), no vtables — SIMD can then reinterpret. - NaN/Inf guards: default init to 0; assert on non-finite inputs in hot paths.
- Deterministic math when needed (see §6).
2. SIMD: The Practical Diet
2.1 The SIMD Types
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.
#include <immintrin.h>
struct alignas(16) float4 { __m128 v; }; // wrap the register type
inline float4 operator+(float4 a, float4 b) { return { _mm_add_ps(a.v, b.v) }; }
// FMA3 (Haswell+/Zen+, compile with -mfma or /arch:AVX2): one rounding instead of two.
inline float4 MulAdd(float4 a, float4 b, float4 c) { return { _mm_fmadd_ps(a.v, b.v, c.v) }; }
// NEON equivalent: vfmaq_f32(c, a, b). Note FMA changes results vs mul+add -> pick one per build for determinism.
2.2 Alignment & Layout
alignas(16)on every SIMD type (avoidmemcpywarnings, usealigned_alloc(16, …)).- Hot arrays (vertex pos, transforms) live in SoA SIMD lanes (§ in memory skill).
- Avoid
-ffast-mathunless you know: it breaks NaN propagation (netcode determinism).
2.3 When SIMD Matters Most
Matrix×Vector, quaternion×vector, AABB test packs, and any tight loop with uniform ops. Expect gains bounded by lane width (4 for SSE/NEON, 8 for AVX) and usually limited by memory bandwidth — benchmark against the scalar reference.
3. Quaternions: The Correct Rotation
3.1 Why Not Euler
Gimbal lock (order-dependent), non-unique representations, bad interpolation. Quaternions: 4 floats, no gimbal lock, slerp smooth, compose cheaply.
3.2 The Core Ops
quat mul(q1, q2); // compose: apply q2 first, then q1 (16 mul + 12 add scalar)
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
3.3 The Sign Problem
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".
3.4 Normalization Drift
Composing 1k quats drifts norm → normalize periodically (per-frame per-transform), cheap with rsqrt.
4. Transforms & Coordinate Conventions
4.1 Transform Composition
Local matrix = T * R * S (column-vector convention: scale, then rotate, then translate). World = ParentWorld * Local. Store as Transform { pos, rot, scale }; non-uniform scale under rotated parents introduces shear, which a TRS struct cannot represent — avoid it or fall back to matrices.
Convention decision (critical, document it):
- Row vectors × matrices (D3D-style) or column vectors (GL style)? Pick ONE for the whole engine + renderer; document in the codebase header; mismatch = mirrored sprites.
4.2 Handedness
- Known conventions: Unreal = left-handed, Z-up; Unity = left-handed, Y-up; Godot / glTF / OpenGL = right-handed, Y-up; Blender = right-handed, Z-up. Pick one for the engine and convert at import (see
import-and-cook). - All camera/projection/shadow conversions flow through one place to avoid 50 copies of the flip.
4.3 Fast Paths
- World→local: combine inverse
T * R^-1 * S^-1without building a full inverse matrix. - AABBs re-computed from transform (or a 3-vector math) — cheaper than
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
- references/collision-primitives.md
- references/curves-and-interp.md
- references/numerical-stability.md
- references/quaternion.md
- references/simd.md
- references/transforms.md
FAQ
What does the math-foundation skill do?
Use when the user asks about game math, quaternions, SIMD vector math, transform hierarchies, frustum culling, collision primitives, interpolation or floating point precision in large worlds. Produces the engine math layer with numerically stable code. Not for physics solver design (use physics-engine).
How do I install the math-foundation skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the math-foundation skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
mcp-builder
AnthropicGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
claude-api
AnthropicReference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
algorithmic-art
AnthropicCreating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
mcp-integration
AnthropicThis skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…