如何获得面向方向与3D空间中的点之间的角度

时间:2013-04-05 23:36:42

标签: c# trigonometry vision angle

我所拥有的信息是玩家视野的垂直角度,与所讨论的点的水平角度,到该点的距离以及该点的垂直高度。

如果玩家没有使用以下内容向上或向下看(垂直角度),我已经想出了如何获得角度。

float GetVisionAngle(float angleHoriz, float angleVert, float distance, float height)
{
double A = Math.Cos(angleHoriz * (Math.PI / 180)) * distance);
double hypotenuse = Math.Sqrt(distance * distance + height * height);
return (float)(Math.Acos(A / hypotenuse) * (180 / Math.PI));
}

Original Method to derive angle between a point in space and player's vision direction

如果玩家的视线方向被垂直角度(向上或向下看)修改,我无法弄清楚如何获得该角度。我已经在脑子里仔细考虑了几天,我无法想办法做到这一点。

我使用它的目的是产生视锥截止。当检查对象的可见性时,我必须使用的信息是对象与玩家的角度,到该对象的距离以及它的高度。这个初始范围检查将从玩家的视线方向返回一个角度,并确定该物体是否可见。

The walls are set to transparent to show vision range

以下是使用@HABO提供的解决方案调试代码的镜头 不幸的是,它总是会导致NaN错误。

NaN seems to always be returned.

在使用它们之前将角度转换为弧度似乎可以解决许多数值误差。我不明白最后将前一个数字转换为最终角度的公式。 Here is a new set of numbers using some easier to work with figures

1 个答案:

答案 0 :(得分:2)

aH = Angle in the horizontal plane between the line of sight (LOS) and the object.  (angleHoriz)

aV = Angle in the vertical plane of the LOS.  (angleVert)

d = Distance to the object in the horizontal plane.  (distance)

h = Height of the object above the horizontal plane.  (height)

dO = Distance from the origin to the object.
   = sqrt( d * d + h * h )

oH = Horizontal offset from the LOS to the object at the base of the wall.
   = sin( aH ) * d

dH = Horizontal distance from the origin to the wall.
   = cos( aH ) * d

hLOS = Height at which the LOS intersects the wall.
     = tan( aV ) * dH

dLOS = Distance from the observer to the LOS at the wall.
     = sqrt( dH * dH + hLOS * hLOS )

dW = Distance along the wall between the line of sight and the object.
   = sqrt( oH * oH + ( h - hLOS ) * ( h - hLOS ) )

answer = acos( ( dLOS * dLOS + dO * dO - dW * dW ) / ( 2 * dLOS * dO ) )