Impact Sensor
Detect a new rigid-body impact for sounds, particles or approximate gameplay damage.
Impact Sensor is an opt-in JoltBody feature. It stores the latest relevant impact snapshot; it is not a trigger, a collision event queue or a continuous force meter.
Create and enable#
// Create event, after the central controller has called jolt_start().
last_impact_sequence = 0;
body = jolt_box_create_dynamic(0, 0, 5, 1, 1, 1, {
mass: 2,
impact_sensor: {min_value: 10}
});
if (is_undefined(body)) show_debug_message(jolt_get_error_message());| API | Contract |
|---|---|
| impact_sensor: true | Enables recording with min_value 0. |
| impact_sensor: {min_value: value} | Enables recording with a nonnegative finite threshold in N*s. |
| body.set_impact_sensor(enabled, min_value=0) | Boolean enabled and nonnegative finite threshold. Returns true on success, false on failure. |
| body.get_impact_sensor() | Reads a snapshot without consuming it. On failure returns an empty snapshot and sets the managed error. |
What counts as an impact?#
Only a new non-sensor OnContactAdded contact is considered. Closing speed at the contact must be at least 0.05 world units per second and EstimateCollisionResponse must predict a positive normal impulse. min_value filters the sum of normal contact impulses, measured in N*s.
OnContactPersisted does not overwrite the snapshot: resting and sliding on an existing contact do not keep retriggering it. If several candidates occur in one native physics step, the strongest is stored and sequence increments once. One jolt_update() may execute several steps; the retained snapshot is the latest committed impact, not necessarily the strongest over the whole update.
Result fields#
| API | Contract |
|---|---|
| hit | Whether a relevant impact has ever been stored since reset. |
| value / impulse | Estimated normal impulse in N*s; impulse aliases value. Not force in newtons. |
| normal_speed | Closing speed at the contact in world units/s. |
| position | World-space {x,y,z} of the strongest contact point. |
| normal | World-space normal from the monitored body towards the other body. |
| other_handle | Historical generation-safe native identity of the other rigid body. |
| other_body / other_user_data | Currently live managed body and its current GML user_data, or undefined if unavailable/destroyed. |
| sequence | Monotonic within an enabled recording session; compare with the last processed value. |
React once after physics updates#
// After the central jolt_update(); do not add a second physics update here.
if (!is_undefined(body) && body.is_valid()) {
var impact = body.get_impact_sensor();
if (jolt_has_error()) {
show_debug_message(jolt_get_error_message());
} else if (impact.hit && impact.sequence != last_impact_sequence) {
last_impact_sequence = impact.sequence;
show_debug_message("Impact: " + string(impact.value) + " N*s");
// Play one sound or spawn one particle effect here.
}
}Reset and lifetime#
Before the first impact, hit is false, sequence is zero and numeric fields are zero. The snapshot persists until replaced or reset. Changing enabled from true to false or false to true clears it. Calling set_impact_sensor(true, threshold) while already enabled only changes the threshold; it does not clear the stored impact.
// Explicitly begin a new recording session on a validated body.
if (body.set_impact_sensor(false) && body.set_impact_sensor(true, 10)) {
last_impact_sequence = 0;
} else show_debug_message(jolt_get_error_message());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 |
|---|---|---|
| JoltBody.get_impact_sensor | JoltBody.get_impact_sensor() | Snapshot hit, value, impulse, normal_speed, position, normal, other_handle, other_body, other_user_data, sequence. No hit/error returns an empty hit=false snapshot; position m, normal_speed m/s. Not scalar get_impact. |
| JoltBody.set_impact_sensor | JoltBody.set_impact_sensor(enabled, min_value = 0.0) | Boolean enabled, nonnegative min_value default 0; Boolean result. Event impact sensor is separate from solver-based get_impact. |