Add physics properties to meshes to make them interact with the physics world:

// Create a ground plane with physics
const ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 50, height: 50}, scene);
ground.physicsImpostor = new BABYLON.PhysicsImpostor(
    ground,
    BABYLON.PhysicsImpostor.BoxImpostor, // Use box collision for ground
    { mass: 0, restitution: 0.3, friction: 0.3 }, // mass 0 = static object
    scene
);

// Create a dynamic sphere
const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2}, scene);
sphere.position.y = 10; // Start above ground
sphere.physicsImpostor = new BABYLON.PhysicsImpostor(
    sphere,
    BABYLON.PhysicsImpostor.SphereImpostor,
    { mass: 1, restitution: 0.7, friction: 0.1 }, // mass > 0 = dynamic object
    scene
);

// Create a dynamic box
const box = BABYLON.MeshBuilder.CreateBox("box", {size: 2}, scene);
box.position = new BABYLON.Vector3(0, 15, 0);
box.physicsImpostor = new BABYLON.PhysicsImpostor(
    box,
    BABYLON.PhysicsImpostor.BoxImpostor,
    { mass: 2, restitution: 0.2, friction: 0.4 },
    scene
);

Different collision shapes for different needs:

Babylon.js provides several impostor types for different collision needs:

  • BoxImpostor: Simple box collision - efficient for rectangular objects
  • SphereImpostor: Spherical collision - most efficient, good for round objects
  • CylinderImpostor: Cylindrical collision - for tubes, pillars, etc.
  • MeshImpostor: Uses actual mesh geometry - most accurate but computationally expensive
  • HeightmapImpostor: Efficient for terrain - uses height data for collision
  • ConvexHullImpostor: Creates simplified collision from mesh vertices - good balance
  • ParticleImpostor: Point with mass but no rotation - for simple particles
  • NoImpostor: For compound objects with child impostors

For performance, always use the simplest collision shape that adequately represents your object.

// Complex mesh with accurate collision
const complexMesh = BABYLON.MeshBuilder.CreateTorusKnot("tk", {}, scene);
complexMesh.position.y = 10;

// Options for different collision types
// 1. Exact mesh collision (expensive)
complexMesh.physicsImpostor = new BABYLON.PhysicsImpostor(
    complexMesh,
    BABYLON.PhysicsImpostor.MeshImpostor,
    { mass: 3, restitution: 0.4 },
    scene
);

// 2. Convex hull (better performance)
complexMesh.physicsImpostor = new BABYLON.PhysicsImpostor(
    complexMesh,
    BABYLON.PhysicsImpostor.ConvexHullImpostor,
    { mass: 3, restitution: 0.4 },
    scene
);

// 3. Simple approximation (best performance)
complexMesh.physicsImpostor = new BABYLON.PhysicsImpostor(
    complexMesh,
    BABYLON.PhysicsImpostor.SphereImpostor,
    { mass: 3, restitution: 0.4 },
    scene
);

Create complex physics objects from multiple shapes:

// Create a compound object (e.g., a dumbbell)
const dumbbell = new BABYLON.Mesh("dumbbell", scene);

// Create the middle bar
const bar = BABYLON.MeshBuilder.CreateCylinder("bar", {
    height: 5,
    diameter: 0.5
}, scene);

// Create the weights
const leftWeight = BABYLON.MeshBuilder.CreateSphere("leftWeight", {
    diameter: 2
}, scene);
leftWeight.position.x = -2.5;

const rightWeight = BABYLON.MeshBuilder.CreateSphere("rightWeight", {
    diameter: 2
}, scene);
rightWeight.position.x = 2.5;

// Make the parts children of the main mesh
bar.parent = dumbbell;
leftWeight.parent = dumbbell;
rightWeight.parent = dumbbell;

// Position the dumbbell
dumbbell.position.y = 10;

// Create compound physics impostor
dumbbell.physicsImpostor = new BABYLON.PhysicsImpostor(
    dumbbell,
    BABYLON.PhysicsImpostor.NoImpostor, // Parent has no direct impostor
    { mass: 10 },
    scene
);

// Add child impostors
bar.physicsImpostor = new BABYLON.PhysicsImpostor(
    bar,
    BABYLON.PhysicsImpostor.CylinderImpostor,
    { mass: 0 }, // Child masses are ignored when part of compound
    scene
);

leftWeight.physicsImpostor = new BABYLON.PhysicsImpostor(
    leftWeight,
    BABYLON.PhysicsImpostor.SphereImpostor,
    { mass: 0 },
    scene
);

rightWeight.physicsImpostor = new BABYLON.PhysicsImpostor(
    rightWeight,
    BABYLON.PhysicsImpostor.SphereImpostor,
    { mass: 0 },
    scene
);