Friday, March 13, 2009

cpVect vs. CGPoint (Chipmunk vs. CoreGraphics)

Chipmunk has a type called cpVect that is used to define a vector.
The above mentioned structure is defined as:
typedef struct cpVect{
cpFloat x,y;
} cpVect;
On the other hand, CoreGraphics has a similar type called CGPoint that is defined as:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
These 2 types has the same attributes (they are identical) but they aren't interchangeable because they are different types.
You can't do add a CGPoint with a cpVect, neither you can't test whether or not a cpVect is inside a CGRect, etc.

Since cocos2d v0.7.1 you will be able to mix them and use them interchangeably. The solution was a simple hack:
// file cpVect.h

#import <CoreGraphics/CGGeometry.h>
#define cpVect CGPoint
//typedef struct cpVect{
// cpFloat x,y;
//} cpVect;

No comments: