Skillshub xmath-usage
Provides xmath API reference and in-place math optimization patterns for Defold. Use when writing performance-critical math code, optimizing vector/quaternion/matrix operations, or when the user mentions xmath, zero-allocation math, or reducing Lua GC pressure.
install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/indiesoftby/defold-agent-config/xmath-usage" ~/.claude/skills/comeonoliver-skillshub-xmath-usage && rm -rf "$T"
manifest:
skills/indiesoftby/defold-agent-config/xmath-usage/SKILL.mdsource content
Using xmath for Zero-Allocation Math in Defold
Prerequisite: Verify xmath Dependency
Before applying any guidance from this skill, you MUST confirm that the project uses xmath. Check the
game.project file for a dependency URL containing thejustinwalsh/defold-xmath (e.g. dependencies#N = https://github.com/thejustinwalsh/defold-xmath/archive/...). Alternatively, check for the presence of xmath/ in the .deps/ directory.
If neither an xmath dependency in
game.project nor a local xmath module is found, do NOT apply this skill. Inform the user that the project does not use xmath and suggest adding the dependency:
[project] dependencies#N = https://github.com/thejustinwalsh/defold-xmath/archive/refs/heads/main.zip
Core Concept: In-Place Mutation to Eliminate Heap Allocations
Standard
vmath creates a new Lua object on every operation, causing constant GC pressure in hot loops:
-- BAD: vmath allocates 3 new objects every frame function update(self, dt) local v = self.dir * 5 * dt -- alloc #1 local pos = go.get_position() -- alloc #2 local result = pos + v -- alloc #3 go.set_position(result) end
xmath mutates an existing variable in place — the result is written into the first argument. You allocate once, reuse forever:
-- GOOD: xmath reuses pre-allocated variables, zero allocations per frame go.property("dir", vmath.vector3(0, 1, 0)) local v = vmath.vector3() -- allocate ONCE at module scope function update(self, dt) local pos = go.get_position() xmath.mul(v, self.dir, 5 * dt) -- writes into v xmath.add(v, pos, v) -- writes into v go.set_position(v) end
Key Rules
- Pre-allocate scratch variables at module scope or in
— never insideinit()
orupdate()
.on_message() - The output variable is always the first argument — this is the fundamental calling convention difference from
.vmath - Functions return nothing — you cannot chain calls. Use a scratch variable at each step.
- Use
to create initial objects —vmath
,vmath.vector3()
,vmath.vector4()
,vmath.quat()
to allocate scratch buffers, then usevmath.matrix4()
to operate on them.xmath - Type polymorphism — functions like
work forxmath.lerp
,vector3
, andvector4
based on the output argument type.quaternion
Optimization Pattern
-- Scratch variables — allocated once local temp_v = vmath.vector3() local temp_q = vmath.quat() function update(self, dt) -- Instead of: local dir = vmath.normalize(target - pos) xmath.sub(temp_v, self.target, self.pos) xmath.normalize(temp_v, temp_v) -- can use same variable as both input and output -- Instead of: local rot = vmath.quat_rotation_z(angle) xmath.quat_rotation_z(temp_q, self.angle) -- Instead of: local rotated = vmath.rotate(rot, dir) xmath.rotate(temp_v, temp_q, temp_v) end
Full API Reference
All functions write the result into the first argument. No return values.
Vector Operations (vector3 / vector4)
| Function | Equivalent | Description |
|---|---|---|
| | Add two vectors |
| | Subtract two vectors |
| | Multiply vector by scalar |
| | Divide vector by scalar |
| | Cross product (vector3 only) |
| | Element-wise multiplication |
| | Normalize vector |
| | Rotate vector3 by quaternion |
| | Reset to zero vector |
Interpolation (vector3 / vector4 / quaternion)
| Function | Equivalent | Description |
|---|---|---|
| | Linear interpolation |
| | Spherical interpolation |
Quaternion Operations
| Function | Description |
|---|---|
| Reset to identity |
| Conjugate of quaternion |
| Quaternion from axis + angle |
| Quaternion from 3 basis vectors (vector3) |
| Rotation quaternion from v1 to v2 |
| Rotation around X axis |
| Rotation around Y axis |
| Rotation around Z axis |
Matrix Operations (matrix4)
| Function | Description |
|---|---|
| Reset to identity or copy from m1 |
| Rotation matrix from axis + angle |
| Matrix from quaternion |
| Frustum projection matrix |
| Matrix inverse |
| View matrix |
| Orthographic projection |
| Orthographic inverse |
| Perspective projection |
| Rotation around X axis |
| Rotation around Y axis |
| Rotation around Z axis |
| Translation matrix from vector3/vector4 |