Game Designer & Developer


Game Dev

Dot Product

AB=A×B×cosθ\vec{A}\cdot\vec{B} = \|\vec{A}\|\times\|\vec{B}\|\times\cos\theta
A[x1,y1]B[x2,y2]=x1×x2+y1×y2\vec{A}[x_{_1}, y_{_1}]\cdot\vec{B}[x_{_2}, y_{_2}] = x_{_1} \times x_{_2} + y_{_1} \times y_{_2}

To calculate AB\vec{A}\cdot\vec{B}, pair up corresponding coordinates, multiply pairs, and add together.
[1,2][3,4]=13+24=3+8=11[1, 2]\cdot[3, 4] = 1·3+2·4 = 3+8 = 11

To visualise AB\vec{A}\cdot\vec{B}, project one onto the other and multiply their lengths together i.e. project A\vec{A} onto B\vec{B} and multiply A2×B\|\vec{A}_2\|\times\|\vec{B}\|. If they point in a similar direction the value will be positive, if they are perpendicular to each other it will be zero, and if they are pointing away from each other it must be negative.

For normalised vectors the dot product can be seen as a measure of similarity because, for two normals, the magnitudes will both be 11, meaning
A^B^=A^×B^×cosθ\widehat{\vec{A}}\cdot\widehat{\vec{B}}=\|\widehat{\vec{A}}\|\times\|\widehat{\vec{B}}\|\times\cos\theta
will be
A^B^=1×1×cosθ\widehat{\vec{A}}\cdot\widehat{\vec{B}}=1\times1\times\cos\theta
which means
A^B^=cosθ\widehat{\vec{A}}\cdot\widehat{\vec{B}}=\cos\theta
and cosθ=1\cos\theta=1 at θ=0°\theta=0\degree,
decreases to cosθ=0\cos\theta=0 at θ=90°\theta=90\degree,
continues to cosθ=1\cos\theta=-1 at θ=180°\theta=180\degree,
heads back to cosθ=0\cos\theta=0 at θ=270°\theta=270\degree (90°-90\degree),
and finally back up to cosθ=1\cos\theta=1 as θ\theta approaches 360°360\degree which is also 0°0\degree.