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

问题描述 投票:0回答:1

我掌握的信息是玩家视野的垂直角度、到该点的水平角度、到该点的距离以及该点的垂直高度。

我已经通过使用以下方法弄清楚了如果玩家没有向上或向下看(垂直角度)如何获取角度。

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

c# trigonometry
1个回答
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 ) )
© www.soinside.com 2019 - 2024. All rights reserved.