Game Designer & Developer


Game Dev

Dot Product

A⃗⋅B⃗=∥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 A⃗⋅B⃗\vec{A}\cdot\vec{B}, pair up corresponding coordinates, multiply pairs, and add together.
[1,2]⋅[3,4]=1⋅3+2⋅4=3+8=11[1, 2]\cdot[3, 4] = 1·3+2·4 = 3+8 = 11

To visualise A⃗⋅B⃗\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 ∥A⃗2∥×∥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.