Skip to content
dev-r3f-r3f-physics logo

R3F Physics

dev-r3f-r3f-physics

Physics integration with Rapier for R3F game development

SKILL.md

Full skill instructions

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

import { Physics, RigidBody } from '@react-three/​rapier';

function PhysicsScene() {
  return (
    <Physics>
      {/​* Falling cube */​}
      <RigidBody>
        <mesh>
          <boxGeometry />
          <meshStandardMaterial />
        </​mesh>
      </​RigidBody>

      {/​* Static floor */​}
      <RigidBody type="fixed">
        <mesh position={[0, -2, 0]}>
          <boxGeometry args={[10, 0.5, 10]} />
          <meshStandardMaterial />
        </​mesh>
      </​RigidBody>
    </​Physics>
  );
}

Decision Framework

NeedRigidBody Type
Player/​Vehicledynamic
Ground/​Wallsfixed
Platforms (moving)kinematicPosition
Triggersfixed + sensor
Collision ShapeUse For
cuboidBoxes, buildings
ballSpheres, wheels
capsuleCharacters
convexHullComplex convex shapes
trimeshComplex concave (static only)

Progressive Guide

Level 1: Basic Physics

import { Physics, RigidBody } from '@react-three/​rapier';

function BasicPhysics() {
  return (
    <Physics gravity={[0, -9.81, 0]}>
      {/​* Dynamic body - affected by physics */​}
      <RigidBody position={[0, 5, 0]}>
        <mesh>
          <boxGeometry />
          <meshStandardMaterial color="red" />
        </​mesh>
      </​RigidBody>

      {/​* Fixed body - doesn't move */​}
      <RigidBody type="fixed">
        <mesh position={[0, -1, 0]} rotation={[-Math.PI / 2, 0, 0]}>
          <planeGeometry args={[20, 20]} />
          <meshStandardMaterial color="green" />
        </​mesh>
      </​RigidBody>
    </​Physics>
  );
}

Level 2: Collision Events

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 (
    <RigidBody onCollisionEnter={handleCollisionEnter} onCollisionExit={handleCollisionExit}>
      <mesh>
        <boxGeometry />
        <meshStandardMaterial />
      </​mesh>
    </​RigidBody>
  );
}

Level 3: Sensors (Triggers)

import { RigidBody, CuboidCollider } from '@react-three/​rapier';

function TriggerZone() {
  const handleIntersection = (event) => {
    console.log('Something entered the zone!');
  };

  return (
    <RigidBody type="fixed">
      <CuboidCollider args={[2, 2, 2]} sensor onIntersectionEnter={handleIntersection} />
      {/​* Visual representation (optional) */​}
      <mesh>
        <boxGeometry args={[4, 4, 4]} />
        <meshStandardMaterial color="blue" transparent opacity={0.3} />
      </​mesh>
    </​RigidBody>
  );
}

Level 4: Applying Forces

import { useRef } from 'react';
import { RigidBody, RapierRigidBody } from '@react-three/​rapier';
import { useFrame } from '@react-three/​fiber';

function JumpingBall() {
  const rigidBodyRef = useRef<RapierRigidBody>(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 (
    <RigidBody ref={rigidBodyRef} onClick={jump}>
      <mesh>
        <sphereGeometry />
        <meshStandardMaterial color="orange" />
      </​mesh>
    </​RigidBody>
  );
}

Level 5: Vehicle Physics

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 (
    <RigidBody
      ref={chassisRef}
      colliders="cuboid"
      mass={1500}
      linearDamping={0.5}
      angularDamping={0.5}
    >
      <mesh>
        <boxGeometry args={[2, 0.5, 4]} />
        <meshStandardMaterial color="blue" />
      </​mesh>
    </​RigidBody>
  );
}

Collision Groups

// Define collision groups (bitmask)
const GROUPS = {
  PLAYER: 0b0001,
  ENEMY: 0b0010,
  GROUND: 0b0100,
  PROJECTILE: 0b1000,
};

// Player collides with ground and enemy, not projectiles
<RigidBody
  collisionGroups={interactionGroups(
    GROUPS.PLAYER,
    GROUPS.GROUND | GROUPS.ENEMY
  )}
>

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

import { Physics, Debug } from '@react-three/​rapier';

function DebugScene() {
  return (
    <Physics>
      <Debug /> {/​* Shows collision shapes */​}
      {/​* Your physics objects */​}
    </​Physics>
  );
}

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

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View