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
x1numberThe x coordinate of the starting position of the ray.
y1numberThe y coordinate of the starting position of the ray.
z1numberThe z coordinate of the starting position of the ray.
x2numberThe x coordinate of the ending position of the ray.
y2numberThe y coordinate of the ending position of the ray.
z2numberThe z coordinate of the ending position of the ray.
callbackfunctionThe function to call when an intersection is detected.

Returns

Nothing

Notes

The callback is passed the shape that was hit, the hit position, 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