Space Query
physics.space.raycast
SharedCasts a ray through the physics world and returns the first collision encountered, if any
Syntax
local hit = physics.space.raycast(
from,
to,
options = {}
)Parameters
| Type | Name | Description |
|---|---|---|
vector3 | from | World-space start position of the ray as {x, y, z} |
vector3 | to | World-space end position of the ray as {x, y, z} |
table | options | Optional query options table Refer Options section |
Returns
| Type | Name | Description |
|---|---|---|
table | hit | Table 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.
| Type | Key | Default | Side | Description |
|---|---|---|---|---|
int | mask | 4294967295 | Shared | Collision layers to test against, as a bitmask The default matches every layer |
table | exclude | {} | Shared | Array of entities to ignore Only physics bodies and areas are accepted, any other value is skipped |
bool | collide_bodies | true | Shared | • When true - physics bodies are tested• When false - physics bodies are ignored |
bool | collide_areas | false | Shared | • When true - areas are tested• When false - areas are ignored |
bool | hit_from_inside | false | Shared | • When true - a ray starting inside a collider reports a hit• When false - colliders the ray starts inside of are ignored |
bool | hit_back_faces | true | Shared | • When true - back faces of concave shapes can be hit• When false - only front faces are hit |
int | dimension | 0 | Shared | Dimension 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
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