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#

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#

ArgumentTypeDescription
body_a / body_bJoltBodyThe two connected managed bodies.
bodyJoltBodySingle body for jolt_hinge_world().
anchor_x / anchor_y / anchor_zRealShared world-space pivot point.
axis_x / axis_y / axis_zRealFinite non-zero world-space rotation axis; normalized internally.
optionsStruct or undefinedLimits, collision, initial state, and optional motor settings.

Options#

FieldDefaultDescription
limits_min-180Minimum angle in degrees relative to the reference orientation.
limits_max180Maximum angle in degrees; must be greater than or equal to limits_min.
collisiontrueWhether connected user bodies may collide; ignored by jolt_hinge_world().
enabledtrueInitial solver state.
motor_max_torque100Finite non-negative symmetric motor torque limit in N m.
motor_velocitynot setInitial target angular velocity in radians per second; selects velocity mode.
motor_positionnot setInitial target angle in degrees; selects position mode.

Methods#

MethodResultHinge behaviour
is_valid()BooleanChecks the managed wrapper and its native generational handle.
get_native_handle()RealReturns the opaque native handle, or 0 when invalid.
destroy()BooleanIdempotently releases only the constraint; connected bodies remain alive.
set_enabled(enabled) / get_enabled()BooleanTemporarily removes or restores solver participation without destroying the constraint.
set_collision_enabled(enabled) / get_collision_enabled()BooleanControls collision for a body pair. World variants always report false.
set_limits(minimum, maximum)BooleanUpdates ordered limits in degrees and wakes both bodies.
get_limits()StructReturns {minimum, maximum} in degrees.
set_rotation(angle)BooleanMoves the zero/reference orientation without moving either body or anchor.
get_angle()RealCurrent signed angle in degrees relative to the reference orientation.
set_motor_velocity(rad_per_second)BooleanSelects and enables velocity mode.
set_motor_position(degrees)BooleanSelects and enables position mode.
enable_motor(enabled)BooleanTurns the motor off or restores its last selected mode.
set_motor_max_torque(torque)BooleanSets the symmetric torque limit.
set_motor_spring(frequency, damping)BooleanTunes the native position-motor spring.
get_motor_state()StructReturns mode, targets, maximum torque and spring settings.
get_spring()StructReturns the position-motor frequency and damping.
get_impulse()RealLatest motor solver impulse, not torque per second.

Example: limited door#

GML
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#

GML
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#

GML
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 / ownerSignatureOperation contract
jolt_hingejolt_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_worldjolt_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_angleJoltConstraint.get_angle()Current Hinge/Cone angle in degrees; invalid/unsupported constraint or error returns 0 and records an error.
JoltConstraint.get_friction_torqueJoltConstraint.get_friction_torque()SwingTwist only; finite nonnegative friction torque in N·m. Setter Boolean, getter number or 0 on error.
JoltConstraint.set_friction_torqueJoltConstraint.set_friction_torque(torque)SwingTwist only; finite nonnegative friction torque in N·m. Setter Boolean, getter number or 0 on error.