ForEachEntityInAABBDo

This command calls the specified function for all entities in the world that intersect the specified AABB. The optional extra parameter will be passed to the callback function each time it is called.

Syntax

Parameters

Remarks

The callback function will only be called for the top-level entities who's recursive bounding boxes intersect the specified AABB. Your callback function should be written so that it is called recursively with the top-level entity's children.

Example

function Callback(entity,extra)

--We only want to execute this on models. If we don't check the class we will change the color of the light, too.
if (entity:GetClass()==Object.ModelClass) then

local v = Vec3()
v = extra
entity:SetColor(v.r,v.g,v.b)
end
end

--Create a window
window = Window:Create()
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
camera:Move(0,0,-6)
local light = DirectionalLight:Create()
light:SetRotation(35,35,0)

--Create some entities
local model = Model:Box()
model:SetColor(0.0,0.0,1.0)
model:SetPosition(-5,0,0)

model = Model:Box()
model:SetColor(0.0,0.0,1.0)
model:SetPosition(-2,1,0)

model = Model:Box()
model:SetColor(0.0,0.0,1.0)
model:SetPosition(5,0,0)

while true do
if window:Closed() or window:KeyHit(Key.Escape) then return false end

--Create an axis-aligned bounding box for testing. This will occupy an area 10x10x10 centered around the position -5,0,0.
--So it will intersect all entities on the left
local aabb = AABB(-10,-5,-5,0,5,5)

--We're going to pass a Vec3 to the callback in the extra parameter
local v = Vec3(1,0,0)

--Execute the callback for all entities that intersect the bounding box. The intersected entities will turn red.
world:ForEachEntityInAABBDo(aabb,"Callback",v)

Time:Update()
world:Update()
world:Render()
context:Sync(false)

end