Skip to content

5. Vector Math

Colin Benaissa edited this page Nov 8, 2024 · 1 revision

Dot Product

The dot product measures the similarity between two vectors in terms of direction. Mathematically, the dot product of vectors A and B (written as A·B) can be calculated as: Screenshot 2024-11-07 at 6 12 32 PM In this equation, theta is the angle between the two vectors, |A| is the magnitude of vector A, and |B| is the magnitude of vector B.

What the dot product tells us: If A·B > 0 the vectors generally point in the same direction. If A·B < 0 they point in the opposite direction. If A·B = 0 they are perpendicular.

Look Vector

The lookVector helps determine the direction an object should face to look at a target. You obtain this vector subtracting an object's position from the target's position and normalizing the result: Screenshot 2024-11-07 at 7 34 10 PM

How these is used in Godot:

Screenshot 2024-11-07 at 7 27 23 PM

Image from Godot Documentation

It is used primarily in facing, or seeing if one entity is facing another entity. In this case, for Zombie A, vector fa is the direction vector (also known as the lookVector) - it is used to then calculate vector fa or the vector between point A and B. Then we take the dot product A·AP and if this value is > 0 that means that Zombie A is facing the player, if it is <=0 the Zombie is not facing the player.

Read more about this: Godot Documentation