Skip to content

Instantly share code, notes, and snippets.

@slembcke
Created November 22, 2010 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slembcke/710162 to your computer and use it in GitHub Desktop.
Save slembcke/710162 to your computer and use it in GitHub Desktop.
#if COCOS2D_VERSION < 0x00009904 || 0x00009904 < COCOS2D_VERSION
#warning ChipmunkSprite has not been tested against this version of Cocos2D.
// ChipmunkSprite overrides a private method (getTransformValues) of CCSprite.
// This could break ChipmunkSprite if Cocos2D changes the method changes in a different version.
// ChipmunkSprite also overrides other CCNode and CCSprite methods to make sure the position and rotation is synchronized.
#endif
@implementation ChipmunkSprite
- (ChipmunkBody *)body {return body;}
-(void)setBody:(ChipmunkBody *)newBody {
ChipmunkBody *oldBody = body;
body = [newBody retain];
cp_body = body.body;
[oldBody release];
}
-(id) addChild: (CCNode*) child z:(int)z tag:(int) aTag {
NSAssert(cp_body != NULL, @"ChipmunkSprite.body has not been set.");
return [super addChild:child z:z tag:aTag];
}
- (CGPoint)position {return cp_body->p;}
- (void)setPosition:(CGPoint)p {
NSAssert(cp_body != NULL, @"ChipmunkSprite.body has not been set.");
cpBodySetPos(cp_body, p);
[super setPosition:p];
}
- (float)rotation {return -CC_RADIANS_TO_DEGREES(cp_body->a);}
- (void)setRotation:(float)a {
NSAssert(cp_body != NULL, @"ChipmunkSprite.body has not been set.");
cpBodySetAngle(cp_body, -CC_DEGREES_TO_RADIANS(a));
[super setRotation:a];
}
-(BOOL)dirty {return TRUE;}
static inline void update(ChipmunkSprite *sprite){
assert(sprite->cp_body);
sprite->rotation_ = CC_RADIANS_TO_DEGREES(-sprite->cp_body->a);
sprite->position_ = sprite->cp_body->p;
}
-(void)updateTransform {
update(self);
[super updateTransform];
}
-(void)visit {
update(self);
[super visit];
}
-(void) transform {
update(self);
[super transform];
}
- (CGAffineTransform)nodeToParentTransform {
update(self);
return [super nodeToParentTransform];
}
struct transformValues_ {
CGPoint pos; // position x and y
CGPoint scale; // scale x and y
float rotation;
CGPoint ap; // anchor point in pixels
};
-(struct transformValues_)getTransformValues {
return (struct transformValues_){
cp_body->p,
{scaleX_, scaleY_},
-CC_RADIANS_TO_DEGREES(cp_body->a),
anchorPointInPixels_
};
}
- (void) dealloc
{
[body release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment