Compound Bodies
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
);