Search code examples
cocos2d-iphonegame-physicschipmunk

cpShapes not colliding?


I have been trying to figure this out for a couple of days now but I haven't been able to figure out what my problem is.

So I am using cocos2d with chipmunk-spaceManager to create my game. So what I am doing is creating 4 cpShapes that are attached to one cpBody and attaching them to a CCSprite. Here is my code.

- (id) helmetInit:(Game*)game {

    cpShape *helmet_1;
    cpShape *helmet_2;
    cpShape *helmet_3;

    cpShape *reference = [game.spaceManager addCircleAt:cpvzero mass:STATIC_MASS radius:2];

    helmet_1 = [game.spaceManager addCircleToBody:reference->body radius:20 offset:cpv(-5, 2)];

    helmet_2 = [game.spaceManager addCircleToBody:reference->body radius:8 offset:cpv(16, -14)];

    helmet_3 = [game.spaceManager addCircleToBody:reference->body radius:8 offset:cpv(8, -14)];

    reference->group    = 1;
//    helmet_1->group     = 1;
//    helmet_2->group     = 1;
//    helmet_3->group     = 1;


    [self initWithFile:@"Helmet.png"];
    [self setShape:reference];
    //[self setBody:reference->body];

    self.spaceManager = game.spaceManager;
    self.autoFreeShapeAndBody = YES;

    gameScreenSize = game.contentSize;

    return self;
}

So my problem is the only time that I get any collisions is if one of my other shapes in my game collide with the (reference shape) which is the shape that is created with the body that all the shapes are sharing. If I understand the way chipmunk works, doesn't every shape have collisions, because the other shapes are like there not even there. Other shapes pass right through them until they collide with the reference shape which is the only shape that has any collisions right now. Am I doing something wrong or do I not understand the way chipmunk works?

Here is a screenshot of the 4 shapes that I have created.enter image description here


Solution

  • Ok, with a lot of frustration I have finally figured out my problem. I was moving a static shape after the chipmunk space had begun simulating. So I fixed it by setting smgr.rehashStaticEveryStep = YES;