How do i render and detect a line of sight?

坚强是说给别人听的谎言 提交于 2019-12-24 00:56:10

问题


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 http://image.com.com/gamespot/images/screenshots/gs/action/metalgearsolid/metalgearsolid_790screen001.jpg

-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.


回答1:


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).



来源:https://stackoverflow.com/questions/2696338/how-do-i-render-and-detect-a-line-of-sight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!