Jump to content
  • Advertisement
Sign in to follow this  
h3ro

How to determine if two entities can see each other?

This topic is 3614 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

Hallo, I have been struggling with this basic problem for a while and could need a bit of help. I am trying to determine if a AI entity can see the player entity. The AI entity knows where the player is, and it has one variable for position and one for rotation. I have tried this:
Vector2D diffPos = playerPos - entityPos
float r = convertToDegrees(atan2(diffPos.y, diffPos.x)) - entityRotation;

if( (r < 45 && r > 0) || (r > 360-45 && r < 360) )
// Can see target
Thanks

Share this post


Link to post
Share on other sites
Advertisement
I could be wrong about this, but I think you need to normalize diffPos before calling atan2.

Share this post


Link to post
Share on other sites
Quote:
Original post by lol
I could be wrong about this, but I think you need to normalize diffPos before calling atan2.
No, atan2() does not require that the input be normalized.

@The OP: I didn't look at the code too closely, but working directly with absolute angles (as you appear to be doing) can be tricky, primarily due to periodicity.

I would recommend instead using relative angles, like this:
vector2 entity.forward(cos(entity.angle), sin(entity.angle));
vector2 diff = player.position - entity.position;
float d = dot(forward, diff);
float pd = perp_dot(forward, diff); // perp_dot(a,b) = -a.y*bx+a.x*by
float relative_angle = fabs(atan2(pd, d));
relative_angle = to_degrees(relative_angle);
if (relative_angle < 45) {
// Entity can see player
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!