Index of every page on this site: llms.txt
All skills
oahc09 avatar

/cocosair-math

@ed38648
by oahc09oahc09/cocosairjs3 stars
2
Run it yourself

Compute vectors, matrices, quaternions, colors and easing. Use for out-first math, frozen Vec3.UP errors, transform composition, rotation units or Mat4.invert.

  • 9 files
  • 119.9 KB
  • MIT
  • Updated last month
  • GitHub
Use this Skill: https://skilld.dev/gh/oahc09/cocosairjs/cocosair-math

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencescommon-mistakes.md

≈1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Derived from the cocosair-math skill and its canonical docs/source pointers; reorganized on 2026-10-11. Source annotations below identify repository inputs, not files guaranteed to ship to npm consumers.

Common Mistakes

[HIGH] Writing through a frozen vector constant

Wrong:

Vec3.UP.x = 5;                       // TypeError: Cannot assign to read only property 'x'
Vec3.UP.set(0, 10, 0);               // TypeError too - set() assigns to frozen fields
const dir = new Vec3(1, 1, 0);
Vec3.normalize(Vec3.ZERO, dir);      // the constant passed as out is frozen

Correct:

const dir = new Vec3(1, 1, 0);
const up = new Vec3();
Vec3.copy(up, Vec3.UP);
up.y = 10;
const dirOut = new Vec3();
Vec3.normalize(dirOut, dir);      // pass a private, mutable out target

Agents copy the constant because it reads like a default value; every static here writes its first argument, and freezeVec3/Object.freeze makes those constants non-writable, so a write (direct or through set) throws TypeError in bundled ESM instead of quietly producing a wrong vector.

[HIGH] Sharing one Vec3 between owners and then mutating it

Wrong:

const sharedOffset = new Vec3(1, 0, 0);
const holderA = { offset: sharedOffset };  // holder A stores the reference, not a copy
const holderB = { offset: sharedOffset };  // holder B stores the same reference
sharedOffset.set(9, 9, 9);                 // both holders now read 9,9,9

Correct:

const sharedOffset = new Vec3(1, 0, 0);
const holderB = { offset: Vec3.clone(sharedOffset) }; // give each owner its own instance
const gravity = new Vec3(0, -9.8, 0);
const tmp = new Vec3();                              // mutate tmp, which nothing else holds
Vec3.add(tmp, sharedOffset, gravity);

Reusing one instance across frames is the engine's own style, so an agent hoists a Vec3 to module scope and keeps writing it; because instance fields are mutable and only copied where a setter copies, the holders that kept a reference all observe the last write.

[MEDIUM] Expecting static operations to allocate a new vector

Wrong:

const a = new Vec3(1, 0, 0); const b = new Vec3(0, 1, 0);
const mid = Vec3.lerp(a, b, 0.5);   // intent: midpoint of a and b
console.log(mid.x);                 // NaN - and `a` itself was overwritten in place

Correct:

const a = new Vec3(1, 0, 0); const b = new Vec3(0, 1, 0);
const mid = new Vec3();
Vec3.lerp(mid, a, b, 0.5);          // signature is lerp(out, a, b, t)
const other = Vec3.clone(a);        // clone is the allocating member of the family

Array-style math libraries return fresh values, so an agent shifts the arguments left; the real signature is lerp(out, a, b, t) (src/cocos/core/math/vec3.ts:373), which reads b.x off the number 0.5 - property access on a number is undefined, not an error, so the write produces NaN in out and nothing throws.

[MEDIUM] Feeding 0-1 fractions into Color channels

Wrong:

const tint = new Color(0.5, 0.5, 0.5);   // bytes: 0.5 rounds to 0 -> solid black, alpha 255

Correct:

const tint = new Color(128, 128, 128, 255); // 0-255 channel convention
const half = new Color();
half.x = 0.5;                               // the x/y/z/w accessors take fractions (byte 128)

Color writes its channels into a Uint8ClampedArray (src/cocos/core/math/color.ts:340), so 0-1 input collapses to 0 or 1 and out-of-range numbers clamp; an agent that reads "normalized color" from the shader side does the division a second time and gets a solid black or solid white tint with no warning.

[MEDIUM] Mixing degrees and radians across euler and axis-angle APIs

The two families are split by entry point, not by type: Quat.fromEuler/Node.setRotationFromEuler/Quat.toEuler take degrees, while Quat.fromAxisAngle/Mat4.rotate*/Mat3.toEuler/Quat.getAxisAngle take radians, so the object just spins at the wrong speed and nothing reports it. Wrong and Correct snippets plus the per-entry unit table are in transforms.md.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No third-party reports yet.

Signed by skilld at ed38648. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated last month
layer
subsystem
All 1 allowed tools
Read Edit Write Bash
Other metadata
compatibility
Cocos AIR source checkout or installed cocosair.js package. Browser rendering requires WebGL 2; source tooling requires Node.js >=18. Use the host-provided tools and user-authorized workspace.
when_to_use
[
  "normalize or project a direction",
  "compose or decompose a transform matrix",
  "choose quaternion rotation and angle units",
  "convert colors or calculate easing and deterministic random",
  "writing a frozen vector constant throws"
]
owns
[
  "vector-math",
  "matrix-transforms",
  "quaternion",
  "color-values",
  "easing",
  "deterministic-random"
]
not_for
[
  "screen and world camera conversion -> cocosair-camera",
  "native tween playback -> cocosair-animation-tween",
  "node parenting and lifecycle -> cocosair-scene-graph"
]
metadata
{
  "author": "oahc09@gmail.com",
  "version": "1.0.0",
  "last-updated": "2026-10-13",
  "description_zh": "使用 Cocos 数学类型进行向量、矩阵、四元数、颜色和插值计算。用于坐标变换、旋转、颜色处理、缓动或确定性随机。"
}

README badge

README badge for oahc09/cocosairjs/cocosair-math