Thanks Cal. Looks like that might work.
EDIT: Ugh..trig/geomentry. I might as well be reading a script in chinese. Don't suppose anyone would be willing to alter something like what Cal suggested to work a bit differently?
This is the script:
/*
fDeviation is the deviation from the facing angle which is permissable
fDistance is the distance between the source and the target which is permissable
*/
int GetIsBehindBackstabTarget( object oSource, object oTarget, float fDeviation, float fDistance ) {
if(!GetIsObjectValid(oSource))
return FALSE;
if(!GetIsObjectValid(oTarget))
return FALSE;
float fFacingTarget=GetFacing(oTarget);
float fFacingTargetX=cos(fFacingTarget);
float fFacingTargetY=sin(fFacingTarget);
float fFacingSource=GetFacing(oSource);
float fFacingSourceX=cos(fFacingSource);
float fFacingSourceY=sin(fFacingSource);
location lTargetLocation=GetLocation(oTarget);
vector vTargetLocation=GetPositionFromLocation(lTargetLocation);
location lSourceLocation=GetLocation(oSource);
vector vSourceLocation=GetPositionFromLocation(lSourceLocation);
float fDirectionHorizontalBetween=vSourceLocation.x-vTargetLocation.x;
float fDirectionVerticalBetween=vSourceLocation.y-vTargetLocation.y;
float fDot=fFacingTargetX*fDirectionHorizontalBetween+fFacingTargetY*fDirectionVertica
lBetween;
float fProductMags=sqrt(pow(fFacingTargetX, 2.0f)+pow(fFacingTargetY, 2.0f))*sqrt(pow(fDirectionHorizontalBetween, 2.0f)+pow(fDirectionVerticalBetween, 2.0f));
//Returns an angle from 0 to 180, the angle between the target's facing angle vector and the vector pointing from target to source
float angleBetween=acos(fDot/fProductMags);
if(angleBetween<(180.0f-fDeviation)) {
//Not valid angle, the source isn't standing within the correct wedge behind the target
return FALSE;
}
//Create a wedge of validity with fDeviation degrees deviation from the source's facing angle
float fFacingSourcePlus=fFacingSource+fDeviation;
float fFacingSourceMinus=fFacingSource-fDeviation;
//Obtain the actual facing angle between source and target
float fFacingSourceToTarget=VectorToAngle(vTargetLocation-vSourceLocation);
/*
The following conditions check to determine if the source to target actual angle is within the permited deviation.
Note that an angle which is greater than 180 deviation from the facing could produce unpredictable results.
*/
if(fFacingSourcePlus>360.0f) {
//Because the addition deviation is greater than 360 it will be necessary to perform two checks
int isValid=FALSE;
if(fFacingSourceToTarget>=fFacingSourceMinus && fFacingSourceToTarget<=360.0f) {
isValid=TRUE;
}
if(!isValid) {
float fFacingSourcePlus2=fFacingSourcePlus-360.0f;
if(fFacingSourceToTarget>=0.0f && fFacingSourceToTarget<=fFacingSourcePlus2) {
isValid=TRUE;
}
}
if(!isValid) {
//Not valid angle, the source isn't properly facing the target
return FALSE;
}
}
else if(fFacingSourceMinus<0.0f) {
//Because the subtraction deviation is less than 0 it will be necessary to perform two checks
int isValid=FALSE;
if(fFacingSourceToTarget>=0.0f && fFacingSourceToTarget<=fFacingSourcePlus) {
isValid=TRUE;
}
if(!isValid) {
float fFacingSourceMinus2=fFacingSourceMinus+360.0f;
if(fFacingSourceToTarget>=fFacingSourceMinus2 && fFacingSourceToTarget<=360.0f) {
isValid=TRUE;
}
}
if(!isValid) {
//Not valid angle, the source isn't properly facing the target
return FALSE;
}
}
else {
//Only one check necessary
if(!(fFacingSourceToTarget>=fFacingSourceMinus && fFacingSourceToTarget<=fFacingSourcePlus)) {
//Not valid angle, the source isn't properly facing the target
return FALSE;
}
}
float fDistanceActual=GetDistanceBetweenLocations(lTargetLocation, lSourceLocation);
if(fDistanceActual>fDistance) {
//Not valid distance between, greater than 2 meters away
return FALSE;
}
return TRUE;
}
Modifié par GhostOfGod, 09 février 2011 - 05:45 .