Ragdolls, Joints and Constraints

Updated June 2026
Constraints are the rules that connect two physics bodies and limit their relative movement. They are the foundation of ragdoll characters, mechanical joints, swinging ropes, breakable structures, and vehicle suspensions. This guide covers every major constraint type, walks through building a ragdoll from scratch, and shows how to use constraints for common gameplay mechanics in browser games.

Every physics engine covered in this pillar, Rapier, Cannon-es, and Havok, supports constraints with different API names but identical concepts. The ideas in this guide apply to all three. Where API differences matter, the engine-specific pages have the exact code.

Understand Constraint Types

Five constraint types appear in virtually every physics engine. A ball-and-socket (also called point-to-point) constraint pins two bodies at a shared point and allows free rotation in all directions. Think of a shoulder joint or a wrecking ball hanging from a crane. The bodies can swing, spin, and twist around the pivot but cannot separate from it.

A hinge constraint allows rotation around exactly one axis. A door on its frame, an elbow, and a wheel on an axle are all hinge joints. You define the pivot point and the hinge axis on each body. The constraint ensures the pivot points stay coincident and only rotation around the specified axis is permitted. Hinge constraints often include angular limits (the door can open between 0 and 120 degrees) and motors (the door swings shut automatically with a certain torque).

A slider (prismatic) constraint allows translation along one axis but no rotation. An elevator, a sliding door, and a piston are slider joints. You define the axis of motion and optional distance limits. Some engines support motors on sliders to drive automatic back-and-forth motion.

A lock (fixed, weld) constraint removes all relative motion, effectively gluing two bodies together. Use this for compound objects that should behave as a single rigid body but need to be separated later, like a destructible wall made of bricks where each brick-to-brick connection is a lock constraint that breaks under sufficient force.

A spring constraint applies a restorative force proportional to the distance between two anchor points on the bodies. When the bodies are pulled apart beyond the rest length, the spring pulls them back. When compressed below the rest length, it pushes them apart. Stiffness controls how strong the force is, and damping controls how quickly oscillations decay. Springs are used for suspension, bouncy connections, and soft bridges.

Design the Ragdoll Skeleton

A ragdoll maps a character's anatomical structure to a set of rigid bodies connected by constrained joints. The typical human ragdoll has 10 to 15 segments: head, upper torso, lower torso (or a single torso), left upper arm, left lower arm, right upper arm, right lower arm, left upper leg, left lower leg, right upper leg, right lower leg, and optionally hands and feet.

Each segment is a rigid body with a collision shape that roughly matches the character's visual proportions. The head is a sphere. The torso is a box or capsule. Arms and legs are capsules or thin boxes. The shapes do not need to match the visual mesh exactly. They need to be close enough that collisions look natural when the ragdoll tumbles across the scene.

Mass distribution matters for realistic ragdoll behavior. The torso should be the heaviest part (around 40% of total mass), followed by the upper legs (about 10% each), head (about 8%), and arms (about 5% each). Getting this roughly right ensures the ragdoll falls with the torso leading, not the head or feet, which would look unnatural.

Map the skeleton before writing code. Draw a simple diagram with boxes for each body segment, circles for each joint, and notes about which constraint type and angular limits each joint needs. This planning step saves significant debugging time because joint misconfigurations produce bizarre, contorted poses that are hard to trace back to the wrong parameter.

Build the Body Chain

Start with the torso as the root body. Create it as a dynamic body with a box or capsule shape at the center of the character. Then create each connected segment (head, upper arms, upper legs) as separate dynamic bodies positioned relative to the torso. Connect each segment to the torso with the appropriate constraint.

The head connects to the torso via a ball-and-socket joint at the neck position. The pivot on the torso is at the top center. The pivot on the head is at the bottom center. This allows the head to tilt, nod, and turn. Add cone limits to prevent the head from rotating more than about 45 degrees in any direction.

Upper arms connect to the torso via ball-and-socket joints at the shoulders. The pivot on the torso is at the shoulder position. Use cone limits to restrict the arm's range of motion to roughly a hemisphere in front and to the sides. Lower arms connect to upper arms via hinge joints at the elbows, with limits of 0 to approximately 150 degrees (the elbow bends forward but not backward).

Upper legs connect to the lower torso via ball-and-socket joints at the hips. Limit the cone angle so legs cannot extend backward past about 30 degrees or outward past about 60 degrees. Lower legs connect to upper legs via hinge joints at the knees, with limits of 0 to about 160 degrees (the knee bends backward but not forward).

