Skip to content

Instantly share code, notes, and snippets.

@slembcke
Created November 20, 2010 01:33
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/707519 to your computer and use it in GitHub Desktop.
Save slembcke/707519 to your computer and use it in GitHub Desktop.
@interface ChipmunkSprite : CCSprite {
ChipmunkBody *body;
cpBody *cp_body;
}
@property(retain) ChipmunkBody *body;
@end
@implementation ChipmunkSprite
- (ChipmunkBody *)body {return body;}
-(void)setBody:(ChipmunkBody *)newBody {
ChipmunkBody *oldBody = body;
body = [newBody retain];
cp_body = body.body;
[oldBody release];
}
- (CGPoint)position {return cp_body->p;}
- (void)setPosition:(CGPoint)p {
position_ = cp_body->p = p;
}
- (float)rotation {return -CC_RADIANS_TO_DEGREES(cp_body->a);}
- (void)setRotation:(float)a {
rotation_ = a;
cpBodySetAngle(cp_body, -CC_DEGREES_TO_RADIANS(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];
}
// making a private copy of the struct definition is not ideal, but works to prove the point
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_
};
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment