Archived

This topic is now archived and is closed to further replies.

seen player

This topic is 5046 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have this function to see if the ai has seen the player void CAi::SeenPlayer(CVector PPosition)// Position of the player { CVector Length; // Get the distances Length = PPosition - Position; // Get the angle double TempAngle = atan(Length.x/Length.z); // Convert it to degrees TempAngle = TempAngle*RadToAng; if((TempAngle > -FOV)&& (TempAngle < FOV)) } Now it works they why i wanted but there no way to tell if you''re behind or infront of the ai. How would i tell if the player is infront or behind the ai? unless someone can tell me a better way of doing it. I''m going to take the convert to degrees bit out when i get it working.

Share this post


Link to post
Share on other sites
This will die horribly when Length.z is 0.

In general, using trigonometry is almost never what you actually want.

In this case, I would do this:

On start-up, I would calculate cos(FOV) and store it in a variable.

When checking, I would do this:


void CAi:ointInFOV( CVector point )
{
point -= Position();
point.Normalize();
CVector myFront = Orientation().FrontVector();
return myFront.Dot( point ) >= cosFieldOfView;
}


The dot of a normalized vector with another normalized vector is the cosine of the angle between those vectors. Which happens to be well defined for "behind" vs "in front".

Note that this calculates a cone, not a upwards-aligned pie slice like yours does. If you want the pie slice, project point (after subtraction of position) and myFront to the ground plane before normalizing, then dot and compare.

Oh, and if you really need atan() to tell you in front/behind sometime (which you usually shouldn''t do) then atan2() is your friend.

Share this post


Link to post
Share on other sites
Thanks. Length.x and Length.z will never be 0 anyway though. one question what is Orientation().FrontVector(); at the moment i just have the ai''s angle along the y axis. how would i calculate the front vector?

Share this post


Link to post
Share on other sites