0
$\begingroup$

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.

$\endgroup$
0

1 Answer 1

1
$\begingroup$

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$.

$\endgroup$
16
  • $\begingroup$ I am not sure if I am 100% correct in using your formula, but basically I converted my Latitude/Longitude/Altitude to cartesian3 points because I was using a helper function in my code to generate the directional vector which use cartesian 3 points. Long story short. plugging directly into that bottom formula my resulting number is -730.6584673334336. The numbers I have and am using are large and fairly precise. Do I need to take the cos of that and convert to degrees? The only reason I ask is because that gives me something like: -13.52 deg. Otherwise I feel like I am doing something wrong. $\endgroup$
    – AndyPet74
    Commented Aug 8, 2023 at 18:47
  • $\begingroup$ Here is a stackblitz that is just a javascript application running that formula with the variables I have: stackblitz.com/edit/js-vkpnir?file=index.js $\endgroup$
    – AndyPet74
    Commented Aug 8, 2023 at 19:00
  • 1
    $\begingroup$ The ^ operator is not square in javascript. It is exclusive or. $\endgroup$
    – Andrei
    Commented Aug 8, 2023 at 21:42
  • $\begingroup$ Ok, after fixing all the code to use Math.Pow() my result is ~-0.00769.... and if I understand correctly I would want to check to make sure that the result I got is less than the angle of my cone correct? If I assume my cone's angle is 5deg then I would want the number I get to be less than or equal to -2.5 to 2.5 deg's is that correct? $\endgroup$
    – AndyPet74
    Commented Aug 9, 2023 at 12:56
  • $\begingroup$ The number is right, but it's the cosine of the angle. So it means that it's close to $90^\circ$. $\cos 2.5^\circ=0.99904822158$. And you want your number to be greater than that. When you are looking straight at the other object, the cosine is $1$. $\endgroup$
    – Andrei
    Commented Aug 9, 2023 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .