Ray

A ray has one origin point in the 3D scene and a direction. It returns potentials Impacts and the distance from the origin when that happens.

Constructors

Ray ( Number3 origin, Number3 direction )

Creates a Ray with an origin and a direction.

local ray = Ray(Camera.Position, Camera.Forward)
local impact = ray:Cast()

Functions

Impact Cast ( Shape filterIn )
Impact Cast ( nil filterIn, Object filterOut )
Impact Cast ( CollisionGroups filterIn, Object filterOut )
Impact Cast ( Shape filterIn, Object filterOut )

Casts a ray and returns an Impact (can be nil).

The Impact contains information about what's been hit.

By default, a ray collides with all CollisionGroups. But filters can be set, if you want your ray to only collide with the Map for example. You can also filter out an Object of any kind.

Pointer.Down = function( pointerEvent )
    local ray = Ray(pointerEvent.Position, pointerEvent.Direction)
    local impact = ray:Cast()
    if impact.Block ~= nil then
      print("block hit:", impact.Block)
    end
end

-- cast rays from Camera's position to remove cubes in the map
Client.Action2 = function()
    local ray = Ray(Camera.Position, Camera.Forward)
    local impact = ray:Cast(Map.CollisionGroups) -- only consider the map for collisions
    if impact.Block ~= nil then
      impact.Block:Remove()
    end
end

-- cast ray down from Player's position to see
-- if there's something under it:
Client.Action3 = function()
    local ray = Ray(Player.Position, {0, -1, 0})
    local impact = ray:Cast(nil, Player) -- filter out Player to avoid direct impacts with it
    if impact ~= nil then
        print("found something under the player, distance:", impact.Distance)
    end
end
-- When a single Shape is provided
-- the Shape's internal block touched is provided
-- as part of the Impact object
Client.Action2 = function()
    local ray = Ray(Camera.Position, Camera.Forward)
    local impact = ray:Cast(myShape) -- only consider a Shape for collisions
    -- impact.Block is Number3(0, 5, 12)
    -- impact.Distance is 42,72234
    -- impact.Object is the `myShape` Shape
end

Properties

The direction of the ray, in world coordinate system.

Defines items colliding with the ray. nil by default, meaning the ray collides with everything.

nil by default. Can be set to an Object to filter it out from possible collisions.

The origin of the ray, in world coordinate system.