views:

180

answers:

2

I have two static (STATIC_MASS) SpaceManager sprites. One is a child of the other - by which I mean that one sort of builds up the other one, but although the child's images shows up in the right place, the child doesn't seem to exists in the chipmunk physics engine, like I would expect. In my case, I have a backboard (rectangular sprite) and a hoop (a circular sprite). Since I might want to move the backboard, I'd like to attach the hoop to backboard so that the hoop automatically moves right along with the backboard.

Here, we see a rotating backboard with attached hoop. It looks OK on the screen, but other objects only bounce off the backboard but pass right through the hoop (in a bad sense of the term). What doesn't my child sprite seem to exist in the physics engine?

  // Add Backboard
  cpShape *shapeRect = [smgr addRectAt:cpvWinCenter mass:STATIC_MASS width:200 height:10 rotation:0.0f ];// We're upgrading this 
  cpCCSprite * cccrsRect = [cpCCSprite spriteWithShape:shapeRect file:@"rect_200x10.png"];
  [self addChild:cccrsRect];

  // Spin the static backboard: http://stackoverflow.com/questions/2691589/how-do-you-make-a-sprite-rotate-in-cocos2d-while-using-spacemanager
  // Make static object update moves in chipmunk
  // Since Backboard is static, and since we're going to move it, it needs to know about spacemanager so its position gets updated inside chipmunk.
  // Setting this would make the smgr recalculate all static shapes positions every step
  //    cccrsRect.integrationDt = smgr.constantDt; 
  //    cccrsRect.spaceManager = smgr;
  // Alternative method: smgr.rehashStaticEveryStep = YES;
  smgr.rehashStaticEveryStep = YES;

  // Spin the backboard
  [cccrsRect runAction:[CCRepeatForever actionWithAction:
                         [CCSequence actions:
                          [CCRotateTo actionWithDuration:2 angle:180],
                          [CCRotateTo actionWithDuration:2 angle:360],
                          nil]

                         ]];


  // Add the hoop
  cpShape *shapeHoop = [smgr addCircleAt:ccp(100,-45) mass:STATIC_MASS radius: 50 ];
  cpCCSprite * cccrsHoop = [cpCCSprite spriteWithShape:shapeHoop file:@"hoop_100x100.png"];
  [cccrsRect addChild:cccrsHoop];

The above code is a bit whacked. The image of the hoop shows up next the board, which is what I want, if I detect collisions, the the collision only happens in the lower left of the screen. Oddly, even though I'm detecting a collision, my object doesn't actually seem to collide, it just passes through it instead of bouncing off of it.

Note: SpaceManager is a toolkit for working with cocos2D-iphone

+1  A: 

The simple answer to this is that it's not really possible. Chipmunk likes everything being in world coordinates and when you start creating parent-child relationships in cocos2d you are setting up a bad mismatch of coordinate systems...

However... I did try to bridge this issue without much success (the problem being rotation I believe) Look at the function "eachShapeAsChildren" in SpaceManager.m. If you set iterateFunc property of SpaceManager to point to this function, you can see what happens....

At some point I may give it another go; in fact I'm feeling more inspired already.

Rob
+2  A: 

The problem is that a parent child relationship doesn't make sense in a physics engine. You can attach multiple shapes to the same body so that when you move the one body it moves several shapes.

slembcke