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...
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...
#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
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.)
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
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
Here's some code to check for objects in range
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
Torque Owner Maddermadcat
Hope that helps. :)