Compound Bodies
Create complex physics objects from multiple shapes using PhysicsShapeContainer in Physics V2:
// Create a compound shape (e.g., a dumbbell) using Physics V2
const dumbbell = new BABYLON.Mesh("dumbbell", scene);
dumbbell.position.y = 10;
// Create child meshes for visualization
const bar = BABYLON.MeshBuilder.CreateCylinder("bar", {
height: 5, diameter: 0.5
}, scene);
bar.parent = dumbbell;
const leftWeight = BABYLON.MeshBuilder.CreateSphere("leftWeight", { diameter: 2 }, scene);
leftWeight.position.x = -2.5;
leftWeight.parent = dumbbell;
const rightWeight = BABYLON.MeshBuilder.CreateSphere("rightWeight", { diameter: 2 }, scene);
rightWeight.position.x = 2.5;
rightWeight.parent = dumbbell;
// Create a compound shape using PhysicsShapeContainer
const containerShape = new BABYLON.PhysicsShapeContainer(scene);
// Add child shapes with local transforms
const barShape = new BABYLON.PhysicsShapeCylinder(
new BABYLON.Vector3(0, -2.5, 0),
new BABYLON.Vector3(0, 2.5, 0),
0.25, scene
);
containerShape.addChild(barShape);
const leftSphereShape = new BABYLON.PhysicsShapeSphere(
new BABYLON.Vector3(-2.5, 0, 0), 1, scene
);
containerShape.addChild(leftSphereShape);
const rightSphereShape = new BABYLON.PhysicsShapeSphere(
new BABYLON.Vector3(2.5, 0, 0), 1, scene
);
containerShape.addChild(rightSphereShape);
// Create body with the compound shape
const body = new BABYLON.PhysicsBody(dumbbell, BABYLON.PhysicsMotionType.DYNAMIC, false, scene);
body.shape = containerShape;
body.setMassProperties({ mass: 10 });