2D Vector Dot Product
The dot product (also called the scalar product) is the magnitude of vector b multiplied by the size of the projection of a onto b. The size of the projection is a cosθ (where θ is the angle between the 2 vectors). In other words:
a.b = |a||b|cosθ
A good physical analogy is to think of the boost pads in Mario Kart. If you approach a pad with |a| equal to zero, you'll get no boost i.e. 0 * b * cosθ is equal to 0. If you approach the pad at 90 degrees you also get no boost i.e. a * b * cos PI/2 is equal to 0. If you approach -PI/2 < θ < PI/2 then you'll get a boost. If your incoming speed is |a|, the maximum boost you'll get is |b|, and the actual amount you'll get is cosθ. If you're perfectly lined up, you'll get |a||b|.
C Code:
float VectorDotProduct(ZVector2 v1, ZVector2 v2)
{
return v1.x * v2.x + v1.y * v2.y;
}
Note
To get the fully projected vector, simply multiply the target vector (the
vector we project onto) by the scalar result of the the dot product.