Constraints and Joints
Connect physics objects with various types of joints:
// Create a hinge joint (like a door hinge)
const hingePivot = new BABYLON.Vector3(0, 5, 0);
const hingeAxis = new BABYLON.Vector3(0, 1, 0); // Rotate around Y axis
const hingeJoint = new BABYLON.PhysicsJoint(
BABYLON.PhysicsJoint.HingeJoint, {
mainPivot: hingePivot,
connectedPivot: new BABYLON.Vector3(0, 0, 0),
mainAxis: hingeAxis,
connectedAxis: hingeAxis
}
);
// Connect a box to a fixed anchor point
const anchor = BABYLON.MeshBuilder.CreateBox("anchor", {size: 0.5}, scene);
anchor.position = hingePivot;
anchor.physicsImpostor = new BABYLON.PhysicsImpostor(
anchor,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 0 }, // Fixed point (zero mass)
scene
);
// Create a panel to act as a door
const door = BABYLON.MeshBuilder.CreateBox("door", {
width: 0.5,
height: 6,
depth: 4
}, scene);
door.position = new BABYLON.Vector3(2, 5, 0);
door.physicsImpostor = new BABYLON.PhysicsImpostor(
door,
BABYLON.PhysicsImpostor.BoxImpostor,
{ mass: 5, friction: 0.5 },
scene
);
// Connect the door to the anchor with a hinge joint
anchor.physicsImpostor.addJoint(door.physicsImpostor, hingeJoint);
Other types of physical joints and constraints:
// Ball-and-socket joint (allows rotation in all directions)
const ballJoint = new BABYLON.PhysicsJoint(
BABYLON.PhysicsJoint.BallAndSocketJoint, {
mainPivot: new BABYLON.Vector3(0, 0, 0),
connectedPivot: new BABYLON.Vector3(0, 2, 0)
}
);
// Distance joint (maintains fixed distance between objects)
const distanceJoint = new BABYLON.PhysicsJoint(
BABYLON.PhysicsJoint.DistanceJoint, {
maxDistance: 5 // Maximum distance constraint
}
);
// Prismatic joint (allows movement along one axis only)
const prismaticJoint = new BABYLON.PhysicsJoint(
BABYLON.PhysicsJoint.PrismaticJoint, {
mainAxis: new BABYLON.Vector3(1, 0, 0), // Movement along X axis
connectedAxis: new BABYLON.Vector3(1, 0, 0)
}
);
// Slider joint with limits
const sliderJoint = new BABYLON.PhysicsJoint(
BABYLON.PhysicsJoint.SliderJoint, {
mainAxis: new BABYLON.Vector3(0, 1, 0),
connectedAxis: new BABYLON.Vector3(0, 1, 0)
}
);
sliderJoint.setLimit(1, 5); // Min and max distance
Create spring-like behavior with physics:
// Create a spring joint
const spring = new BABYLON.PhysicsJoint(
BABYLON.PhysicsJoint.SpringJoint, {
length: 5, // Rest length
stiffness: 50, // Spring strength
damping: 0.5, // Damping factor
collision: true // Allow connected bodies to collide
}
);
// Create anchor point
const springAnchor = BABYLON.MeshBuilder.CreateSphere("anchor", {diameter: 1}, scene);
springAnchor.position.y = 15;
springAnchor.physicsImpostor = new BABYLON.PhysicsImpostor(
springAnchor,
BABYLON.PhysicsImpostor.SphereImpostor,
{ mass: 0 }, // Fixed position
scene
);
// Create suspended object
const bob = BABYLON.MeshBuilder.CreateSphere("bob", {diameter: 2}, scene);
bob.position.y = 10;
bob.physicsImpostor = new BABYLON.PhysicsImpostor(
bob,
BABYLON.PhysicsImpostor.SphereImpostor,
{ mass: 2, restitution: 0.6 },
scene
);
// Connect with spring joint
springAnchor.physicsImpostor.addJoint(bob.physicsImpostor, spring);
// Visualize the spring connection with a line
const springLine = BABYLON.MeshBuilder.CreateLines("springLine", {
points: [springAnchor.position, bob.position],
updatable: true
}, scene);
// Update the line in the render loop
scene.onBeforeRenderObservable.add(() => {
springLine = BABYLON.MeshBuilder.CreateLines("springLine", {
points: [springAnchor.position, bob.position],
instance: springLine
});
});