使用 SpaceManager 时,如何使静态精灵成为 cocos2D 中另一个精灵的子对象

发布于 2024-08-30 05:54:58 字数 1910 浏览 6 评论 0原文

我有两个静态 (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 的工具包

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城孤痞 2024-09-06 05:54:58

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

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.

世态炎凉 2024-09-06 05:54:58

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

但是......我确实尝试弥合这个问题,但没有取得太大成功(问题是旋转我相信)看看SpaceManager.m中的函数“eachShapeAsChildren”。如果您将 SpaceManager 的 iterateFunc 属性设置为指向此函数,您可以看到会发生什么......

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文