1

我有两个静态 (STATIC_MASS) SpaceManager 精灵。一个是另一个的孩子——我的意思是一种建立另一个,但是虽然孩子的图像出现在正确的地方,孩子似乎并不存在于花栗鼠物理引擎中,就像我一样会期望。就我而言,我有一个篮板(矩形精灵)和一个箍(圆形精灵)。因为我可能想移动篮板,所以我想将篮筐连接到篮板上,以便篮筐自动与篮板一起移动。

在这里,我们看到一个带有附加箍的旋转篮板。它在屏幕上看起来不错,但其他物体只会从篮板上反弹,但会直接穿过篮筐(从术语上看是不好的)。我的孩子精灵似乎不存在于物理引擎中?

  // 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];

上面的代码有点乱。箍的图像显示在板子旁边,这就是我想要的,如果我检测到碰撞,碰撞只发生在屏幕的左下角。奇怪的是,即使我检测到碰撞,我的对象实际上似乎并没有碰撞,它只是穿过它而不是从它反弹。

注:SpaceManager 是一个与 cocos2D-iphone 配合使用的工具包

4

2 回答 2

2

问题是父子关系在物理引擎中没有意义。您可以将多个形状附加到同一个主体,这样当您移动一个主体时,它会移动多个形状。

于 2010-04-30T14:56:20.980 回答
1

对此的简单回答是,这实际上是不可能的。花栗鼠喜欢一切都在世界坐标中,当你开始在 cocos2d 中创建父子关系时,你正在设置坐标系的严重不匹配......

但是...我确实尝试在没有太大成功的情况下解决这个问题(我相信问题是旋转)查看 SpaceManager.m 中的函数“eachShapeAsChildren”。如果你设置 SpaceManager 的 iterateFunc 属性指向这个函数,你可以看到会发生什么......

在某个时候,我可能会再试一次;事实上,我已经感觉更有灵感了。

于 2010-04-22T20:05:26.907 回答