Raycasting
Your First Raycast
Builds a safe interaction ray from initialization to consuming the result.
The managed API uses the default world created by jolt_start(). Supply a world-space origin and endpoint, perform the query, inspect the global error immediately, and only consume contact fields when hit is true.
1. Start physics#
if (!jolt_start()) {
var error = jolt_get_error();
show_error(error.context + ": " + error.message, true);
}2. Cast from an origin to an endpoint#
var from = { x: camera_x, y: camera_y, z: camera_z };
var to = {
x: from.x + aim_x * 20,
y: from.y + aim_y * 20,
z: from.z + aim_z * 20
};
var hit = jolt_raycast(
from.x, from.y, from.z,
to.x, to.y, to.z
);3. Distinguish an error, a miss and a hit#
var error = jolt_get_error();
if (error.code != JOLT_ERROR_NONE) {
show_debug_message(error.context + ": " + error.message);
}
else if (!hit.hit) {
// Successful query with a clear line.
}
else {
show_debug_message("Distance: " + string(hit.distance));
if (is_struct(hit.body)) {
hit.body.add_impulse(aim_x * 2, aim_y * 2, aim_z * 2);
}
}4. Ignore the casting body#
var hit = jolt_raycast(from.x, from.y, from.z, to.x, to.y, to.z, {
ignore: player_body
});ignore accepts one live JoltBody from the active default world. This prevents a camera or weapon ray from immediately hitting its owner.
Common first mistakes#
- Calling the query before jolt_start().
- Passing a direction instead of an endpoint.
- Using identical endpoints or non-finite coordinates.
- Reading position or normal without first testing hit.
- Assuming sensors or the casting body are excluded automatically.
- Using a thin ray where a swept volume is required.