Finding objects in range
Over the course of a game we often need to know what objects are around a particular point in space. Some examples are to determine the number of bad guys immediately around the player, to find the number of zombies near a grenade explosion, or to discover the number of skeletons to throw back from a Gust of Wind spell. In this recipe, we will learn how to perform a radial search of Torque 3D's scene graph for objects of interest.
How to do it...
The following code snippet performs a search for all the ShapeBase
objects within five meters of a given position in the world:
echo("Starting search..."); %worldPos = "0 0 241"; initContainerRadiusSearch(%worldPos, 5, $TypeMasks::ShapeBaseObjectType); %obj = containerSearchNext(); while(%obj != 0) { echo("Found object: " @ %obj.getId()); %obj = containerSearchNext(); } echo("Finished search.");
The console output will look similar to the following screenshot:
How it works...
The initContainerRadiusSearch...