Retrieve physical entities in a volume box

Overview

It's possible to query the physics system to get all the physical objects/entities contained into a specific box volume defined by a minimum and a maximum point.

For achieving this result you can use the function GetEntitiesInBox:

virtual int GetEntitiesInBox(Vec3 ptmin,Vec3 ptmax, IPhysicalEntity **&pList, int objtypes, int szListPrealloc=0) = 0;

... as follows:

IPhysicalEntity** entityList = 0;
int entityCount = gEnv->pPhysicalWorld->GetEntitiesInBox(m_volume.min, m_volume.max, entityList,
      ent_static | ent_terrain | ent_sleeping_rigid | ent_rigid);

The function GetEntitiesInBox returns the number of entities found and receives the following parameters:

Parameter

Description

ptmin

Minimum point in the space that defines the desired box volume.

ptmas

Maximum point in the space that defines the desired box volume.

pList

Pointer to a list of objects that will be filled by the function.

objtypes

Types of objects that need to be considered in the query.

szListPrealloc

If specified it's the maximum number of objects contained by the pList array.

The possible object types are described into the header physinterface.h in the entity_query_flags enumerators. Some of the possibilities are:

Entity type flag

Description

ent_static

Static entities

ent_terrain

Terrain

ent_sleeping_rigid

Sleeping rigid bodies

ent_rigid

Rigid bodies

...

Others

At this point you can easily iterate through the objects and perform the desired operations

for (int i = 0; i < entityCount; \++i)
{

   IPhysicalEntity\* entity = entityList[i];
   
   [...]
   
   if (entity->GetType() == PE_RIGID)
   {
      [...]
   }

   [...]

}