GetForwardVector
by Brandon Fogerty · in Technical Issues · 07/07/2007 (3:20 pm) · 8 replies
Hi. I am trying to do is if there is an ai bot with id "1441" 5 units in front of me, I want "YAY!" to be printed on the screen. Otherwise I want "Boo!" to be printed.
%obj is my player instance. "1441" is the id on the ai bot.
However the following code doesn't work exactly as I expected. It is as if the the bot is detected all the time regardless of my distance from the bot. Any hints? Thanks!
function Player::checkDir(%obj)
{
%dir = VectorScale(%obj.getForwardVector(), "5 0 5");
%person = getWord(ContainerRayCast ( %obj.getPosition() , %dir , $TypeMasks::PlayerObjectType),0);
if(%person $= "1441")
{
echo("YAY!");
}
else
{
echo("Boo!");
}
//echo("SEE " SPC %person);
%obj.schedule(1,checkDir,true);
}
%obj is my player instance. "1441" is the id on the ai bot.
However the following code doesn't work exactly as I expected. It is as if the the bot is detected all the time regardless of my distance from the bot. Any hints? Thanks!
function Player::checkDir(%obj)
{
%dir = VectorScale(%obj.getForwardVector(), "5 0 5");
%person = getWord(ContainerRayCast ( %obj.getPosition() , %dir , $TypeMasks::PlayerObjectType),0);
if(%person $= "1441")
{
echo("YAY!");
}
else
{
echo("Boo!");
}
//echo("SEE " SPC %person);
%obj.schedule(1,checkDir,true);
}
#2
07/07/2007 (6:18 pm)
Hey Chris. Thanks for the tip but yeah that doesn't change anything. It is a math issue. Any other ideas? Thanks in advance!
#3
Those all evaluate to the same thing, assuming of course that playerObject.getId() returns 1234.
I'm not 100% positive, as I'm not familiar with the ContainerRayCast function, but ... for the sake of sanity checking, what is the value of %person before it is checked? Also, what is the value returned from ContainerRayCast ...
try this;
check those ... and use the results to double check your math ...
07/07/2007 (6:55 pm)
Actually, whether it's an int or a string is irrelevant with torquescript, unless you specifically tell torquescript to evaluate it as a string it will first try to evaluate it as an int ... and then finally as an object name reference ...%person == "1234" %person == "playerObject" %person == playerObject %person == playerObject.getId()
Those all evaluate to the same thing, assuming of course that playerObject.getId() returns 1234.
I'm not 100% positive, as I'm not familiar with the ContainerRayCast function, but ... for the sake of sanity checking, what is the value of %person before it is checked? Also, what is the value returned from ContainerRayCast ...
try this;
%fv = %obj.GetForwardVector();
%dir = VectorScale(%fv, "5 0 5");
%cr = ContainerRayCast(%obj.getPosition(), %dir, $TypeMasks::PlayerObjectType);
%person = getWord(%cr, 0);
echo("ForwardVector:" SPC %fv);
echo("VectorScale:" SPC %dir);
echo("ContainerRayCast:" SPC %cr);
echo("Person:" SPC %person);check those ... and use the results to double check your math ...
#4
So %person will either be a 0 (No one is in the player's line of sight) or an object id(The id of the ai bot that is in the player's line of sight.)
Because %person is intialized via the function ContainerRayCast, its prior value is irrelevant.
Any other ideas?
07/07/2007 (7:52 pm)
Hey David. ContainerRayCast is a function that projects a line and will return the id of any object that it intersects given a starting point vector and an ending point vector. If the line doesn't intersect with any object, it will return a 0.So %person will either be a 0 (No one is in the player's line of sight) or an object id(The id of the ai bot that is in the player's line of sight.)
Because %person is intialized via the function ContainerRayCast, its prior value is irrelevant.
Any other ideas?
#5
get the value, then check to see the value -- you said that it 'always appear as though the it sees the bot, regardless of distance' ... so check the value returned from ContainerRayCast ...
And, without knowing the outcome of the script modifications i gave you ... no, I have no other ideas.
07/07/2007 (9:00 pm)
Brandon, I said check the value after it's been initialized, not before ... check the script code I wrote ... get the value, then check to see the value -- you said that it 'always appear as though the it sees the bot, regardless of distance' ... so check the value returned from ContainerRayCast ...
And, without knowing the outcome of the script modifications i gave you ... no, I have no other ideas.
#6
07/07/2007 (10:02 pm)
Sorry, I should rephrase my words. Sometimes, I don't see the bot but most of the time I do. The math and values *SEEM* correct to me but obviously they are not in practice.
#7
the first two are just notes on code practice, but the third one may actually help you out. :)
* what's with "5 0 5" in VectorScale(%obj.getForwardVector(), "5 0 5"); ?
VectorScale takes a vector and a scalar, but you're passing it a vector and a vector. It so happens that VectorScale(%foo, 5) is equivelant to VectorScale(%foo, "5 0 5"), but it seems like you might be expecting this guy to do someting other that what it does. I'm pretty sure that what you really want here is just VectorScale(%forwardVector, 5);.
* are you really using 1441 as a constant ? that ID number will change as soon as you add another object to the simulation which is created before the AIPlayer you now have. ie, another AIPlayer, a building, a tree, a simgroup, etc. fine for debugging, but for the real app you'll want to use a variable.
* your call to ContainerRayCast() is casting a ray from the center of the player out five units in front of the player, and scanning for object of type PlayerObjectType. It's going to hit the player itself. So i would expect that the return value of getWord(containerraycast(), 0) would always be the ID of %obj itself. Try adding a echo("obj:" SPC %obj); in there in addition to the other prints David suggested. --> to avoid having the raycast collide with %obj, use the optional exempt parameter, which is documented here.
07/07/2007 (10:18 pm)
Brandon - a couple things:the first two are just notes on code practice, but the third one may actually help you out. :)
* what's with "5 0 5" in VectorScale(%obj.getForwardVector(), "5 0 5"); ?
VectorScale takes a vector and a scalar, but you're passing it a vector and a vector. It so happens that VectorScale(%foo, 5) is equivelant to VectorScale(%foo, "5 0 5"), but it seems like you might be expecting this guy to do someting other that what it does. I'm pretty sure that what you really want here is just VectorScale(%forwardVector, 5);.
* are you really using 1441 as a constant ? that ID number will change as soon as you add another object to the simulation which is created before the AIPlayer you now have. ie, another AIPlayer, a building, a tree, a simgroup, etc. fine for debugging, but for the real app you'll want to use a variable.
* your call to ContainerRayCast() is casting a ray from the center of the player out five units in front of the player, and scanning for object of type PlayerObjectType. It's going to hit the player itself. So i would expect that the return value of getWord(containerraycast(), 0) would always be the ID of %obj itself. Try adding a echo("obj:" SPC %obj); in there in addition to the other prints David suggested. --> to avoid having the raycast collide with %obj, use the optional exempt parameter, which is documented here.
#8
So...
Good luck
07/10/2007 (8:16 pm)
If you don't have TDN access, ContainerRayCast with the exempt parameter looks like this:ContainerRayCast([b][start][/b],[b][end][/b],[b][search masks][/b],[b][exempt][/b]);Set the exempt parameter to %obj, and the raycast wont hit it. I'm doing the same thing you are (only with a key binding), no luck yet.
So...
%fv = %obj.GetForwardVector();
%fv = VectorNormalize(%fv); // Have no idea but this might do something
%dir = VectorScale(%fv, "5");
%cr = ContainerRayCast(%obj.getPosition(), %dir, $TypeMasks::PlayerObjectType,%obj);
%person = getWord(%cr, 0);
echo("ForwardVector:" SPC %fv);
echo("VectorScale:" SPC %dir);
echo("ContainerRayCast:" SPC %cr);
echo("Person:" SPC %person);
if(%person)
echo("YAY!");
else
echo("Boo!");Good luck
Torque Owner ChrisG
try:
if(%person == 1441)
instead of
if(%person $= "1441")
This may not be the only though, just that ids are usually ints.