【问题标题】:cocos2d sprite collisioncocos2d精灵碰撞
【发布时间】:2011-12-04 18:37:01
【问题描述】:

我有一个 CCSprite 数组,它们同时显示。 每个精灵都有一个移动路径,移动路径是屏幕上的一个随机点。

所有精灵都同时移动到屏幕上的随机点。

我想做的是检测精灵之间的碰撞,然后改变它们的运动路径。

有可能吗?

【问题讨论】:

标签: iphone objective-c cocos2d-iphone collision ccsprite


【解决方案1】:

遍历数组中的每个CCSprite(称为A),并为每次迭代再次遍历数组中的每个CCSprite(不包括A 本身)(称其为 B)。现在,使用CGRectIntersectsRectboundingBox 来查找它们之间的冲突。它是这样的:

        for (CCSprite *first in mySprites) {
            for (CCSprite *second in mySprites) {
                if (first != second) {
                    if (CGRectIntersectsRect([first boundingBox], [second boundingBox])) {
                        // COLLISION! Do something here.
                    }
                }
            }
        }

编辑:但是当然,如​​果两个精灵发生碰撞,“碰撞事件”会发生两次(首先从精灵A的角度来看,然后从精灵的角度来看)精灵 B) 的视图。

如果您只希望每次检查都触发一次碰撞事件,则需要记住这些对,以便您可以忽略该检查中已经发生的碰撞。

有无数种方法可以检查,但这里有一个示例(更新的代码):

再次编辑

NSMutableArray *pairs = [[NSMutableArray alloc]init];
    bool collision;
    for (CCSprite *first in mySprites) {
        for (CCSprite *second in mySprites) {
            if (first != second) {
                if (CGRectIntersectsRect([first boundingBox], [second boundingBox])) {
                    collision = NO;
                    // A collision has been found.
                    if ([pairs count] == 0) {
                        collision = YES;
                    }else{
                        for (NSArray *pair in pairs) {
                            if ([pair containsObject:first] && [pair containsObject:second]) {
                                // There is already a pair with those two objects! Ignore collision...
                            }else{
                                // There are no pairs with those two objects! Add to pairs...
                                [pairs addObject:[NSArray arrayWithObjects:first,second,nil]];
                                collision = YES;
                            }
                        }
                    }
                    if (collision) {
                        // PUT HERE YOUR COLLISION CODE.
                    }
                }
            }
        }
    }
    [pairs release];

【讨论】:

  • 注意:这仅在没有旋转任何精灵时才有效。否则 boundingBox 可能会比图像 rect 大很多。
  • 我还有一个问题:如果两个精灵相互交叉,'碰撞块'中的代码被多次调用,我如何才能为当前的碰撞对只调用一次?跨度>
  • @Omega 谢谢你的回答,但是对数组没有对象,所以它永远不会运行'for (NSArray *pair in pairs)'
  • 我的错。我认为它现在已修复。
  • @Omega 感谢您的耐心等待,但在两个精灵相互交叉时,“碰撞块”被多次调用
【解决方案2】:

看看这个S.O. answers

您可以使用CGRectIntersectsRect 和节点boundingBox 进行简单的碰撞检测。如果您需要更高级的功能,请查看 chipmunkBox2D 之类的物理引擎。

【讨论】:

    【解决方案3】:

    Ray Wenderlich 写了一篇关于使用 Box2D 进行碰撞检测的很好的教程,如果您有兴趣这样做的话。 http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone

    首先检查您的精灵是否可以近似为矩形。如果是这样,那么@Omega 的回答很棒。如果它们不能,可能是因为它们包含很多透明度或出于其他原因,您可能需要使用多边形来近似您的精灵并使用它们。

    【讨论】:

      猜你喜欢
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多