by Spike » Sun Sep 02, 2012 5:45 am
This post is basically an explaination of the infront function.
in qc terms:
cos(anglebetweenvectors) == normalisedvector1 * normaliseedvector2
so vector*vector will be 1 if they are identical, -1 if they point in opposite directions, and 0 if they point 90 degrees to each other.
Considering the greatest angle difference you can have is 0 to 180 and fields of view are measured in both directions instead of just one, ((fwd*normalilze(targetpos - sourcepos)) > cos(fovvalue/2)) can be used to check whether the direction is within the given field-of-view angle.
cos, however, is an extension, but you can replace the result of that function with a constant (for example, quake's collision code reguarly uses 0.7 for surfaces that are considered pointing upwards for 'onground' checks).
Note that this is NOT the same as 'onscreen', as this would require a circular screen. The virtual circular screen will generally have the same width as the screen, thus this may fail for the corners of the screen. Also, you may also need to pay attention to bounding boxes.
Additionally, makevectors(self.angles) is generally only valid when angles_x=0 (which is true for monsters). If used for a player, you should be using makevectors(self.v_angle) instead. This will scale correctly with pitch (instead of being 1/3rd), and will also work around an old (unfixable) sw quake bug where .angles_x on alias models is inverted.
(qc's vector*vector operation/instruction is actually a 3-componant dotproduct)
(dotproduct is useful for all sorts of things!)
.