GPU Physics Engine

WebGPU compute-based physics engine with full PhysX 5.4.1 feature parity. 15 modules in engine/sim/physics/gpu/, ~400KB total. All shaders written in WGSL. No PhysX code modified — parallel implementation for future migration.

PhysX 5 Audit Complete. Every GPU-accelerated feature in PhysX 5.4.1 (rigid bodies, broadphase, contact gen, solver, CCD, vehicles, CCT, soft bodies, triggers, scene queries, heightfield, triangle mesh) is covered by a dedicated module. See the full parity table below.

Architecture

┌─────────────────────────────────────────────────────────────┐
│                    GPU Physics Pipeline                      │
│                                                             │
│  ┌──────────┐  ┌──────────┐  ┌───────────┐  ┌──────────┐  │
│  │  CCD     │→ │Broadphase│→ │Narrowphase│→ │  Solver  │  │
│  │  Expand  │  │  (Hash)  │  │  (PCM)    │  │ (TGS_Soft)│  │
│  └──────────┘  └──────────┘  └───────────┘  └──────────┘  │
│       ↓              ↓             ↓              ↓         │
│  ┌──────────┐  ┌──────────┐  ┌───────────┐  ┌──────────┐  │
│  │Heightfield│  │TriMesh  │  │  Triggers │  │Integration│  │
│  │ Contacts │  │ Contacts │  │  Events   │  │  + Sleep  │  │
│  └──────────┘  └──────────┘  └───────────┘  └──────────┘  │
│                                                             │
│  ┌──────────┐  ┌──────────┐  ┌───────────┐  ┌──────────┐  │
│  │ Vehicles │  │ Soft Body│  │   CCT     │  │ Readback │  │
│  │  (GPU)   │  │  (FEM)   │  │   (CPU)   │  │  (Async) │  │
│  └──────────┘  └──────────┘  └───────────┘  └──────────┘  │
│                                                             │
│  CPU-side queries (synchronous):                            │
│  ┌──────────┐  ┌──────────┐                                │
│  │  Raycast │  │  Scene   │                                │
│  │ (CPU/GPU)│  │  Query   │                                │
│  └──────────┘  └──────────┘                                │
└─────────────────────────────────────────────────────────────┘

Key Principles

PhysX 5 Feature Parity

Audited against PhysX 5.4.1 (nvidia-omniverse.github.io/PhysX/physx/5.4.1/).

GPU Physics Modules (15 files)

PhysX 5 FeatureOur ModuleStatus
GPU Rigid BodiesGPURigidBodyWorld.jsBuilt
GPU BroadphaseGPUBroadphase.jsBuilt
GPU Contact Gen (PCM)GPUNarrowphase.jsBuilt
GPU Constraint Solver (TGS)GPUConstraintSolver.jsBuilt
Convex Hull CookingGPUConvexHull.jsBuilt
RaycastingGPURaycast.jsBuilt
Speculative CCDGPUContinuousCollision.jsBuilt
Vehicle SDKGPUVehicle.jsBuilt
Character ControllerGPUCharacterController.jsBuilt
FEM Soft BodiesGPUSoftBody.jsBuilt
Trigger EventsGPUTriggerSystem.jsBuilt
Scene QueriesGPUSceneQuery.jsBuilt
Heightfield TerrainGPUHeightfield.jsBuilt
Triangle Mesh ColliderGPUTriangleMesh.jsBuilt

Existing Engine Systems (already built)

These modules in the parent engine/sim/physics/ directory already cover additional PhysX features. They are referenced from the GPU barrel export.

FeatureModuleNotes
PBD ClothGPUClothSolver.jsSmall substeps (Macklin 2019) + OGC contact
ArticulationsGPUArticulationSolver.jsCosserat rods, PBD stiff rods
SDF Mesh GeometrySDFCollision.jsSDF grid, mesh-to-SDF (PhysX 5 style)
PBD Solver (XPBD)PBDSolver.jsDistance / angle / volume, graph coloring
MLS-MPM Soft BodyMLSMPMSolver.jsSnow, sand, jelly, topology changes
OGC ContactOGCContact.jsPenetration-free barrier (SIGGRAPH 2025)
CPU CCDSpeculativeContacts.jsAABB expansion, time-of-closest-approach
Shock PropagationShockPropagation.jsGuendelman et al. SIGGRAPH 2003
Destruction (Blast SDK)VoronoiFracture.js + FragmentPhysics.jsVoronoi fracture + fragment manager
Convex DecompositionConvexDecomposition.jsV-HACD voxelization + decomposition
Voxel Mesh ColliderVoxelMeshCollision.jsGPU voxelized mesh collider with LOD
Spatial Hash (GPU)GPUSpatialHash.jsMorton codes (GPU Gems 3 Ch.32)

Features That Exceed PhysX 5

FeatureModuleWhy It's Unique
OGC Contact ModelOGCContact.jsSIGGRAPH 2025 — penetration-free barrier energy
MLS-MPMMLSMPMSolver.jsHandles topology changes (melting, fracture)
LBM WindWindSimulation.jsLattice Boltzmann GPU wind field
Thermal ParticlesParticleSimWorld.jsPhase transitions, buoyancy, chemistry
Rope InteractionRopeParticleInteraction.jsPer-fiber thermal (burn, wet, corrode)

Core Rigid Body Pipeline

GPURigidBodyWorld.js 55KB

Central world manager. Owns all GPU buffers, body descriptions, and the simulation island manager.

GPUBroadphase.js 22KB

Spatial hash broadphase. Outputs collision pair buffer for narrowphase.

GPUNarrowphase.js 30KB

Analytic shape-shape contact generation with persistent contact manifold (PCM).

GPUConstraintSolver.js 30KB

