Pick
This function performs a pick operation on a single entity.
Syntax
- bool Pick(Vec3 p0, Vec3 p1, Pick pick, number radius = 0.0, bool closest=false, bool recursive=false, number collisiontype=0)
Parameters
- p0: the origin of the ray in global coordinates.
- p1: the terminal end of the ray in global coordinates.
- pick: a pick object to contain pick information.
- radius: the radius of the ray. If the radius is greater than 0.0, a slower swept sphere collision will be performed.
- closest: if set to true, the closest intersection will be found, otherwise the operation will return as soon as a single intersection is found.
- recursive: if set to true, the entity's sub-hierarchy will be tested recursively.
- collisiontype: if specified, the entity collision type of all children will be tested against this, and non-colliding entities will be skipped.
Returns
Returns true if anything is hit, otherwise false is returned.
Remarks
This function should only be used in special cases. A World::Pick or Camera::Pick function call will be much faster in most cases, as it first performs an AABB intersection test with the ray.
Example
--Create a window
window = Window:Create()
context = Context:Create(window)
world = World:Create()
local camera = Camera:Create()
camera:SetRotation(35,0,0)
camera:Move(0,0,-8)
local light = DirectionalLight:Create()
light:SetRotation(35,35,0)
camera:SetProjectionMode(2)
camera:SetZoom(50)
--Create a model
local spherescale = 3.6
local model = Model:Sphere()
model:SetPickMode(Entity.SpherePick)
model:SetPickRadius(spherescale/2.0)
model:SetScale(model:GetPickRadius()*2.0)
--Create a sphere to indicate where the pick hits
local picksphere = Model:Sphere()
picksphere:SetColor(1.0,0.0,0.0)
picksphere:SetPickMode(0)
local pickradius = 0.25
picksphere:SetScale(pickradius*2.0)
local pickinfo = PickInfo()
if (world:Pick(Vec3(-10,1,0),Vec3(10,1,0),pickinfo,pickradius,true)) then
picksphere:SetPosition(pickinfo.position)
else
picksphere:Release()
end
while true do
if window:Closed() or window:KeyHit(Key.Escape) then return false end
Time:Update()
world:Update()
world:Render()
context:Sync()
end