#### Archived

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

# seen player

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

## 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 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 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?

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632665
• Total Posts
3007710
• ### Who's Online (See full list)

There are no registered users currently online

×