See all glossary terms

Dot Product

The dot product of two vectors is a measure of the similarity between two vectors. It can also be conceptualized as a measure of how much one vector reinforces the other.
Imagine an airport moving walkway going in one direction at a speed of 5 km/h. If you walk on the treadmill in the same direction at a speed of 3 km/h, you're moving at 8 km/h relative to the ground. Your vector and the moving walkway's vector are reinforcing each other, because they're going in the same direction.
But if you walk in the opposite direction at 3 km/h, you're moving at 2 km/h relative to the ground: the two vectors are opposing each other. Now, let's say that you walk perpendicular to the moving walkway; then, you and the walkway have completely independent motion, and you don't influence each other at all.
The dot product is a measure of how much two vectors reinforce each other. If it is positive, it means the two vectors are relatively similar. If it is negative, it means they are relatively dissimilar. If the vectors are perpendicular, the dot product is zero.
Dot product examples
Dot product examples
Examples of dot product results between normalized vectors (both vectors have a length of 1)
In mathematical terms, the dot product is the product of the lengths of the two vectors multiplied by the cosine of the angle between them.
In games, we most often use the dot product to:
  • Find the angle between two vectors.
  • Know if they face the same direction or the opposite direction.
When the two vectors are normalized (when their length is 1), their dot product is equal to the cosine of the angle between them (because the product of their lengths is also 1). It's useful because the cosine of an angle is a value ranging from -1 to 1.
NOTE:
In other words, if you're using normalized vectors, the dot product and the cosine of the angle between them are the same thing.
When the vectors are not normalized, the dot product is still a measure of similarity, but it cannot directly be compared to a cosine value.
Negative dot product
Negative dot product
Positive dot product
Positive dot product
Examples of dot product between vectors of different lengths
You can use this to calculate line of sight, to know if two characters are facing each other, and more. The following example comes from M4 in Learn 3D Gamedev From Zero and uses the dot product to give a mob a cone of vision.
class_name Mob

## The player character
@export var player: Node3D
## The range at which the mob can see the player
@export var vision_range := 7.0
## The angle of the mob's vision cone
@export_range(0.0, 360.0, 0.1, "radians_as_degrees") var vision_angle := PI / 4.0:
  set = set_vision_angle

var _cos_max_angle_of_vision := 0.0

func set_vision_angle(value: float) -> void:
    vision_angle = value
    _cos_max_angle_of_vision = cos(value)


func _ready():
  # ensure the setter has run and _cos_max_angle_of_vision is set
  vision_angle = vision_angle


func _physics_process(delta):

		var distance := global_position.distance_to(player.global_position)
		if distance > vision_range:
			return

		var direction := global_position.direction_to(player.global_position)
		var dot_product := global_basis.z.dot(direction)

		var player_is_in_vision_cone := dot > cos_max_angle_of_vision
		if player_is_in_vision_cone:
      # ... do something about it
The dot product is also used for visual flourish. For example, when shading, you can use it to calculate if each pixel on the surface of a 3D model faces towards or away from a light source. It's also used for rim light effects, where you brighten parts of a 3D surface that are perpendicular or face away from the camera direction.