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

#include "Leadwerks.h"

using namespace Leadwerks;

void Callback(Entity* entity, Object* 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)
{
Vec3* v = (Vec3*)extra;
entity->SetColor(v->r,v->g,v->b);
}
}

int main(int argc, const char *argv[])
{
Leadwerks::Window* window = Leadwerks::Window::Create();
Context* context = Context::Create(window);
World* world = World::Create();
Camera* camera = Camera::Create();
camera->Move(0,0,-6);
Light* light = DirectionalLight::Create();
light->SetRotation(35,35,0);

//Create some entities
Model* 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)
{
if (window->Closed() || window->KeyDown(Key::Escape)) return false;

//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
AABB aabb = AABB(-10,-5,-5,0,5,5);

//We're going to pass a Vec3 to the callback in the extra parameter
Vec3* v = new 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);

//Delete the Vec3 object
delete v;

Leadwerks::Time::Update();
world->Update();
world->Render();
context->Sync(false);

}
return 0;
}