TGS_Soft iterative solver for contacts and joints (Box2D v3 / Catto 2023).

GPUConvexHull.js 19KB

CPU-side Quickhull 3D + GPU buffer upload. PhysX 64-vertex GPU limit enforced.

GPURaycast.js 24KB

GPU parallel raycasting (ray-sphere/box/capsule) with workgroup reduction. CPU fallback for synchronous queries.

GPUContinuousCollision.js 18KB

Speculative CCD to prevent tunneling of fast bodies.

Extended Simulation

GPUVehicle.js 27KB

Complete vehicle physics inspired by PhysX 5 Vehicle SDK.

GPUCharacterController.js 17KB

CPU-driven kinematic character controller (PhysX CCT equivalent).

GPUSoftBody.js 30KB

FEM soft body simulation using tetrahedral meshes (PhysX 5 signature feature).

GPUTriggerSystem.js 22KB

Trigger volumes and contact event system (PhysX PxSimulationEventCallback equivalent).

Geometry & Queries

GPUSceneQuery.js 19KB

CPU-side synchronous scene queries. Scene queries are typically 1-10 per frame and need immediate results, so GPU readback latency would negate the benefit.

GPUHeightfield.js 19KB

Heightmap-based terrain collider (PhysX PxHeightField equivalent).

GPUTriangleMesh.js 25KB

Static triangle mesh collider with BVH traversal (PhysX PxTriangleMeshGeometry equivalent).

Constants & Limits

ConstantValueModule
MAX_BODIES4096GPURigidBodyWorld
MAX_CONTACTS16384GPUNarrowphase (shared)
MAX_PAIRS32768GPUBroadphase
MAX_JOINTS2048GPUConstraintSolver
MAX_VEHICLES64GPUVehicle
MAX_WHEELS_PER_VEHICLE8GPUVehicle
MAX_TRIGGER_EVENTS4096GPUTriggerSystem
MAX_CCD_PAIRS8192GPUContinuousCollision
MAX_SOFT_BODY_NODES16384GPUSoftBody
MAX_SOFT_BODY_TETS32768GPUSoftBody
MAX_TRIMESH_TRIANGLES65536GPUTriangleMesh
MAX_TRIMESH_INSTANCES16GPUTriangleMesh
MAX_HEIGHTFIELD_SIZE1024GPUHeightfield

All limits are configurable via constructor options. PhysX 5 GPU pattern: fixed pre-allocated buffers with overflow warnings.

Pipeline Execution Order

Each physics frame should dispatch in this order:

  1. CCD Expand AABBsGPUContinuousCollision.dispatchExpand
  2. BroadphaseGPUBroadphase.dispatch
  3. NarrowphaseGPUNarrowphase.dispatch
  4. Heightfield ContactsGPUHeightfield.dispatch
  5. Triangle Mesh ContactsGPUTriangleMesh.dispatch
  6. CCD TOI ContactsGPUContinuousCollision.dispatchTOI
  7. Constraint SolverGPUConstraintSolver.dispatch
  8. Integration + SleepGPURigidBodyWorld.dispatchIntegration
  9. Trigger & Contact EventsGPUTriggerSystem.dispatch + processEvents
  10. Vehicle ForcesGPUVehicleManager.dispatch
  11. Soft Body StepGPUSoftBody.step
  12. Character ControllerCharacterControllerManager.updateAll
  13. ReadbackGPURigidBodyWorld.readbackPositions

Scene queries and raycasts can be called at any time (CPU-side, synchronous).

Design Decisions

DecisionRationaleSource
TGS_Soft solverSub-stepping + warm starting + soft constraintsBox2D v3 (Catto 2023)
Delta-position formulationFP32 stability far from world originErin Catto GDC 2024
Simulation islands (CPU)Union-find groups for per-island sleep/wakeJolt, PhysX 5
D6 as sole GPU jointAll types decompose to D6 — one shaderPhysX 5 GPU best practice
Spatial hash broadphaseO(1) insert/query, good GPU utilizationGPU Gems 3 Ch.32
Fixed buffersPre-allocated, overflow warnings, no runtime allocPhysX 5 GPU pattern
PCM contactsPersistent manifold + local anchors + feature IDsPhysX 5 eENABLE_PCM
CPU scene queries1-10 queries/frame need synchronous resultsPhysX 5 architecture
CPU character controllerLatency-sensitive input, kinematic sweep testsPhysX 5 CCT
Co-rotational FEMStable under large deformation, cheaper than Neo-HookeanMüller, Irving et al.
BVH for triangle meshMedian-split AABB tree, iterative GPU traversalEmbree, PhysX buildGPUData

File Listing

engine/sim/physics/gpu/
├── GPURigidBodyWorld.js       55KB  Core world + integration
├── GPUBroadphase.js           22KB  Spatial hash broadphase
├── GPUNarrowphase.js          30KB  Shape-shape contacts (PCM)
├── GPUConstraintSolver.js     30KB  TGS_Soft solver + D6 joints
├── GPUConvexHull.js           19KB  Quickhull 3D + GPU upload
├── GPURaycast.js              24KB  GPU parallel raycast
├── GPUContinuousCollision.js  18KB  Speculative CCD
├── GPUVehicle.js              27KB  Vehicle physics
├── GPUCharacterController.js  17KB  Kinematic CCT
├── GPUSoftBody.js             30KB  FEM soft body (tetrahedral)
├── GPUTriggerSystem.js        22KB  Triggers + contact events
├── GPUSceneQuery.js           19KB  Overlap / sweep queries
├── GPUHeightfield.js          19KB  Terrain heightfield
├── GPUTriangleMesh.js         25KB  Static mesh collider (BVH)
└── index.js                    3KB  Barrel export
                              ─────
                              ~400KB total

References

Academic

Engine