Dot Product & Field of View
Calculate angles, vision cones, and facing directions.
How does it work? ↓How does the Dot Product work?
The Magic Value (-1 to 1)
The dot product compares two normalized vectors (vectors
with a length of exactly 1). It returns a single number (a float)
between -1.0 and 1.0 that perfectly describes
how parallel those two vectors are.
-
1.0= They point in the exact same direction. -
0.0= They are perfectly perpendicular (90 degrees, forming an L). -
-1.0= They point in exact opposite directions.
Vision Cones (The Cosine Trick)
The dot product is actually mathematically identical to the Cosine
of the angle between the two vectors:
dot(A, B) = cos(angle).
Because of this, we can easily check if a target is inside an enemy's
Field of View (FOV). If the enemy has a 90° FOV, they can see 45°
to the left and 45° to the right.
Instead of doing expensive angle math every frame, we just pre-calculate
the threshold:
threshold = cos(FOV / 2). Then, if the
dot(enemy_forward, dir_to_target)
is greater than that threshold, the target is inside
the cone!
Common Uses
- Vision Cones: Is the player in front of the enemy?
- Lighting: How directly is light hitting this surface?
- Movement: Are we moving forwards or sliding sideways?
- Backstabbing: Did the attack hit the enemy's back?