Once all segments are created and connected, verify the ragdoll by spawning it in mid-air and letting it fall. It should collapse naturally without any limbs penetrating the torso or bending in impossible directions. If joints look wrong, adjust the pivot positions and angular limits until the pose is anatomically plausible.

Set Angular Limits for Realistic Poses

Angular limits prevent joints from reaching unnatural angles. Without them, a ragdoll's elbow can bend backward, its head can rotate 360 degrees, and its legs can twist into a pretzel. Setting correct limits is the difference between a ragdoll that looks believable and one that looks broken.

For ball-and-socket joints, limits are typically expressed as a cone angle (maximum tilt from the rest direction) and optionally a twist range (rotation around the limb's long axis). A shoulder might have a cone angle of 90 degrees (arm can swing freely in a forward hemisphere) and a twist range of 90 degrees (the arm can rotate along its length by a quarter turn).

For hinge joints, limits are a minimum and maximum angle. An elbow has a minimum of 0 degrees (fully extended) and a maximum of about 145 degrees (fully bent). A knee has a minimum of 0 (extended) and a maximum of about 160 degrees (bent). A door might range from 0 to 90 degrees.

Start with conservative limits (smaller ranges) and widen them if the ragdoll looks too stiff. Overly tight limits make the ragdoll appear rigid and robotic when it falls. Overly loose limits produce grotesque poses. Human joints have soft limits in reality, meaning they resist more as they approach their extremes. Some engines let you model this with spring-like limit softness, adding cushioning at the edges of the range.

Transition Between Animation and Ragdoll

In most games, characters are animated with keyframe animation while alive and switch to ragdoll physics on death or knockback. The transition needs to be smooth, or the character will visibly snap from its animated pose to its ragdoll pose.

The standard approach is to maintain both systems simultaneously. While the character is alive, the ragdoll bodies exist but are set to kinematic mode. Each frame, you read the animated bone positions from the skeleton and write them to the kinematic bodies. The bodies track the animation perfectly because kinematic bodies go wherever you tell them.

When the character dies or is knocked back, switch every body from kinematic to dynamic. Crucially, at the moment of switching, read the current linear and angular velocities from the animation (computed from the position change between the last two frames) and set them on the dynamic bodies. This velocity matching ensures the ragdoll continues the character's motion rather than going limp from a dead stop. If the character was running when it died, the ragdoll should stumble forward with momentum, not drop straight down.

Some games blend between animation and ragdoll by keeping certain bodies kinematic (the torso, for example) while releasing others (the limbs). This produces a partial ragdoll effect where the character staggers but maintains some control, useful for hit reactions or stun effects that do not kill the character.

Build Gameplay Mechanics with Constraints

Constraints power more than just ragdolls. A grappling hook is a distance constraint between the player body and an anchor point on the environment. When the player fires the hook, cast a ray from the player to the target. If it hits, create a distance constraint between the player body and a static body at the hit point. The rest length equals the current distance. The player swings like a pendulum. To reel in, reduce the rest length over time. To release, remove the constraint.

A chain or rope is a series of small bodies connected by ball-and-socket constraints. Each link is a capsule or sphere with a ball-and-socket joint to the next link. The first link attaches to a static anchor, and the last link attaches to a dynamic payload. The chain swings and drapes naturally under gravity. Performance scales with the number of links, so keep chains short (10 to 20 links) or use simplified rope physics (Verlet integration on a particle chain) for longer ropes.

Breakable structures use lock constraints between adjacent bodies with a force threshold. When the solver applies a force exceeding the threshold to maintain the constraint, remove the constraint and let the pieces scatter. A wall might be a grid of box bodies, each locked to its neighbors. An explosion applies an impulse at the center, and the constraints nearest the blast exceed their threshold and break, while distant constraints hold. This creates a localized destruction effect.

Swinging platforms use hinge or ball-and-socket constraints with motors. The motor applies periodic torque to swing the platform back and forth on a fixed cycle. The player stands on the platform (which is kinematic or has high mass so the player's weight does not stop it) and rides the motion. Timing jumps between swinging platforms is a classic platformer mechanic that constraints make easy to implement.

Key Takeaway

Constraints turn individual physics bodies into connected systems: ragdolls, machines, ropes, and breakable structures. The five core types (ball-and-socket, hinge, slider, lock, spring) cover virtually every gameplay need. For ragdolls, the key is correct joint placement, anatomically plausible angular limits, and smooth velocity-matched transitions from animation to physics.