agentleFS
alton47alton47/threejs-skills/skills/threejs-physics/SKILL.md

threejs-physics

Skill13 starsChanged 7 months agoMITView on GitHub

Integrates physics engines with Three.js — cannon-es, Rapier (WASM), and Ammo.js. Covers rigid bodies, constraints, collision detection, character controllers, and physics-visual sync. Use when the user asks about physics, collisions, rigid bodies, gravity, constraints, joints, or physics simulation. Trigger keywords: physics, cannon-es, rapier, ammo.js, rigid body, collision, gravity, constraint, character controller.

  • Installs packages

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Don't use Trimesh for dynamic bodies in cannon-es — only static.

    line 181

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Three.js Physics

cannon-es (simplest, JavaScript)

npm install cannon-es

See CANNON.md for complete reference.

import * as CANNON from "cannon-es";

// 1. Physics world
const world = new CANNON.World({ gravity: new CANNON.Vec3(0, -9.81, 0) });
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 10;

// 2. Ground (static)
const groundBody = new CANNON.Body({ mass: 0 }); // mass 0 = static
groundBody.addShape(new CANNON.Plane());
groundBody.quaternion.setFromEuler(-Math.PI / 2, 0, 0);
world.addBody(groundBody);

// 3. Box (dynamic)
const boxBody = new CANNON.Body({
  mass: 1,
  position: new CANNON.Vec3(0, 5, 0),
  shape: new CANNON.Box(new CANNON.Vec3(0.5, 0.5, 0.5)), // half-extents
});
world.addBody(boxBody);

// 4. Sync Three.js mesh to physics body
const boxMesh = new THREE.Mesh(
  new THREE.BoxGeometry(1, 1, 1),
  new THREE.MeshStandardMaterial(),
);
scene.add(boxMesh);

// 5. Render loop — step world, sync visuals
const fixedTimeStep = 1 / 60;
const maxSubSteps = 3;
const clock = new THREE.Clock();

function animate() {
  requestAnimationFrame(animate);
  const delta = clock.getDelta();
  world.step(fixedTimeStep, delta, maxSubSteps);

  // Sync mesh to body
  boxMesh.position.copy(boxBody.position);
  boxMesh.quaternion.copy(boxBody.quaternion);

  renderer.render(scene, camera);
}

Rapier (WASM, most accurate)

npm install @dimforge/rapier3d-compat

See RAPIER.md for complete reference.

import RAPIER from "@dimforge/rapier3d-compat";

await RAPIER.init(); // WASM init — must await before use

const world = new RAPIER.World({ x: 0, y: -9.81, z: 0 });

// Ground
const groundDesc = RAPIER.RigidBodyDesc.fixed();
const groundBody = world.createRigidBody(groundDesc);
const groundCollider = world.createCollider(
  RAPIER.ColliderDesc.cuboid(10, 0.1, 10),
  groundBody,
);

// Dynamic box
const boxDesc = RAPIER.RigidBodyDesc.dynamic().setTranslation(0, 5, 0);
const boxBody = world.createRigidBody(boxDesc);
const boxCollider = world.createCollider(
  RAPIER.ColliderDesc.cuboid(0.5, 0.5, 0.5),
  boxBody,
);

// Sync in loop
function animate() {
  requestAnimationFrame(animate);
  world.step();

  const pos = boxBody.translation();
  const rot = boxBody.rotation();
  boxMesh.position.set(pos.x, pos.y, pos.z);
  boxMesh.quaternion.set(rot.x, rot.y, rot.z, rot.w);

  renderer.render(scene, camera);
}

cannon-es: Common Shapes

new CANNON.Sphere(radius);
new CANNON.Box(new CANNON.Vec3(hw, hh, hd)); // half-extents
new CANNON.Plane(); // infinite flat plane
new CANNON.Cylinder(rTop, rBottom, height, segments);
new CANNON.Trimesh(vertices, indices); // concave — expensive
new CANNON.ConvexPolyhedron(points, faces); // convex — preferred for custom
new CANNON.Heightfield(data, { elementSize: 1 }); // terrain

cannon-es: Constraints & Joints

// Lock two bodies together at a point
const constraint = new CANNON.PointToPointConstraint(
  bodyA,
  new CANNON.Vec3(0, 0.5, 0),
  bodyB,
  new CANNON.Vec3(0, -0.5, 0),
);
world.addConstraint(constraint);

// Hinge (door, wheel)
const hinge = new CANNON.HingeConstraint(bodyA, bodyB, {
  pivotA: new CANNON.Vec3(0.5, 0, 0),
  pivotB: new CANNON.Vec3(-0.5, 0, 0),
  axisA: new CANNON.Vec3(0, 1, 0),
  axisB: new CANNON.Vec3(0, 1, 0),
});
world.addConstraint(hinge);

// Lock constraint (weld)
const lock = new CANNON.LockConstraint(bodyA, bodyB);
world.addConstraint(lock);

cannon-es: Collision Events

boxBody.addEventListener("collide", (event) => {
  const contact = event.contact;
  const velocity = contact.getImpactVelocityAlongNormal();
  if (Math.abs(velocity) > 1.5) {
    playImpactSound();
  }
});

cannon-es: Materials & Friction

const iceMaterial = new CANNON.Material("ice");
const groundMaterial = new CANNON.Material("ground");

const iceGroundContact = new CANNON.ContactMaterial(
  iceMaterial,
  groundMaterial,
  { friction: 0.0, restitution: 0.3 }, // restitution = bounciness
);
world.addContactMaterial(iceGroundContact);

boxBody.material = iceMaterial;
groundBody.material = groundMaterial;

Common Gotchas

  • cannon-es Box uses half-extents — new CANNON.Box(new CANNON.Vec3(0.5, 0.5, 0.5)) for a 1×1×1 cube
  • Rapier must be await RAPIER.init() before any use — WASM needs to load
  • mass: 0 in cannon-es = static body (never moves)
  • world.step(fixedTimeStep, delta, maxSubSteps) — use fixed step for determinism
  • Don't use Trimesh for dynamic bodies in cannon-es — only static. Use ConvexPolyhedron instead.
  • Sync mesh quaternion as mesh.quaternion.copy(body.quaternion) — both use the same XYZW format
  • Rapier cuboid also uses half-extents: ColliderDesc.cuboid(0.5, 0.5, 0.5) for 1×1×1

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.