Code: Select all
_terrain = [[CCSprite alloc] initWithFile:@"terrain.png"];
_terrain.anchorPoint = CGPointZero;
_terrain.position = ccp(10, 220);
Code: Select all
_terrain = [[CCSprite alloc] initWithFile:@"terrain.png"];
_terrain.anchorPoint = CGPointZero;
_terrain.position = ccp(10, 220);
Code: Select all
CGSize pixels = [CCDirector sharedDirector].winSizeInPixels;
_target = [[AutoGeometrySprite alloc] initWithFile:@"ball_0.png"];
_target.position = ccp(pixels.width / 2.0f, pixels.height / 2.0f);
_target.space = _space;
[self addChild:_target];
Code: Select all
- (id)initWithFile:(NSString *)filename {
if((self = [super init])) {
CGSize pixels = [CCDirector sharedDirector].winSizeInPixels;
if(self.isStatic)
self.chipmunkBody = [ChipmunkBody staticBody];
else
self.chipmunkBody = [ChipmunkBody bodyWithMass:INFINITY andMoment:INFINITY];
mutableSprite = [CCSprite spriteWithFile:@"ball_5.png"];
mutableSprite.anchorPoint = ccp(0, 0);
renderTexture = [[CCRenderTexture alloc] initWithWidth:pixels.width height:pixels.height pixelFormat:kCCTexture2DPixelFormat_RGBA8888];
renderTexture.position = ccp(0, 0);
renderTexture.sprite.anchorPoint = ccp(0.5, 0.5);
[self addChild:renderTexture z:0];
sampler = [[ChipmunkGLRenderBufferSampler alloc] initWithXSamples:pixels.width ySamples:pixels.height];
sampler.renderBounds = (CGRect){CGPointZero, pixels};
sampler.borderValue = 0.0;
}
return self;
}
Code: Select all
- (void)addHoleAtLocation:(CGPoint)location {
CGPoint loc = [mutableSprite convertToNodeSpace:location];
CCSprite *scorch = [CCSprite spriteWithFile:@"Scorch.png"];
scorch.position = loc;
scorch.rotation = 360.0*CCRANDOM_0_1();
scorch.scale = 1.0;
scorch.blendFunc = (ccBlendFunc){GL_ZERO, GL_SRC_COLOR};
[mutableSprite addChild:scorch];
[self updateTexture];
}
Code: Select all
- (void)updateTexture {
[renderTexture beginWithClear:0 g:1 b:0 a:1];
[mutableSprite visit];
[renderTexture end];
[sampler renderInto:^{
[mutableSprite visit];
}];
for(ChipmunkShape *seg in _chipmunkObjects)
[_space remove:seg];
_chipmunkObjects = [self buildPoly];
}
Code: Select all
- (NSArray *)buildPoly {
cpFloat mass = 0.0;
cpFloat moment = 0.0;
cpFloat downsample = 1;
ChipmunkBody *body = self.chipmunkBody;
NSMutableArray *polyObjects = [@[] mutableCopy];
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, 0, 0);
transform = CGAffineTransformRotate(transform, -body.angle);
transform = CGAffineTransformScale(transform, downsample, downsample);
for(ChipmunkPolyline *polyline in [sampler marchAllWithBorder:TRUE hard:FALSE]){
cpFloat area = polyline.area;
if(area <= 0.0)
continue;
for(ChipmunkPolyline *hull in [polyline toConvexHulls_BETA:1]) {
int count = hull.count - 1;
cpVect transformed[hull.count];
for(int i = 0; i < count; i++)
transformed[i] = CGPointApplyAffineTransform(hull.verts[i], transform);
cpFloat density = 1;
cpFloat m = area * density;
mass += m;
moment += cpMomentForPoly(m, count, transformed, cpvzero);
ChipmunkShape *shape = [ChipmunkPolyShape polyWithBody:body count:count verts:transformed offset:cpvzero];
shape.friction = 1;
shape.elasticity = 1;
shape.layers = COLLISION_LAYERS_TERRAIN;
shape.collisionType = PhysicsIdentifier(COLLISION_TYPE_TERRAIN);
[polyObjects addObject:shape];
[_space add:shape];
}
}
if(!body.isStatic) {
body.mass = mass;
body.moment = moment;
}
return polyObjects;
}
Users browsing this forum: No registered users and 22 guests