logo
Physics

core

Physics

Free

Install with the CLI:

bjs download Physics

Physics

Rigid-body simulation that drops onto any meshed entity, driven through the renderer adapter.

What it does

Physics is the shared rigid-body substrate: attach it alongside a MeshPrimitive and, once that mesh's handle is ready, the PhysicsSystem asks the adapter to create a body (physicsCreateBody) and steps the world each frame (physicsStep). The renderer adapter decides the implementation — Havok under Babylon, a pure-core Euler integrator under Three or the test mock — so the System stays renderer-agnostic. It emits physics.body.created when a body is registered and physics.body.disposed when one is removed, and listens for input events to set velocity, gravity, restitution, simulation rate, collider visibility, and to pause/resume.

Use it in a scene

A scene is just data — a list of entities, each with its components. At startup the SceneLoader turns this JSON into a live world; edit the file and reload to rebuild it.

Fastest path

bjs download scene Physics

Pulls this exact scene into your project and runs it — no copy-paste. Add --all to grab the components and assets it needs too.

Or build it from scratch with the bjs CLI:

  1. 1Scaffold a project
    npm create @babylonjsmarket/arcade@latest my-game

    World, renderer, and dev server — ready to run.

  2. 2Install dependencies
    cd my-game && npm install
  3. 3Add Physics
    bjs download Physics

    Copies its source into src/ so the scene resolves.

    First time? Run bjs login once.

  4. 4Paste the scene into src/scenes/arcade-room.ts and run
    npm run dev

    SceneLoader builds the world from the JSON; reload to rebuild.

JSON
{
  "entities": {
    "Ball": {
      "components": {
        "MeshPrimitive": {
          "primitive": "sphere",
          "diameter": 1,
          "position": [0, 5, 0]
        },
        "Physics": { "shapeType": "sphere", "motionType": "dynamic", "mass": 1, "restitution": 0.5 }
      }
    }
  }
}

Props

  • shapeType ('sphere' | 'box' | 'capsule', default 'sphere') — collider shape for the body.
  • motionType ('dynamic' | 'static' | 'kinematic', default 'dynamic', or 'static' when mass is 0) — dynamic falls under gravity, static never moves, kinematic is driven by the caller and ignores gravity.
  • mass (number, default 1) — body mass.
  • friction (number, default 0.5) — surface friction, bleeds off sliding speed on contact.
  • restitution (number, default 0.5) — bounciness on impact.
  • lockRotation (boolean, default false) — prevent the body from rotating.
  • autoCreate (boolean, default true) — when true, the System creates the body automatically once the sibling MeshPrimitive's handle is ready; set false to hand-drive creation.
  • simulationHz (number, default 60) — fixed engine simulation rate in Hz. Only the first PhysicsComponent encountered is consulted for the world-wide rate (e.g. pinball wants ~240).

Events

  • emits physics.body.created once the adapter creates a body for an entity, and physics.body.disposed when its body is removed.
  • listens for physics.setVelocity ({ entityId, vx, vy, vz }), physics.pause, physics.resume, physics.setTimeStep ({ hz }), physics.setGravity ({ x, y, z }), physics.setRestitution ({ entityId, restitution }), and physics.setCollidersVisible ({ visible }).

Dependencies

MeshPrimitive — Physics waits for the sibling MeshPrimitive's mesh handle before creating a body, so an entity needs both.

Notes

  • All physics work goes through the renderer adapter (this.world.renderer), never through @babylonjs or three directly. Under Babylon the adapter delegates to Havok; under Three or the mock it falls back to the pure-core integrator in Physics.core.ts.
  • Body creation is deferred until the mesh handle lands and is re-checked cheaply each tick, so attaching Physics before the mesh exists is fine.
  • physics.setTimeStep is a no-op on adapters without a configurable rate (Three/Mock). Higher Hz reduces tunneling at the cost of CPU.
  • The pure core clamps each step's dt and pins bodies to a ground plane at y = 0; it's intentionally simpler than Havok's constraint solver.

More like this

AIGoalSeek
AIKick
MeshPrimitive
Movement
AiPilot
Animation
ArcCamera
Asteroid
BoatMode
BoatWater
Bullet
Bumper
CameraFollow
ContactDamage
ContactImpulse
DirectionalLight
EnemySpawner
EnvironmentTexture
FreighterCar
FreighterChain
FreighterHead
GameConfig
Health
HemisphericLight
Jump
KeyboardInput
KeyboardMover
Lifetime
LineOfSight
MachineGun
Mesh
Missile
MissileLauncher
MissionDirector
MouseHole
MouseInput
Obstacle
ObstacleField
OilBlob
Parts
PlayerInput
PlayerWalkAnimator
RespawnTimer
RoundReset
Score
Shadow
ShipLoadout
SkeletonAnimator
SpaceShooterBullet
SpaceShooterHealth
SpaceShooterScore
Spinner
SurfacePatch
Transform6DOF
TwinStickEnemy
Velocity6DOF
WaveDirector
Waypoint
WaypointTrack

Was this page helpful?

We read every note — tell us what's working and what isn't.

↑↓ NavigateEnter SelectEsc CloseCtrl+K Open Search