Line of Sight with Field of Vision code
by JesseL · in Torque Game Engine Advanced · 11/08/2008 (2:27 pm) · 1 replies
I worked with a math major over some previous code on the forums that did this. We made some tweaks to the code so that a sphere was represented in two 180 degree half circles around the character. One is positive the right side and one is negative the left side.
function Player::lineOfSight( %this, %obj )
{
%eyeTrans = %this.getEyeTransform();
%eyeEnd = %obj.getWorldBoxCenter();
%searchResult = containerRayCast(%eyeTrans, %eyeEnd, $TypeMasks::TerrainObjectType |
$TypeMasks::InteriorObjectType | $TypeMasks::ItemObjectType | $TypeMasks::PlayerObjectType
| $TypeMasks::StaticObjectType , %this);
%foundObject = getword(%searchResult,0);
if(%foundObject == %obj)
{
%angle = Player::check2DAngletoTarget( %this, %obj );
if( %angle <= 45 && %angle >= -45 )
{
return true;
}
}
return false;
} // End of function line of sight which checks to see if the player can actually see what its looking at
function Player::check2DAngletoTarget(%this, %obj)
{
%eyeVec = VectorNormalize(%this.getEyeVector());
%eyeangle = %this.getAngleofVector(%eyeVec);
%posVec = VectorSub(%obj.getPosition(), %this.getPosition());
%posangle = %this.getAngleofVector(%posVec);
%angle = %posangle - %eyeAngle;
%angle = %angle ? %angle : %angle * -1;
return %angle;
}
function Player::getAngleofVector(%this, %vec)
{
%vector = VectorNormalize(%vec);
%vecx = getWord(%vector,0);
%vecy = getWord(%vector,1);
if(%vecx >= 0 && %vecy >= 0)
%quad = 1;
else if(%vecx >= 0 && %vecy < 0)
%quad = 2;
else if(%vecx < 0 && %vecy < 0)
%quad = 3;
else
%quad = 4;
%angle = mATan(%vecy, %vecx);
%degangle = mRadToDeg(%angle);
switch(%quad)
{
case 1:
%angle = %degangle;
case 2:
%angle = %degangle+360;
case 3:
%angle = %degangle+180;
case 4:
%angle = %degangle+90;
}
return %degangle;
}About the author
I just realized that if I wanted to create a cat that caught on fire and ran up a telephone pole and then burst into a blue waterfall. That wouldn't be to hard!
Torque Owner JesseL
Pyrotronics-Games