2

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?

  • possible duplicate of [how to use cocos2d sprite collision?](http://stackoverflow.com/questions/1338569/how-to-use-cocos2d-sprite-collision) – sergio Dec 02 '11 at 13:55

3 Answers3

2

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];
Saturn
  • 17,888
  • 49
  • 145
  • 271
  • 2
    Note: this only works if none of the sprites are rotated. Otherwise the boundingBox may be significantly larger than the image rect. – CodeSmile Dec 02 '11 at 19:27
  • 1
    I have another question: if the two sprites crossing each other, the code in the 'collision block' is being called several times, how can I call it only once for the current collision pair ? –  Dec 02 '11 at 19:30
  • @Omega thank you for your answer, but pairs array has no objects so it'll never run 'for (NSArray *pair in pairs)' –  Dec 03 '11 at 07:35
  • My bad. I think it is fixed now. – Saturn Dec 03 '11 at 13:15
  • @Omega I appreciate your patience but the 'Collision block' is being called several times during two sprites crossing each other –  Dec 03 '11 at 16:20
  • What are you doing when the collision happens? – Saturn Dec 03 '11 at 16:24
  • I want the sprites to move to their opposite sides like two bowling ball that colliding –  Dec 03 '11 at 16:50
  • Have you achieved such already? (I mean, the movement path calculation etc) – Saturn Dec 03 '11 at 17:22
  • Not yet, the movement path might be changed, at the moment I want to detect collision between the two sprites and call the code only once at the same collision –  Dec 03 '11 at 18:15
1

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.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
0

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.

Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44