Okay, I have tossed out my complex scenario for a basic box and circle scenario just so I can learn this. Here is my box class:
Code: Select all
-(id)initAtPos:(cpVect)pos {
//************************************************************************
self.position = pos;
hitPoints = 1000;
chassis = [ChipmunkCocosBody bodyWithMass:10.0 andMoment:cpMomentForBox(10.0, 100.0, 30.0)];
ChipmunkPolyShape* mainShape = [ChipmunkPolyShape boxWithBody:chassis width:100.0 height:30.0];
// assign body position to GameObject
chassis.pos = self.position;
mainShape.collisionType = [Box class];
mainShape.group = self;
mainShape.layers = 1;
chipmunkObjects = [[NSArray alloc] initWithObjects:
chassis, mainShape, nil];
return self;
}
+(Box*)boxAtPos:(cpVect)pos {
return [[Box alloc] initAtPos:pos];
}
And my circle class:
Code: Select all
-(id)initWithName:(NSString*)name atPos:(cpVect)pos {
// set objectType to inert
self.objectType = kTypeInert;
// load sprite frame cache
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sprites.plist"];
// load sprite from
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:name];
// add sprite to array
objectSprites = [[NSArray alloc] initWithObjects:sprite, nil];
body = [ChipmunkCocosBody bodyWithMass:5.0 andMoment:cpMomentForCircle(5.0, 12, 12, cpvzero)];
ChipmunkCircleShape* rock = [ChipmunkCircleShape circleWithBody:body radius:12 offset:cpvzero];
// set position of body to provided position
body.pos = pos;
// assign body position to GameObject
self.position = body.pos;
rock.collisionType = [Rock class];
rock.group = self;
rock.layers = 1;
// load sprites array to synced nodes
body.syncedNodes = objectSprites;
// load chipmunk objects to chipmunkObjects array
chipmunkObjects = [[NSArray alloc] initWithObjects:body, rock, nil];
return self;
}
+(Rock*)rockWithName:(NSString*)name atPos:(cpVect)pos {
return [[Rock alloc] initWithName:name atPos:pos];
}
Here are my collision methods (they don't do anything except tell me they are being executed):
Code: Select all
static inline void applyDamage(Box* box, cpArbiter* arb, GameLayer* self) {
if(cpArbiterIsFirstContact(arb)){
// Divide the impulse by the timestep to get the collision force.
cpFloat impact = cpvlength(cpArbiterTotalImpulse(arb))/FIXED_TIMESTEP;
CCLOG(@"Impact Value: %f",impact);
}
}
-(void)begin:(cpArbiter*)arbiter space:(ChipmunkSpace*)space {
CCLOG(@"COLLISION");
CHIPMUNK_ARBITER_GET_SHAPES(arbiter, box, rock);
applyDamage(box.data, arbiter, self);
}
-(void)addCollisionHandlers {
[space addCollisionHandler:self typeA:[SimpleCar class] typeB:[Rock class] begin:@selector(begin:space:)
preSolve:nil postSolve:nil separate:nil];
}
Now, currently, dropping a circle on the box results in a collision call (with impact of 0.000), but the circle drops right through the box. If I don't call the handlers (comment out the [self addCollisionHandlers]), the circles bounce off the box as they should. Again, I am trying to model this off of the Angry Chipmunks demo, but for the life of me, I can't get it to work. What am I missing?