Game Vector Math Primitives
Use this skill when game code depends on vector operations whose geometric
meaning matters. Prefer clear primitives and named intent over opaque formula
paste.
Primary source: Geometry for Programmers by Oleksandr Kaleniuk
(https://www.manning.com/books/geometry-for-programmers), transformed and
paraphrased, especially chapters 2 and 9.
Core Workflow
- Identify the geometric question: angle, projection, side, area, normal,
volume, distance, facing, or interpolation.
- Choose the primitive that answers that question directly.
- Check the input assumptions: dimensionality, coordinate space, units,
handedness, normalization, and zero-length vectors.
- Preserve sign when orientation or sidedness matters. Discard sign only when
the caller truly needs magnitude.
- Name intermediate values by meaning:
normal, signed_area2,
light_factor, plane_distance, facing, projection_t.
- Add tests that exercise orientation, reversed winding, orthogonal vectors,
parallel vectors, and zero-length inputs.
Primitive Guide
Dot Product
Use dot products for:
- Projection onto a direction.
- Angle/facing tests.
- Lambert-style lighting coefficients.
- Distance along a ray or segment.
- Checking orthogonality: dot equals zero under the numeric policy.
If both vectors are normalized, the dot product is the cosine of the angle
between them. Clamp before inverse cosine if a display angle is needed.
Cross Product And 2D Cross Scalar
Use cross products for:
- Surface normals from two triangle edges.
- Signed 2D orientation tests.
- Twice the signed area of a 2D triangle.
- Detecting parallel vectors through near-zero magnitude.
- Building tangent frames with a known handedness.
For 2D, use the scalar a.x * b.y - a.y * b.x instead of constructing fake 3D
vectors unless the codebase already uses a 3D type.
Triple Product
Use the scalar triple product for:
- Signed volume.
- Point side relative to an oriented plane.
- Point-to-plane distance when divided by the plane normal length.
- Tetrahedron orientation and containment-style predicates.
Keep the sign when side matters. Take absolute value only for unsigned volume or
distance.
Guardrails
- Never normalize before checking vector length.
- Distinguish a direction vector from a point vector in naming and APIs.
- Keep normals in the same space as the vectors they are compared with.
- Recompute or transform normals correctly after nonuniform scale.
- Decide whether clockwise or counterclockwise winding is canonical.
- Use squared length when comparing distances and no square root is needed.
- Avoid inverse trigonometric functions in hot paths unless a real angle is
required. Dot thresholds are usually cheaper.
Common Mistakes
- Treating every vector as normalized because examples used unit vectors.
- Losing useful sign information by taking absolute values too early.
- Mixing coordinate spaces in one dot or cross operation.
- Using angle calculations where a dot-product threshold would be simpler.
- Flipping triangle winding without updating normals, culling, or orientation
predicates.
Source: hashgraph-online/awesome-codex-plugins → plugins/LVTD-LLC/skills/skills/game-vector-math-primitives/SKILL.md
1---2name: game-vector-math-primitives3description: Apply vector math primitives for game rendering, collision, orientation, lighting, steering, and geometry predicates. Use when working with dot products, cross products, triple products, normals, projections, signed areas, signed volumes, facing checks, or vector normalization.4---567# Game Vector Math Primitives89Use this skill when game code depends on vector operations whose geometric10meaning matters. Prefer clear primitives and named intent over opaque formula11paste.1213Primary source: Geometry for Programmers by Oleksandr Kaleniuk14(https://www.manning.com/books/geometry-for-programmers), transformed and15paraphrased, especially chapters 2 and 9.1617## Core Workflow18191. Identify the geometric question: angle, projection, side, area, normal,20 volume, distance, facing, or interpolation.212. Choose the primitive that answers that question directly.223. Check the input assumptions: dimensionality, coordinate space, units,23 handedness, normalization, and zero-length vectors.244. Preserve sign when orientation or sidedness matters. Discard sign only when25 the caller truly needs magnitude.265. Name intermediate values by meaning: `normal`, `signed_area2`,27 `light_factor`, `plane_distance`, `facing`, `projection_t`.286. Add tests that exercise orientation, reversed winding, orthogonal vectors,29 parallel vectors, and zero-length inputs.3031## Primitive Guide3233### Dot Product3435Use dot products for:3637- Projection onto a direction.38- Angle/facing tests.39- Lambert-style lighting coefficients.40- Distance along a ray or segment.41- Checking orthogonality: dot equals zero under the numeric policy.4243If both vectors are normalized, the dot product is the cosine of the angle44between them. Clamp before inverse cosine if a display angle is needed.4546### Cross Product And 2D Cross Scalar4748Use cross products for:4950- Surface normals from two triangle edges.51- Signed 2D orientation tests.52- Twice the signed area of a 2D triangle.53- Detecting parallel vectors through near-zero magnitude.54- Building tangent frames with a known handedness.5556For 2D, use the scalar `a.x * b.y - a.y * b.x` instead of constructing fake 3D57vectors unless the codebase already uses a 3D type.5859### Triple Product6061Use the scalar triple product for:6263- Signed volume.64- Point side relative to an oriented plane.65- Point-to-plane distance when divided by the plane normal length.66- Tetrahedron orientation and containment-style predicates.6768Keep the sign when side matters. Take absolute value only for unsigned volume or69distance.7071## Guardrails7273- Never normalize before checking vector length.74- Distinguish a direction vector from a point vector in naming and APIs.75- Keep normals in the same space as the vectors they are compared with.76- Recompute or transform normals correctly after nonuniform scale.77- Decide whether clockwise or counterclockwise winding is canonical.78- Use squared length when comparing distances and no square root is needed.79- Avoid inverse trigonometric functions in hot paths unless a real angle is80 required. Dot thresholds are usually cheaper.8182## Common Mistakes8384- Treating every vector as normalized because examples used unit vectors.85- Losing useful sign information by taking absolute values too early.86- Mixing coordinate spaces in one dot or cross operation.87- Using angle calculations where a dot-product threshold would be simpler.88- Flipping triangle winding without updating normals, culling, or orientation89 predicates.9091---9293**Source:** [`hashgraph-online/awesome-codex-plugins`](https://github.com/hashgraph-online/awesome-codex-plugins) → `plugins/LVTD-LLC/skills/skills/game-vector-math-primitives/SKILL.md`