Sign in to follow this  

Line of Sight

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

Quote:
Original post by Regano
I'm looking for a simple 2D solution to displaying a line of sight cone based on the enemies current position and viewing angle. Can anyone help me?
Here's some pseudocode:
Vector2 forward(cos(enemy.directionAngle), sin(enemy. directionAngle));
Vector2 side(-forward.y, forward.x);
float fov = enemy.fieldOfView * 0.5f;
float c = cos(fov);
float s = sin(fov);
Vector2 v1(c*forward+s*side);
Vector2 v2(c*forward-s*side);
v1 and v2 now represent the edges of the line of sight cone. To draw the cone, simple draw two lines starting at the enemy position and in the direction of v1 and v2 respectively.

Let me know if anything is unclear.

Share this post


Link to post
Share on other sites
Quote:
Original post by Regano
Quote:


Vector2 v1(c*forward+s*side);
Vector2 v2(c*forward-s*side);



Thanks for your help, is v1 and v2 supposed to have just one parameter?
That's meant to be the copy constructor. You could also write it as:
Vector2 v1 = c*forward+s*side;
Vector2 v2 = c*forward-s*side;

Share this post


Link to post
Share on other sites

This topic is 4306 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this