简体   繁体   English

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

[英]How to get an angle between a direction faced and a point in 3D space

The information I have is the vertical angle of the player's vision, the horizontal angle to the point in question, the distance to that point and the point's vertical height. 我所拥有的信息是玩家视野的垂直角度,与所讨论的点的水平角度,到该点的距离以及点的垂直高度。

I've figured out how to get the angle if the player isn't looking up or down (vertical angle) by using the following. 如果玩家没有向上或向下看(垂直角度),我已经想出如何通过使用以下来获得角度。

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));
}

原始方法导出空间点与玩家视觉方向之间的角度

What I can't figure out is how to get that angle if the player's vision direction is modified by a vertical angle (looking up or down). 如果玩家的视线方向被垂直角度(向上或向下看)修改,我无法弄清楚如何获得该角度。 I've been mulling this over in my head for a few days and I can't figure out a way to do it. 我已经在脑子里仔细考虑了几天,我无法想办法做到这一点。

What I'm using this for is to generate a vision cone cutoff. 我正在使用的是生成视锥截止。 When an object is checked for visibility, the information I have to work with is the angle of the object from the player, the distance to that object, and its height. 当检查对象的可见性时,我必须使用的信息是对象与玩家的角度,到该对象的距离以及它的高度。 This initial range check will return an angle from the player's direction of vision and determine whether the object is visible or not. 该初始范围检查将从玩家的视线方向返回一个角度,并确定该对象是否可见。

墙壁设置为透明,以显示视野范围

Here is a shot of the code in debug using the solution provided by @HABO Unfortunately, it always results in a NaN error. 下面是使用@HABO提供的解决方案调试代码的镜头。不幸的是,它总是导致NaN错误。

NaN似乎总是被退回。

Converting the angles to radians before using them seems to fix a lot of the numerical errors. 在使用它们之前将角度转换为弧度似乎可以解决许多数值误差。 I don't understand the formula at the end that converts the previous numbers into the final angle though. 我不明白最后将前一个数字转换为最终角度的公式。 这是一组新的数字,使用一些更容易使用的数字

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM