Game Development Community

Detect objects in range or target

by Hunter-Digital · in Torque Game Engine · 06/06/2008 (7:51 pm) · 9 replies

Hello again,

I have no ideea how to do this... what I wanna know is how to detect an object in a radius of the player, and if it's the closest, "select it", place it's name, id, or whatever, in a variable... so when the player makes an action (click, presses Use key, etc) that object is the target...

#1
06/07/2008 (9:59 am)
Quote:InitContainerRadiusSearch ( centerPos , radius , mask )
Purpose

Use the InitContainerRadiusSearch function to find all objects matching the specified mask type within a bounding radius centered at centerPos.

Syntax
centerPos - An XYZ vector specifying the world position of the search container's centroid.
radius - A floating-point value specifying the radius of the search container.
mask - A bitmask corresponding to the type of objects to check for. See the 'Object Types Table'.

Returns
No return value.

Notes
This search is static. That is it will find all objects within the specified radius and then the found objects can be retrieved with ContainerSearchNext. To find new objects, you will have to re-initialize the search.

Quote:ContainerSearchNext()

Purpose
Use the ContainerSearchNext function to find the next object in the currently defined search container.

Returns
Returns non-zero integer value equal to the ID of an object, or zero if no objects were found.

Notes
Caution! Do not call this function without first setting up a search container or the engine will crash.

Hope that helps. :)
#2
06/08/2008 (2:01 am)
Uhm... this is really complicated :)) any way to detect an object in the center of the screen ? Like a crosshair spot ? :)
#3
06/08/2008 (2:05 am)
Look at containerCastRay and you need to get the camera's transforms or the player's eyeVector.
#4
06/08/2008 (6:12 am)
The result may depend on whether you are running it on the server or a client. This is because they may have different lists of objects. For example, the server won't send objects to a client it thinks the client shouldn't be concerned with. Presumably, if you only allow close objects to be picked, then the server would have been sending it.

Be aware of this potential issue anytime you are doing something for display on the User Interface (because by definition, you are running it on the client.)
#5
06/08/2008 (9:30 am)
Well, I don't care about that because the game is singleplayer :)
#6
06/08/2008 (9:46 am)
Here's a code sample to use InitContainerRadiusSearch and check for an object called "doorKnob"

Here's the TDN on ray checks...
ContainerRayCast on TDN

...also look here for an example of ContainerRayCast
ContainerRayCast

...and this link should help (yes a lot of reading but worth learning)
Object picking

function rayCheck()
{
   %player = LocalClientConnection.getControlObject();
 
   if (%player.getClassName() $= "Player")
   {
      %eyeVec = %player.getEyeVector();
 
      %startPos = %player.getEyePoint();
      %endPos = VectorAdd(%startPos, VectorScale(%eyeVec, 1000));
 
      %target = ContainerRayCast(%startPos, %endPos, $TypeMasks::ItemObjectType);
 
      if (%target)
      {
         // you are now targeting some object - do a check here to see
         // what is it... ie look for a door knob
         if (%target.internalName $= "doorKnob")
         {
               turnDoorKnob();  // do something with it.
         }
      }
   }
}

Here's some code to check for objects in range

function scanForObjectByName(%name)
{
   // check for an entity based on the search params and return
   // 0 or the entity for later processing
   %position = "0 0 0"; // - could be player pos
   %radius = 10000;
   %type = $TypeMasks::ItemObjectType;

   // start search
   InitContainerRadiusSearch(%position, %radius, %type);
   
   // the line below means keep checking all objects until search is exhausted
   while ((%targetObject = containerSearchNext()) != 0)
   {
      if (%targetObject.internalName $= %name)
          {
             echo("Found something kinda wonderful");
             return %targetObject;
          }
   }
   echo("nothing found");
   return 0;
}
#7
06/08/2008 (7:45 pm)
Yeah, Andy, it's the mod forum, so scripts are fine. Source code isn't.
#8
06/09/2008 (1:06 am)
You should care about how things work in a client-server based engine like Torque. It will save you alot of headaches later.
#9
06/09/2008 (9:06 am)
See code above