cocos2d精灵碰撞

发布于 2024-12-19 13:50:11 字数 133 浏览 1 评论 0原文

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

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

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

是否可以?

I have an Array of CCSprites that being displayed all at once.
Every sprite has a movement path, a movement path is a random point on screen.

All the sprites are moving all at once to random points on screen.

What I want to do is to detect collision between the sprites and then change their movement path.

Is it possible?

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

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

发布评论

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

评论(3

风苍溪 2024-12-26 13:50:11

迭代数组中的每个 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 的角度来看,然后从精灵 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];

Iterate through every CCSprite in your array (call it A), and for every iteration iterate again through every CCSprite in the array (excluding A itself of course) (call this one B). Now, use CGRectIntersectsRect along with boundingBox to find a collision between them. It goes something like this:

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

Edit: But of course, it is possible that if two sprites collide, the "collision event" will occur twice (first from the point of view of sprite A, and then from the point of view of sprite B).

If you only want the collision event to trigger once every check, you will need to memorize the pairs so that you can ignore collisions that already did happen on that check.

There are countless ways you could check for that, but here's an example (updated code):

Edited again:

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];
三岁铭 2024-12-26 13:50:11

看看这个SO答案

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

Have a look at this S.O. answers.

You can do simple collision detection using CGRectIntersectsRect and the node boundingBox. If you need more advanced features, have a look at a physics engine like chipmunk or Box2D.

梦明 2024-12-26 13:50:11

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

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

Ray Wenderlich has written a good tutorial about using Box2D just for collision detection, if you're interested in going that way. http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone

Check first that your sprites can be approximated by rectangles. If so then @Omega's answer was great. If they can't be, perhaps because they contain a lot of transparency or for some other reason, you might need to approximate your sprites with polys and work with those.

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