Constraints

Distance Constraint

Keeps two attachment points within a configurable minimum and maximum separation.

Distance constraints create rigid rods, ropes with slack, or damped springs without rendering a rope. Body-to-body attachment points are supplied in world space at creation and stored relative to each body internally.

Syntax#

Syntax
jolt_distance(body_a, body_b, options=undefined)
jolt_distance_world(body, anchor_x, anchor_y, anchor_z, min_distance, max_distance, options=undefined)

Body-to-body options#

FieldDefaultDescription
point_abody_a positionWorld-space attachment point on body_a at creation.
point_bbody_b positionWorld-space attachment point on body_b at creation.
min_distancecurrent point distanceMinimum permitted distance in world units.
max_distancecurrent point distanceMaximum permitted distance in world units.
spring_frequencynot appliedWhen present, enables native frequency/damping spring behaviour.
spring_damping1Damping paired with spring_frequency.
collisiontrueWhether connected user bodies may collide.
enabledtrueInitial solver state.

World-variant arguments#

ArgumentMeaning
bodyLive managed body attached to the fixed world.
anchor_x / anchor_y / anchor_zFixed world-space point.
min_distance / max_distanceExplicit ordered length range in world units.
optionsenabled plus optional spring_frequency and spring_damping.

Methods#

MethodResultDistance 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 the non-negative ordered distance range.
get_limits()StructReturns {minimum, maximum} in world units.
set_spring(frequency, damping)BooleanSets non-negative native spring frequency and damping.
get_spring()StructReturns {frequency, damping}.
get_current_distance()RealCurrent geometric separation between the two attachment points.
get_impulse()RealLatest signed solver impulse along the distance axis.

Rigid, slack and spring behaviour#

  • Equal minimum and maximum values create a fixed-length rod or tether.
  • A range permits free motion until an attachment point reaches either boundary.
  • A spring softens correction toward the permitted range; higher frequency is stiffer and damping suppresses oscillation.
  • The constraint is mathematical geometry only: it has no rope collision, bending, wrapping or rendering.

Example: damped tether#

GML
var anchor = jolt_box_create_static(0, 0, 6, 0.4, 0.4, 0.4);
var load   = jolt_box_create_dynamic(0, 0, 2, 1, 1, 1, { mass: 5 });

var tether = jolt_distance(anchor, load, {
    point_a: { x: 0, y: 0, z: 6 },
    point_b: { x: 0, y: 0, z: 2 },
    min_distance: 2,
    max_distance: 4,
    spring_frequency: 3,
    spring_damping: 0.65,
    collision: false
});

Example: attached directly to world#

GML
var load = jolt_box_create_dynamic(0, 0, 2, 1, 1, 1, { mass: 5 });
var tether = jolt_distance_world(load, 0, 0, 6, 2, 4, {
    spring_frequency: 2,
    spring_damping: 0.7
});

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.
  • Attachment structs must contain finite x, y and z values.
  • Distance limits must be finite, non-negative and ordered minimum <= maximum.
  • Frequency and damping must be finite and non-negative.

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_distancejolt_distance(body_a, body_b, options = undefined)Two bodies; world-space point_a/b default body origins, min/max_distance default current separation m, 0<=min<=max. Optional spring_frequency Hz/damping1. Constraint/undefined; check subsequent options errors.
jolt_distance_worldjolt_distance_world(body, anchor_x, anchor_y, anchor_z, min_distance, max_distance, options = undefined)Dynamic Body and world XYZ anchor m + min/max length m; second point is body origin. Constraint/undefined, enabled true; optional spring_frequency/damping1.
JoltConstraint.get_current_distanceJoltConstraint.get_current_distance()Distance only: current anchor distance in m; 0 on error.
JoltConstraint.get_limit_springJoltConstraint.get_limit_spring(axis)SixDOF: {frequency, damping}, Hz and ratio. Motor axes 0..5, limit only 0..2; on error components may contain native fallback 0.
JoltConstraint.get_springJoltConstraint.get_spring()Distance/Hinge/Slider/Path: {frequency, damping}, Hz and ratio; undefined on error or other type.
JoltConstraint.set_limit_springJoltConstraint.set_limit_spring(axis,frequency,damping)SixDOF translation axes 0..2 only; finite frequency ≥0 Hz and damping ≥0 as ratio. Rotation axes are rejected, Boolean result.
JoltConstraint.set_springJoltConstraint.set_spring(frequency, damping)Distance limit spring or Slider motor spring: finite frequency ≥0 Hz, damping ≥0 (ratio). Boolean.