Raycasting

Raycast Results

Explains the managed result fields and the safe distinction between a hit, a miss and an error.

A normal closest-hit query returns a complete managed struct. Early high-level validation failures can return a minimal {hit:false} value, so jolt_get_error() must be read immediately after the call before relying on optional result fields.

Closest-hit results now also carry ray:{world,origin,target} metadata on a valid hit or miss. Pass the complete fresh result to JoltRenderSurface.set_touch_raycast(), never result.hit. Contact fields still require hit=true; early validation failures may lack ray metadata.

Normal closest-hit fields#

FieldTypeMeaning
hitBooleantrue when a managed rigid body was intersected.
bodyJoltBody | undefinedResolved JoltBody, or undefined on a miss or failed mapping.
body_handleRealOpaque public generational body handle owned by the extension; not a Jolt BodyID.
entityAnyCurrent JoltBody.user_data value, or undefined.
positionStruct {x:Real, y:Real, z:Real}World-space public Z-up hit point {x,y,z}.
normalStruct {x:Real, y:Real, z:Real}World-space public Z-up surface normal {x,y,z}.
fractionRealNormalized position along the segment, normally 0 through 1.
distanceRealWorld-space distance from the origin.
error_codeRealNative result status for a normal low-level query.
error_messageStringNative message; empty on a valid hit or miss.

Authoritative state check#

GML
var result = jolt_raycast(x1, y1, z1, x2, y2, z2, filter);
var error = jolt_get_error(); // Read before another Jolt call.

if (error.code != JOLT_ERROR_NONE) {
    show_debug_message(error.context + ": " + error.message);
}
else if (!result.hit) {
    // Valid miss. Do not read contact fields as a hit.
}
else {
    var point = result.position;
    var normal = result.normal;
    var body = result.body;
    var data = result.entity;
}

Result states#

StateRecognition
Closest hitjolt_get_error().code is NONE and result.hit is true.
Closest missError code is NONE and result.hit is false.
Closest errorjolt_get_error().code is non-zero; result may contain only hit=false.
All-hit successError code is NONE; the array may contain zero or more hits.
All-hit errorError code is non-zero; the returned array is empty.

Fraction and distance#

For the endpoint form, maximum distance is the length from the origin to the endpoint. distance equals fraction multiplied by that length. position is already the world-space point, so it normally does not need to be reconstructed.

Body, handle and user data#

The native body stores the extension's public handle in its user-data slot. The wrapper validates that handle against the managed registry, exposes it as body_handle, resolves body to JoltBody and then copies JoltBody.user_data into entity. entity is caller-owned and is not guaranteed to be a GameMaker instance.

Fields that are not exposed#

  • Jolt BodyID
  • SubShapeID or compound child
  • Triangle or face index
  • Physics material
  • Motion type, layer or sensor flag

Low-level result shape#

GML
{
    hit, error_code, error_message, body,
    x, y, z,
    normal_x, normal_y, normal_z,
    fraction, distance,
    position, normal
}

Low-level body is the numeric public extension handle. The managed form resolves it to JoltBody and preserves the number as body_handle.