Quick review: If we have two directions, vectors, coordinates in any number of dimensions that we want to find out how they are related and if they are independent then there is a unique plane which contains them. Otherwise they fall on the same line through the origin and there is an infinite number of planes, so this is a potential hedge case. And with FP computations they don’t have to be exactly linear. Choose one to be the reference “a” and that is the direction of the positive x-axis of the plane (a Cartesian coordinate system) and some of the relations (with how they relate to trig) are:
[tr][td]unit direction of positive x[/td][td] a/|a| = a/sqrt(a·a)[/td][/tr]
[tr][td]scaled projection of ‘b’ onto the x axis[/td][td] (a·b)[/td][td]|a||b| cos(t)[/td][/tr]
[tr][td]scaled projection of ‘b’ onto the y axis[/td][td] sqrt((a·a)(b·b) - (a·b)2)[/td][td]sqrt(|a|2|b|2 - (|a||b| cos(t))2) = |a||b| sin(t)[/td][/tr]
[tr][td]unit direction of positive y[/td][td] ((a·a)b - (a·b)a)/sqrt((a·a)(b·b) - (a·b)2)[/td][/tr]
One thing I forgot to point out before was why ‘b’ magically is projected into the positive ‘y’ axis. This is because how we would compute the direction of ‘y’ if we actually needed. If ‘a’ and ‘b’ fall on the same line through the origin, then the projection into ‘y’ is zero and if we were to attempt to compute the unit direction of it the equation would explode. This is common when an equation expects a unique solution and there is in fact none or an infinite number.
An example, walking through @theagentd code above, translates into:
We have an entity at point § with a unit direction facing of (f) and we want to know if some other entity at point (e) is inside a view cone, which is centered about the facing and the outer edge has some angle (t) with respect to the facing. If we choose (f) to be the reference direction (a=f), then the relative direction to the other entity is (b=e-p). Pre-compute: k=cos(t), then
(a·b) = |a||b| cos(s), and since |a|=1, (a·b) = |b| cos(s), where ‘s’ is the unknown angle
and since we want to know if s < t, we can change that to cos(s) > cos(t) given the properties of cos. Since |b| is unknown, theagentd balances out the scale by computing: (a·b)/|b| > k. The thing I mentioned earlier is a property of inequalities: multiplying both side by a positive number is valid and doesn’t change the direction. So this can be computed as:
(a·b) > |b| k
For geometry (and math in general) it’s always true that “a change in coordinate system doesn’t change the problem”. In this case the extra uniform scaling on the problem does effect the relation.
If restricting the maximal half-angle of the cone to >= Pi/2, then
(a·b)2 > (b·b) k2, where k2 could be the pre computed value.
EDIT: had dot(a,b)…instead of dot(b,b) on that last left-hand side, which is |b|^2…opps. Also the restriction can be lifted, but need to inequality properties typed up…and this whole scaled projection into a direction is half of how SAT works.