Space Query

physics.space.raycast

Shared

Casts a ray through the physics world and returns the first collision encountered, if any

Syntax

local hit = physics.space.raycast(
    from,
    to,
    options = {}
)

Parameters

TypeNameDescription
vector3fromWorld-space start position of the ray as {x, y, z}
vector3toWorld-space end position of the ray as {x, y, z}
tableoptionsOptional query options table
Refer Options section

Returns

TypeNameDescription
tablehitTable with position, normal, and collider fields, or false if nothing was hit

Options

All fields are optional and fall back to their defaults if not specified.

TypeKeyDefaultSideDescription
intmask4294967295SharedCollision layers to test against, as a bitmask
The default matches every layer
tableexclude{}SharedArray of entities to ignore
Only physics bodies and areas are accepted, any other value is skipped
boolcollide_bodiestrueShared• When true - physics bodies are tested
• When false - physics bodies are ignored
boolcollide_areasfalseShared• When true - areas are tested
• When false - areas are ignored
boolhit_from_insidefalseShared• When true - a ray starting inside a collider reports a hit
• When false - colliders the ray starts inside of are ignored
boolhit_back_facestrueShared• When true - back faces of concave shapes can be hit
• When false - only front faces are hit
intdimension0SharedDimension whose physics world is queried, from 0 to 65535
• Anything outside this range raises an error
• A dimension with nothing in it simply finds nothing

Examples

Raycast forward from a position
local hit = physics.space.raycast({0, 2, 0}, {0, 2, -10}, {
    mask = 1,
    collide_bodies = true
})

if hit then
    core.engine.print("info", hit.position)
end

On this page