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

/cocosair-physics

@ed38648
by oahc09oahc09/cocosairjs3 stars
2
Run it yourself

Choose builtin, cannon or Box2D physics; wire rigid bodies, colliders and raycasts. Use for missing gravity, trigger filters, backend limits or material error 9642.

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

Nothing lands on disk. Nothing to clean up.

Fork this Skill

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

referencesbackend-selection.md

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

Derived from src/air/physics-backend.ts, src/air/app-config.ts, src/air/bootstrap.ts, src/cocos/physics/framework/physics-selector.ts, tools/build/build.cjs, docs/notes/physics-asset-sourcing.md, docs/notes/physics-3d-notes.md - 2026-10-10.

Backend selection in Cocos AIR

The option and its default

  • AirAppOptions.physics?: 'builtin' | 'cannon' (src/air/app.ts:72), typed AirPhysicsBackend (src/air/physics-backend.ts:21, engine ids mapped at :23-26).
  • Normalization: const physics = options.physics ?? 'builtin' (src/air/app-config.ts:45); any other value throws AIR_E_PHYSICS_BACKEND (src/air/app-config.ts:46). So createAirApp({}) is createAirApp({ physics: 'builtin' }), byte-for-byte the previous shipping behavior (docs/reference/api-stability.md:93).
  • The frozen normalized options compare physics in identity checks (src/air/app-config.ts:108, :115).

When and how the choice is applied

  1. initializeAirApp registers selectPhysicsBackend(options.physics) on Game.EVENT_PRE_SUBSYSTEM_INIT before game.init() and removes it in the finally (src/air/bootstrap.ts:141-142, :165).
  2. Both upstream backends self-register on the same event - builtin with five wrappers (src/cocos/physics/cocos/instantiate.ts:33-40), cannon with the full wrapper set (src/cocos/physics/cannon/instantiate.ts:45); the AIR listener runs last inside the event, so the user's pick deterministically wins (src/air/bootstrap.ts:137-140).
  3. The official contract is "the system will use the last backend registered before initialization" (src/cocos/physics/framework/physics-selector.ts:100-107); register writes the registry entry and moves selector.id/selector.wrapper while no world exists yet (:127-136), syncing the legacy CC_PHYSICS_BUILTIN / CC_PHYSICS_CANNON macros (:121-125).
  4. selectPhysicsBackend then calls selector.register(engineId, wrapper) (src/air/physics-backend.ts:64) and seeds the default material (:65).

Failure modes, exactly

Condition Behavior Citation
Registry empty (source graph or tests, no physics feature imported) console.warn only, engine keeps its tolerated PHYSICS_NONE state src/air/physics-backend.ts:46-52
Requested id absent from a non-empty registry throw Error listing registered ids src/air/physics-backend.ts:45-57
Selection after selector.physicsWorld exists throw Error, late selection unsupported src/air/physics-backend.ts:58-62

PHYSICS_NONE is a real capability constant on the system class (src/cocos/physics/framework/physics-system.ts:48-53). selector.switchTo is the post-world path (src/cocos/physics/framework/physics-selector.ts:145-164) and is not offered by AIR as a runtime switch (docs/reference/api-stability.md:52).

Default physics material and error 9642

  • PhysicsSystem.initDefaultMaterial looks up builtinResMgr.get('default-physics-material'); on a miss it logs errorID(9642) and returns with _material unset (src/cocos/physics/framework/physics-system.ts:223-230); message text "Failed to load builtinMaterial" (line 420 of DebugInfos.json under src/).
  • Benign for builtin: BuiltinShape.setMaterial(v) {} is an empty function, so the missing material never dereferences (docs/notes/physics-3d-notes.md:26, :396-397).
  • Fatal for cannon without compensation: CannonShape.setMaterial falls back to PhysicsSystem.instance.defaultMaterial and reads its fields, throwing a TypeError per shape (docs/notes/physics-3d-notes.md:390-395).
  • Compensation: seedDefaultPhysicsMaterial registers a PhysicsMaterial named default-physics-material in builtinResMgr - idempotently at AIR module evaluation (src/air/physics-backend.ts:74-85) and again on selection (:65) - so the createAirApp path shows no 9642 on either backend.

What is not shipped

  • Default AIR_FEATURES: 'physics-2d-framework', 'physics-2d-box2d', 'physics-builtin', 'physics-cannon' (tools/build/build.cjs:32, :41); physics-2d-builtin is deliberately excluded (tools/build/build.cjs:31).
  • ammo/bullet, physx and box2d-wasm sources exist in src/cocos/physics/bullet/, src/cocos/physics/physx/ and src/cocos/physics-2d/box2d-wasm/, but each imports Emscripten artifacts under external:emscripten/** that are absent from the upstream snapshot, from upstream git, from the installed @cocos/box2d@1.0.2 (JS-only), and from any fetch hook (docs/notes/physics-asset-sourcing.md:11-17, :20-26). Missing assets fail only at runtime import, which is why the sources compile (docs/notes/physics-asset-sourcing.md:30).
  • Consequence: selector.backend in the default bundle carries 'builtin' and 'cannon.js' for 3D and 'box2d' for 2D (src/cocos/physics-2d/box2d/instantiate.ts:42-43); no WASM id is selectable, and the honest fallback plan on record is the D8 partial-pass wording in docs/notes/physics-asset-sourcing.md:44-47.

Identity checks that actually work

  • PhysicsSystem.PHYSICS_BUILTIN / PHYSICS_CANNON / PHYSICS_NONE constants (src/cocos/physics/framework/physics-system.ts:48-66) plus PhysicsSystem.instance.physicsWorld.constructor.name ('BuiltInWorld' or 'CannonWorld', docs/notes/physics-3d-notes.md:48).
  • Do not use the flat selector import for 3D: the name is claimed by the 2D selector in the aggregate export (docs/notes/physics-3d-notes.md:40-45; build/cocosair.module.js exports selector$1 as selector, currently :161574).

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
[
  "dynamic bodies do not fall or stack",
  "triggers stop firing after changing collision groups",
  "choose builtin or cannon before startup",
  "query 2D bodies with raycasts or overlap tests",
  "physics material error 9642 appears"
]
owns
[
  "builtin-physics",
  "cannon",
  "box2d",
  "collision-filter",
  "rigid-body",
  "physics-queries"
]
not_for
[
  "camera pixel-to-ray conversion -> cocosair-camera",
  "node hierarchy and transforms -> cocosair-scene-graph",
  "non-physics startup errors -> cocosair-application"
]
metadata
{
  "author": "oahc09@gmail.com",
  "version": "1.0.0",
  "last-updated": "2026-10-13",
  "description_zh": "选择 builtin 或 cannon,并区分碰撞检测、运动积分和二维 Box2D 的能力。用于刚体、触发器、射线查询或物理后端问题。"
}

README badge

README badge for oahc09/cocosairjs/cocosair-physics