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#
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#
| Field | Default | Description |
|---|---|---|
| point_a | body_a position | World-space attachment point on body_a at creation. |
| point_b | body_b position | World-space attachment point on body_b at creation. |
| min_distance | current point distance | Minimum permitted distance in world units. |
| max_distance | current point distance | Maximum permitted distance in world units. |
| spring_frequency | not applied | When present, enables native frequency/damping spring behaviour. |
| spring_damping | 1 | Damping paired with spring_frequency. |
| collision | true | Whether connected user bodies may collide. |
| enabled | true | Initial solver state. |
World-variant arguments#
| Argument | Meaning |
|---|---|
| body | Live managed body attached to the fixed world. |
| anchor_x / anchor_y / anchor_z | Fixed world-space point. |
| min_distance / max_distance | Explicit ordered length range in world units. |
| options | enabled plus optional spring_frequency and spring_damping. |
Methods#
| Method | Result | Distance 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 the non-negative ordered distance range. |
| get_limits() | Struct | Returns {minimum, maximum} in world units. |
| set_spring(frequency, damping) | Boolean | Sets non-negative native spring frequency and damping. |
| get_spring() | Struct | Returns {frequency, damping}. |
| get_current_distance() | Real | Current geometric separation between the two attachment points. |
| get_impulse() | Real | Latest 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#
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#
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 / owner | Signature | Operation contract |
|---|---|---|
| jolt_distance | jolt_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_world | jolt_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_distance | JoltConstraint.get_current_distance() | Distance only: current anchor distance in m; 0 on error. |
| JoltConstraint.get_limit_spring | JoltConstraint.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_spring | JoltConstraint.get_spring() | Distance/Hinge/Slider/Path: {frequency, damping}, Hz and ratio; undefined on error or other type. |
| JoltConstraint.set_limit_spring | JoltConstraint.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_spring | JoltConstraint.set_spring(frequency, damping) | Distance limit spring or Slider motor spring: finite frequency ≥0 Hz, damping ≥0 (ratio). Boolean. |