I use CesiumJS, a javascript library, to render entities in a 3-dimensional space. I need to detect if one entity is "viewing" another entity. My initial thought is to use a cone to represent the field of view of the entity. I need to be able to make the cone variable angle and height. What is the most efficient way to check if Entity 2 is inside the cone emitted by Entity 1? I am doing this calculation a bunch of times so I am trying to find a way to do it efficiently.
In Cesium, my entities are plotted using Lat/long/alt, which I figured I could think of as X,Y,Z. I also know the orientation of each point, so I know which way to make the cone emit. Cesium has some built-in functions for checking intersections but all seem to use rays or lines emitting from entity 1, which don't give me my desired output. Any help would be greatly appreciated.
Let's assume that your point where you check the viewing is $\vec P_0(x_0,y_0,z_0)$, you are looking in a direction $\vec d(d_x,d_y,d_z)$ in a cone with opening (angle between side and axis) $\alpha$, and you want to check if $\vec P_1(x_1,y_1,z_1)$ is inside the cone. Then you want the angle between the vector $\vec P_1-\vec P_0$ and $\vec d$ to be less than $\alpha$. You can write this using scalar product: $$\cos\left(\angle(\vec P_1-\vec P_0, \vec d)\right)=\frac{(\vec P_1-\vec P_0)\cdot \vec d}{|\vec P_1-\vec P_0||\vec d|}\\=\frac{(x_1-x_0)d_x+(y_1-y_0)d_y+(z_1-z_0)dz}{\sqrt{(x_1-x_0)^2+(y_1-y_0)^2+(z_1-z_0)^2}\sqrt{dx^2+dy^2+d_z^2}}$$ If the viewing angle is smaller than $\alpha$, then the last expression must be greater than $\cos\alpha$.