World:raycast

Casts a ray through the World, calling a function every time the ray intersects with a Shape.

World:raycast(x1, y1, z1, x2, y2, z2, callback)

Arguments

NameTypeDescription
x1number The x coordinate of the starting position of the ray.
y1number The y coordinate of the starting position of the ray.
z1number The z coordinate of the starting position of the ray.
x2number The x coordinate of the ending position of the ray.
y2number The y coordinate of the ending position of the ray.
z2number The z coordinate of the ending position of the ray.
callbackfunction The function to call when an intersection is detected.

Returns

Nothing

Notes

The callback is passed the shape that was hit, the hit position (in world coordinates), and the normal vector of the hit.

Example

function lovr.load()
  world = lovr.physics.newWorld()
  world:newSphereCollider(0, 0, 0, 2)

  -- Cast a ray through the sphere
  local x1, y1, z1 = .5, 3, 0
  local x2, y2, z2 = -.5, -2, 0
  world:raycast(x1, y1, z1, x2, y2, z2, function(shape, x, y, z, nx, ny, nz)
    print('Collision detected!', shape, x, y, z, nx, ny, nz)
  end)
end

See also