Game Development Reference
In-Depth Information
The vector methods
There is a lot you can do with vectors, and there are many ways to create them and manipu-
late them. And Cocos2d-x comes bundled with helper methods that will take care of most
of the calculations for you. Here are some examples:
• You have a vector, and you want to get its angle—use
getAngle()
• You want the length of a vector—use
getLength()
• You want to subtract two vectors; for example, to reduce the amount of movement
of a sprite by another vector—use
vector1 - vector2
• You want to add two vectors; for example, to increase the amount of movement of
a sprite by another vector—use
vector1 + vector2
• You want to multiply a vector; for example, applying a friction value to the amount
of movement of a sprite—use
vector1 * vector2
• You want the vector that is perpendicular to another (also known as a vector's nor-
mal)—use
getPerp()
or
getRPerp()
• And, most importantly for our game example, you want the dot product of two
vectors—use
dot(vector1, vector2)
Now let me show you how to use these methods in our game example.