views:

92

answers:

1

If you look at the top right you'll see on a radar an enemy unit line of sight.

I was wondering what is the most efficient or easiest way (little code, fairly accurate. doesnt need to be perfect) to detect if something is in your line of sight? I may or may not need to render it (i likely wont).

I dont know the formula nor used any math libs/namespaces in C#

-edit-

Basically this is a 2d prototype. nothing has to be perfect and it will have movable camera, units and it will only look left right up down but not diagonally. There may be a wall blocking line of sight but nothing else. Also other enemies shouldnt trigger an action when they walk into it.

So really i need a source(enemy), a dst (player) and keep account of walls blocking vision.

alt text

-edit- i ended up using a rect. It was good enough and i was able to work on other thing in the prototype then writing raycast code.

+3  A: 

It really depends on how your world geometry is set up, but the usual method is via ray casting. That is, you draw an imaginary line between yourself and the enemy (or whatever you're interested in) and query the environment to see whether there is any geometry intersecting that line.

Different world geometry will have different methods of performing ray casting (for example, a BSP tree would be different to a portal system, which would be different again to a heightfield terrain and so on).

Dean Harding
it doesnt need to be complex or very fast. Just not ridiculously slow. I edit the question for more info.
acidzombie24
Again, it depends entirely on how you store your world geometry. If your world is made up of a simple list of lines that represent the "walls" then you will just need to do a series of ray-ray intersections between the line joining the player and enemy and the lines that make up the walls. If any intersect, then your line of sight is blocked.
Dean Harding