Public Group

# How to determine if two entities can see each other?

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

## 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 on other sites
I could be wrong about this, but I think you need to normalize diffPos before calling atan2.

##### Share on other sites
Quote:
 Original post by lolI 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*byfloat relative_angle = fabs(atan2(pd, d));relative_angle = to_degrees(relative_angle);if (relative_angle < 45) {    // Entity can see player}

1. 1
2. 2
Rutin
20
3. 3
A4L
15
4. 4
5. 5
khawk
14

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633739
• Total Posts
3013618
×