Game Development Reference
In-Depth Information
To calculate Ob, the following equation is used:
sin(θ) = opposite / hypotenuse = Ob / |V|
Giving:
Ob = |V| sin(θ) = x component
The Dot Product : The dot product gives the angle between two vectors—something you
will need to calculate often when programming AI. Given the two two-dimensional vec-
tors, u and v, the equation looks like the following:
u.v = ux vx + uy uy //Equation (1)
The . (dot) symbol denotes the dot product. Equation (1) doesn't give us an angle
though. I promised an angle, so you'll get one! Here's another way of calculating the dot
product:
u.v = |u| |v| cos(θ)
Rearranging, we get:
cos(θ) = u.v / |u| |v|
Remember, the vertical lines surrounding a vector indicate its magnitude. Now is the time
when you discover one of the useful uses for normalizing vectors. If v and u are both nor-
malized, then the equation simplifies enormously to:
cos(θ) = u.v / 1*1 = u.v
Substituting in the equation from Equation (1) for the right-hand side gives:
cos(θ) = u.v = ux vx + uy uy
This gives us an equation for the angle between the vectors.
Search WWH ::




Custom Search