Constraints
Hinge Constraint
Keeps a shared anchor together and permits relative rotation around one world-space axis.
Use a Hinge for doors, levers, wheels and powered pivots. It provides angular limits in degrees, a changeable reference rotation, current-angle state, and torque-limited velocity or position motors.
Syntax#
jolt_hinge(body_a, body_b, anchor_x, anchor_y, anchor_z, axis_x, axis_y, axis_z, options=undefined)
jolt_hinge_world(body, anchor_x, anchor_y, anchor_z, axis_x, axis_y, axis_z, options=undefined)Arguments#
| Argument | Type | Description |
|---|---|---|
| body_a / body_b | JoltBody | The two connected managed bodies. |
| body | JoltBody | Single body for jolt_hinge_world(). |
| anchor_x / anchor_y / anchor_z | Real | Shared world-space pivot point. |
| axis_x / axis_y / axis_z | Real | Finite non-zero world-space rotation axis; normalized internally. |
| options | Struct or undefined | Limits, collision, initial state, and optional motor settings. |
Options#
| Field | Default | Description |
|---|---|---|
| limits_min | -180 | Minimum angle in degrees relative to the reference orientation. |
| limits_max | 180 | Maximum angle in degrees; must be greater than or equal to limits_min. |
| collision | true | Whether connected user bodies may collide; ignored by jolt_hinge_world(). |
| enabled | true | Initial solver state. |
| motor_max_torque | 100 | Finite non-negative symmetric motor torque limit in N m. |
| motor_velocity | not set | Initial target angular velocity in radians per second; selects velocity mode. |
| motor_position | not set | Initial target angle in degrees; selects position mode. |
Methods#
| Method | Result | Hinge behaviour |
|---|---|---|
| is_valid() | Boolean | Checks the managed wrapper and its native generational handle. |
| get_native_handle() | Real | Returns the opaque native handle, or 0 when invalid. |
| destroy() | Boolean | Idempotently releases only the constraint; connected bodies remain alive. |
| set_enabled(enabled) / get_enabled() | Boolean | Temporarily removes or restores solver participation without destroying the constraint. |
| set_collision_enabled(enabled) / get_collision_enabled() | Boolean | Controls collision for a body pair. World variants always report false. |
| set_limits(minimum, maximum) | Boolean | Updates ordered limits in degrees and wakes both bodies. |
| get_limits() | Struct | Returns {minimum, maximum} in degrees. |
| set_rotation(angle) | Boolean | Moves the zero/reference orientation without moving either body or anchor. |
| get_angle() | Real | Current signed angle in degrees relative to the reference orientation. |
| set_motor_velocity(rad_per_second) | Boolean | Selects and enables velocity mode. |
| set_motor_position(degrees) | Boolean | Selects and enables position mode. |
| enable_motor(enabled) | Boolean | Turns the motor off or restores its last selected mode. |
| set_motor_max_torque(torque) | Boolean | Sets the symmetric torque limit. |
| set_motor_spring(frequency, damping) | Boolean | Tunes the native position-motor spring. |
| get_motor_state() | Struct | Returns mode, targets, maximum torque and spring settings. |
| get_spring() | Struct | Returns the position-motor frequency and damping. |
| get_impulse() | Real | Latest motor solver impulse, not torque per second. |
Example: limited door#
var frame = jolt_box_create_static(-1.8, 0, 2, 0.3, 0.3, 4);
var door = jolt_box_create_dynamic(0, 0, 2, 3.6, 0.25, 4, { mass: 18 });
var hinge = jolt_hinge(frame, door, -1.8, 0, 2, 0, 0, 1, {
limits_min: 0,
limits_max: 110,
collision: false
});Example: position and velocity motors#
hinge.set_motor_max_torque(45);
hinge.set_motor_spring(3, 0.8);
hinge.set_motor_position(90); // degrees
// Later, switch to continuous velocity mode.
hinge.set_motor_velocity(4); // radians per second
var angle = hinge.get_angle();
var state = hinge.get_motor_state();Reference rotation#
set_rotation(angle) redefines the Hinge zero angle while preserving its handle, anchor, limits, collision state and motor settings. Existing limits are then measured from the new reference.
World variant#
var lever = jolt_box_create_dynamic(0, 0, 2, 4, 0.3, 0.3, { mass: 6 });
var pivot = jolt_hinge_world(lever, -2, 0, 2, 0, 1, 0, {
limits_min: -35,
limits_max: 35
});Errors and lifecycle#
- Body-to-body creators require two different live managed JoltBody values in the same managed default world.
- At least one connected body must be movable; native Jolt rejects an unsupported static-static pair.
- Constructors return a managed JoltConstraint on success or undefined after a validation/native creation failure.
- Destroying either attached body automatically destroys and invalidates the constraint before releasing that body.
- Read jolt_get_error() after a failed creator or method when the values may be untrusted.
- Anchor and axis components must be finite and the axis must have non-zero length.
- Limits must be finite and ordered; motor torque must be finite and non-negative.
- If both motor_velocity and motor_position are supplied, position is applied last and becomes the active mode.
Verified operation contracts#
These contracts distinguish units, return values, errors and lifetime according to the current implementation. Check results: false, 0 and undefined can mean different things for different methods.
| Symbol / owner | Signature | Operation contract |
|---|---|---|
| jolt_hinge | jolt_hinge(body_a, body_b, anchor_x, anchor_y, anchor_z, axis_x, axis_y, axis_z, options = undefined) | Returns JoltConstraint/undefined, does not own bodies. Two distinct live same-world bodies; _world variant one Dynamic body and fixed world. Enabled defaults true; ordinary two-body collision defaults true unless specified otherwise below. Also check jolt_has_error after creation: some subsequent options can fail while returning the created constraint. World anchor/nonzero axis; limits_min/max default-180/180°, min[-180, 0], max[0, 180]. |
| jolt_hinge_world | jolt_hinge_world(body, anchor_x, anchor_y, anchor_z, axis_x, axis_y, axis_z, options = undefined) | Returns JoltConstraint/undefined, does not own bodies. Two distinct live same-world bodies; _world variant one Dynamic body and fixed world. Enabled defaults true; ordinary two-body collision defaults true unless specified otherwise below. Also check jolt_has_error after creation: some subsequent options can fail while returning the created constraint. World anchor/nonzero axis; limits_min/max default-180/180°, min[-180, 0], max[0, 180]. |
| JoltConstraint.get_angle | JoltConstraint.get_angle() | Current Hinge/Cone angle in degrees; invalid/unsupported constraint or error returns 0 and records an error. |
| JoltConstraint.get_friction_torque | JoltConstraint.get_friction_torque() | SwingTwist only; finite nonnegative friction torque in N·m. Setter Boolean, getter number or 0 on error. |
| JoltConstraint.set_friction_torque | JoltConstraint.set_friction_torque(torque) | SwingTwist only; finite nonnegative friction torque in N·m. Setter Boolean, getter number or 0 on error. |