Space Query

physics.space.cast_motion

Shared

Predicts how far a shape could travel along a motion vector before colliding, without actually moving anything

Syntax

local motion_result = physics.space.cast_motion(
    shape,
    position,
    motion,
    options = {}
)

Parameters

TypeNameDescription
tableshapeShape descriptor table
Refer Shape section
vector3positionWorld-space starting position of the shape as {x, y, z}
vector3motionMotion vector to test as {x, y, z}
tableoptionsOptional query options table
Refer Options section

Returns

TypeNameDescription
tablemotion_resultTable with safe and unsafe fractions of the motion that can be traveled, or false if the query failed

Shape

Shapes are described by a table, only the fields of the chosen type are read.

TypeKeyShapeDefaultDescription
stringtypeAll-Shape kind to build
• "box" - uses size
• "sphere" - uses radius
• "capsule" - uses radius and height
• "cylinder" - uses radius and height
Required, an unknown type makes the query return false
vector3sizebox{1, 1, 1}Full extents of the box as {x, y, z}
numberradiussphere, capsule, cylinder• sphere - 0.5
• capsule, cylinder - 0.4
Radius of the shape
numberheightcapsule, cylinder1.8Total height of the shape

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
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

Predict a capsule's movement
local motion_result = physics.space.cast_motion(
    { type = "capsule", radius = 0.4, height = 1.8 },
    position, {0, 0, -2}
)

core.engine.print("info", motion_result.safe)

On this page