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

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

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.

The walls are set to transparent to show vision range

Here is a shot of the code in debug using the solution provided by @HABO Unfortunately, it always results in a NaN error.

NaN seems to always be returned.

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. Here is a new set of numbers using some easier to work with figures

有帮助吗?

解决方案

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归因
不隶属于 StackOverflow
scroll top