问题描述
如果您查看右上角,您会在雷达上看到敌方单位的视线.
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).
我不知道公式,也没有在C#中使用任何数学库/命名空间
I dont know the formula nor used any math libs/namespaces in C#
-编辑-
基本上,这是2d原型.没有什么是完美的,它具有可移动的摄像头和单元,并且只能从左上右下看,而不能从对角线看.可能有一堵墙挡住了视线,但没有别的.另外,其他敌人走进他们的行列时也不应触发动作.
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.
所以我真的需要一个来源(敌人),一个dst(玩家)并记下阻碍视力的墙壁.
So really i need a source(enemy), a dst (player) and keep account of walls blocking vision.
-edit-我最终使用了rect.足够好了,我能够用 prototype 进行其他工作,然后编写raycast代码.
-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.
推荐答案
这实际上取决于您如何设置世界几何,但通常的方法是通过射线投射.也就是说,您在自己和敌人(或您感兴趣的任何物体)之间绘制了一条假想的线,并查询环境以查看是否有任何几何形状与该线相交.
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.
不同的世界几何将具有执行射线投射的不同方法(例如,BSP树将不同于门户系统,而高度系统地形也会再次不同,依此类推).
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).
这篇关于如何渲染和检测视线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!