Search code examples
c#c++xnacocos2d-x

Vector2 equivalent in c++?


Hi I'm porting my game from c#/xna to c++/cocos2d-x and I was trying to find an equivalent to Vector2. I did find a couple such as b2Vec2, I'm just not sure which one I should use because I need it to be cross platform and not bound to a specific operating system.


Solution

  • The class you're looking for is the poorly-named* CCPoint class. Cocos2d-x is a direct port of the API of Cocos2d, which was designed for Objective C. And since Objective C doesn't have operator overloading, neither does Cocos2d-x, even though it's written in C++ where operator overloading exists. So you have to use free-functions to do vector operations.

    *: In case you're wondering why it is poorly named, "vector" covers the concept of "point" (a position within a space) and "direction" (a direction in a space). A vector can be either of these. Adding vectors has a specific meaning geometrically, as does subtracting them, multiplying them, taking the length, etc. Adding a point to another point is nonsense, geometrically. You can only add vector directions, not positions. You can transform positions into another space, but you don't do that by adding one position to another.