如何计算某人视野中的某物是否存在 [英] How to calculate if something lies in someone's field of vision

查看:244
本文介绍了如何计算某人视野中的某物是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主题,在2D空间和速度中都有一个位置,都用矢量表示。这个主题的每个视角都有135度的视野。它看起来与它正在移动的方向相同(速度矢量)。



我有一个在由矢量表示的2D空间中的位置的对象。



在图形中,蓝色背景上的对象是可见的,红色背景上的对象对主体不可见。



我可以计算在给定时刻哪些物体位于主体的视野内吗?

解决方案

您只需要找到与对象的距离和他们的角度。然后,检查距离不大于浅蓝色圆圈的半径,速度矢量与对象矢量之间的角度不会太大。

欧几里得距离简单地为 sqrt((x2 - x1)^ 2 +(y2 - y1)^ 2) link )。



至于角度,表示向量 (x2 - x1,y2 - y1)和速度(vx,vy)极坐标系统,然后检查角度之间的绝对差值不超过135度。在编程语言中,通常有一个方便的函数 atan2(y,x)来查找单个向量的极角( link )。






极地从矢量Ox逆时针方向测量一个点的角度。考虑我们有两点:速度矢量的终点(vx,vy)(左图)以及来自我们对象(x1, (x2,y2):它是向量(x2 - x1,y2 - y1) )(中心图片)。让我们说,极角分别是 alpha beta 。那么(vx,vy)(x2 - x1,y2 - y1)之间的角度就是它们之间的差异(右图)。





然而有一个窍门。您获得的 alpha beta 值通常是从-PI到+ PI或从0到2PI。因此, beta - alpha 之间的差异将在-2PI和+ 2PI之间,您希望它位于-PI(从当前方向顺时针180度)到+ PI (从电流方向逆时针旋转180度)。为此,一些简单的转换就足够了,比如这个伪代码:

  if angle> + PI:
angle:= angle - 2PI
如果角度< -PI:
angle:=角度+ 2PI


I have a subject that has a position in a 2D space and a velocity, both represented by a vector. The subject has a field of vision of 135 degree to each side. It looks in the same direction as it is moving (the velocity vector).

I have objects with a position in a 2D space represented by a vector.

In the drawing the objects on blue background are visible, the objects on red background are not visible to the subject.

How can i calculate which objects lie in the subject's field of vision at a given moment?

解决方案

You just need to find the distance to the objects and the angle towards them. Then, check that the distance is not greater than the radius of that light blue circle, and the angle between the speed vector and the vector to the object is not too large.

The Euclidean distance is simply sqrt ((x2 - x1)^2 + (y2 - y1)^2) (link).

As for the angle, represent the vector (x2 - x1, y2 - y1) and the speed (vx, vy) in polar coordinate system, and then check that the absolute difference between the angles is no more than 135 degrees. In programming languages, there is often a convenient function atan2 (y, x) to find the polar angle of a single vector (link).


The polar angle of a point is measured from vector Ox in counterclockwise direction. Consider we have two points: endpoint of the velocity vector (vx, vy) (left pic) and endpoint of a vector from our object (x1, y1) to the object in question (x2, y2): it's the vector (x2 - x1, y2 - y1) (center pic). Let us say the polar angles are alpha and beta, respectively. Then the angle between (vx, vy) and (x2 - x1, y2 - y1) is the difference of these polar angles (right pic).

There's a trick however. The alpha and beta values you get are typically either from -PI to +PI, or from 0 to 2PI. So, the difference beta - alpha will be between -2PI and +2PI, and you'll want it be between -PI (180 degree clockwise from current direction) and +PI (180 degree counterclockwise from current direction). For that, some simple transformation will suffice, such as this pseudocode:

if angle > +PI:
    angle := angle - 2PI
if angle < -PI:
    angle := angle + 2PI

这篇关于如何计算某人视野中的某物是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