Dot and Cross Products
There are two ways to multiply two vectors. The dot product gives a number that measures how much they point the same way. The cross product gives a new vector perpendicular to both. Between them they handle angles, projections, lighting, field-of-view checks and surface normals.
The dot product
For and :
The two forms are equal, which gives a way to find the angle between any two vectors: . The sign of the dot product alone tells you a lot:
| Angle | Meaning | |
|---|---|---|
| less than 90° | roughly the same direction | |
| exactly 90° | perpendicular (orthogonal) | |
| more than 90° | roughly opposite directions |
Two more facts: , and the projection of onto , the part of that lies along , is . Its signed length is the scalar projection (the component of along ).
import numpy as np
a = np.array([2.0, 1.0, -2.0])
b = np.array([3.0, 4.0, 0.0])
dot = np.dot(a, b)
cos_t = dot / (np.linalg.norm(a) * np.linalg.norm(b))
print("a . b =", dot)
print("angle (deg) =", round(float(np.degrees(np.arccos(cos_t))), 2))
proj = dot / np.dot(a, a) * a
print("proj of b on a =", np.round(proj, 4))
print("rest of b =", np.round(b - proj, 4), " (dot with a:", round(float(np.dot(b - proj, a)), 10), ")")a . b = 10.0
angle (deg) = 48.19
proj of b on a = [ 2.2222 1.1111 -2.2222]
rest of b = [0.7778 2.8889 2.2222] (dot with a: 0.0 )
Projection splits into a part along and a part perpendicular to . The check at the end shows that the leftover part really is perpendicular to .
The dot product in games
Diffuse lighting. In the Lambert model a surface is brightest when it faces the light. With unit normal and unit vector pointing toward the light, the brightness is , which is clamped at zero for surfaces facing away.
Field of view. An enemy with unit forward vector can see a target if the angle between and the unit direction to the target is at most half the view cone. Comparing cosines avoids calling acos at all: the target is visible when .
import numpy as np
def unit(v):
return v / np.linalg.norm(v)
# Lambert lighting for three surface normals
light = unit(np.array([1.0, 2.0, 2.0]))
for n in [np.array([0.0, 1.0, 0.0]), unit(np.array([1.0, 1.0, 1.0])), np.array([0.0, -1.0, 0.0])]:
print("normal", np.round(n, 3), "brightness", round(max(0.0, float(np.dot(n, light))), 3))
# Field-of-view check: 90° cone, guard at the origin facing +x
forward, half_fov_cos = np.array([1.0, 0.0]), np.cos(np.radians(45))
for target in [np.array([5.0, 2.0]), np.array([3.0, 4.0]), np.array([-1.0, 0.5])]:
d = unit(target)
print(target, "visible" if np.dot(forward, d) >= half_fov_cos else "hidden")normal [0. 1. 0.] brightness 0.667
normal [0.577 0.577 0.577] brightness 0.962
normal [ 0. -1. 0.] brightness 0.0
[5. 2.] visible
[3. 4.] hidden
[-1. 0.5] hidden
The target at (3, 4) is 53° off the forward axis, just outside the 45° half-angle. More in Game Math's Dot Product and Angle Between Vectors. The same formula, called cosine similarity, compares text embeddings in vector search.
The cross product
The cross product exists only in 3D. It is easiest to remember as a determinant:
Its properties:
- is perpendicular to both and . Its direction follows the right-hand rule: curl the fingers of your right hand from toward , and your thumb points along .
- , the area of the parallelogram spanned by and . Half of it is the area of the triangle.
- It is anti-commutative: . Swapping the order flips the normal.
- exactly when and are parallel.
Normals from triangles
A 3D mesh is a list of triangles. The normal of triangle is , normalised. Vertex order matters: counter-clockwise order (seen from outside) makes the normal point outward, and that is how engines decide which side of a triangle is the front for back-face culling. See Cross Product.
import numpy as np
P = np.array([0.0, 0.0, 0.0])
Q = np.array([4.0, 0.0, 0.0])
R = np.array([0.0, 3.0, 0.0])
n = np.cross(Q - P, R - P)
print("cross :", n)
print("unit n :", n / np.linalg.norm(n))
print("area :", np.linalg.norm(n) / 2)
print("reversed :", np.cross(R - P, Q - P))
print("checks :", np.dot(n, Q - P), np.dot(n, R - P))cross : [ 0. 0. 12.]
unit n : [0. 0. 1.]
area : 6.0
reversed : [ 0. 0. -12.]
checks : 0.0 0.0
The triangle is a 3-4-5 right triangle in the xy-plane, so its area is , and its normal points straight up the z-axis.
Exercises
Exercise 1: Perpendicular or not?
Which pairs are perpendicular? (a) and , (b) and , (c) and .
Answer
(a) and (b) are perpendicular; (c) is not.
Solution
Exercise 2: Angle between vectors
Find the angle between and .
Answer
60°.
Solution
Exercise 3: Triangle area
Find the area of the triangle with corners , , .
Answer
.
Solution
import numpy as np
A, B, C = np.array([1, 0, 0]), np.array([0, 2, 0]), np.array([0, 0, 2])
n = np.cross(B - A, C - A)
print(n, round(float(np.linalg.norm(n) / 2), 3))[4 2 2] 2.449
Exercise 4: Is the target behind me? (code)
Write is_behind(forward, to_target) that returns True when the target lies more than 90° away from the forward direction. Test it with forward and targets and .
Answer
is behind (True); is not (False).
Solution
"More than 90°" means a negative dot product. No normalisation is needed because only the sign matters.
import numpy as np
def is_behind(forward, to_target):
return bool(np.dot(forward, to_target) < 0)
f = np.array([0.0, 1.0])
print(is_behind(f, np.array([2.0, -1.0])), is_behind(f, np.array([-3.0, 0.5])))True False
Test yourself
If for two non-zero vectors, what is the angle between them?
- 0°
- 45°
- 90°
- 180°
What does measure?
- The angle between and
- The area of the parallelogram spanned by and
- The length of plus
- The projection of on
A triangle's normal is computed as . What happens if you list the vertices in the opposite order?
- Nothing changes
- The normal gets twice as long
- The normal points the opposite way
- The cross product becomes zero