--- name: dev-r3f-r3f-physics description: Physics integration with Rapier for R3F game development category: r3f --- # R3F Physics > "Physics makes games feel real – Rapier is fast and reliable." ## When to Use This Skill Use when: - Adding physics simulation to objects - Implementing collision detection - Creating vehicles or character controllers - Building interactive environments ## Quick Start ```tsx import { Physics, RigidBody } from '@react-three/rapier'; function PhysicsScene() { return ( {/* Falling cube */} {/* Static floor */} ); } ``` ## Decision Framework | Need | RigidBody Type | | ------------------ | ------------------- | | Player/Vehicle | `dynamic` | | Ground/Walls | `fixed` | | Platforms (moving) | `kinematicPosition` | | Triggers | `fixed` + sensor | | Collision Shape | Use For | | --------------- | ----------------------------- | | `cuboid` | Boxes, buildings | | `ball` | Spheres, wheels | | `capsule` | Characters | | `convexHull` | Complex convex shapes | | `trimesh` | Complex concave (static only) | ## Progressive Guide ### Level 1: Basic Physics ```tsx import { Physics, RigidBody } from '@react-three/rapier'; function BasicPhysics() { return ( {/* Dynamic body - affected by physics */} {/* Fixed body - doesn't move */} ); } ``` ### Level 2: Collision Events ```tsx import { RigidBody } from '@react-three/rapier'; function CollisionBox() { const handleCollisionEnter = (event) => { console.log('Collision with:', event.colliderObject); }; const handleCollisionExit = (event) => { console.log('Collision ended with:', event.colliderObject); }; return ( ); } ``` ### Level 3: Sensors (Triggers) ```tsx import { RigidBody, CuboidCollider } from '@react-three/rapier'; function TriggerZone() { const handleIntersection = (event) => { console.log('Something entered the zone!'); }; return ( {/* Visual representation (optional) */} ); } ``` ### Level 4: Applying Forces ```tsx import { useRef } from 'react'; import { RigidBody, RapierRigidBody } from '@react-three/rapier'; import { useFrame } from '@react-three/fiber'; function JumpingBall() { const rigidBodyRef = useRef(null); const jump = () => { rigidBodyRef.current?.applyImpulse({ x: 0, y: 10, z: 0 }, true); }; useFrame(() => { // Apply continuous force (e.g., for movement) // rigidBodyRef.current?.applyForce({ x: 5, y: 0, z: 0 }, true); }); return ( ); } ``` ### Level 5: Vehicle Physics ```tsx import { useRef } from 'react'; import { RigidBody, useRapier } from '@react-three/rapier'; import { useFrame } from '@react-three/fiber'; function Vehicle() { const chassisRef = useRef(); const { rapier, world } = useRapier(); useFrame(() => { if (!chassisRef.current) return; // Get velocity const vel = chassisRef.current.linvel(); const speed = Math.sqrt(vel.x ** 2 + vel.z ** 2); // Apply steering based on input // ... implement steering logic }); return ( ); } ``` ## Collision Groups ```tsx // Define collision groups (bitmask) const GROUPS = { PLAYER: 0b0001, ENEMY: 0b0010, GROUND: 0b0100, PROJECTILE: 0b1000, }; // Player collides with ground and enemy, not projectiles ``` ## Anti-Patterns **DON'T:** - Use `trimesh` for dynamic bodies (not supported) - Apply forces every frame without deltaTime - Create/destroy RigidBodies frequently - Use physics for non-interactive decorations - Ignore collision layers (performance) **DO:** - Use appropriate collider shapes (simpler = faster) - Use `fixed` for static environment - Pool physics objects when possible - Use collision groups for filtering - Dispose physics world on scene change ## Physics Debugging ```tsx import { Physics, Debug } from '@react-three/rapier'; function DebugScene() { return ( {/* Shows collision shapes */} {/* Your physics objects */} ); } ``` ## Checklist Before implementing physics: - [ ] Appropriate RigidBody type selected - [ ] Collision shapes match visual reasonably - [ ] Collision groups configured - [ ] Events handled (if needed) - [ ] Forces scaled by deltaTime - [ ] Physics debug visualized during development ## Reference - [Rapier documentation](https://rapier.rs/docs/) - [@react-three/rapier docs](https://github.com/pmndrs/react-three-rapier) - `developer/r3f/r3f-fundamentals.md` — R3F basics