2
votes

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?

3
possible duplicate of how to use cocos2d sprite collision?sergio

3 Answers

2
votes

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];
1
votes

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.

0
votes

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.