Click.
Bam!
BOOM💥
START
Drag the Guard or Ninja to interact

Code Snippet: Vision Check

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?

Answers to Common Developer Questions

What is the Dot Product used for in games?
The Dot Product is incredibly fast at mathematically comparing the alignment of two vectors. It is universally used to check if an enemy is in front of or behind the player, to calculate 3D dynamic lighting angles, and to determine Field of View (FOV) vision cones.
What does it mean if the Dot Product is 0?
If the Dot Product of two normalized vectors is exactly 0.0, it means the two vectors are perfectly perpendicular (90 degrees apart). In gaming terms, the target is exactly to the Side of the player.
How do you calculate a Vision Cone with the Dot Product?
You calculate the dot product of your Enemy's Forward Vector and the Direction-to-Target vector. If the result is greater than the Cosine of half your desired Field of View angle (e.g., Math.cos(45) for a 90 degree FOV), the target is mathematically visible inside the cone.