How can I test sprite collision in cocos2d?

12,242

Solution 1

As Eric pointed out, CGRectIntersectsRect is the method to test two bounding rects for overlapping. Use the boundingBox method of the CCNode classes to get the correct bounding box for each sprite (or other node).

See my answer here: Collision Detection in Cocos2d game?

Solution 2

I would look into b2ContactListener

You can do some searches on that, and you'll easily get some results

Solution 3

You can also perform very simple collision detecting by checking if two rectangles intersect. You can use the CGRectIntersectsRect function to do this. Basically, both rects are based on the position and dimensions of sprites you want to check for collisions. If CGRectIntersectsRect returns true, you know a collision occurred.

More info here: [http://icodeblog.com/2009/02/18/iphone-game-programming-tutorial-part-2-user-interaction-simple-ai-game-logic/][1]

Share:
12,242
user162304
Author by

user162304

Updated on June 04, 2022

Comments

  • user162304
    user162304 almost 2 years

    How do I start to implement a class for sprite collision?

  • LearnCocos2D
    LearnCocos2D over 13 years
    for "simple" collision detection you don't need a physics engine, that's overkill.
  • Michael Wildermuth
    Michael Wildermuth about 12 years
    I believe this is apart of box2d which is apart of Cocos2d. Might help with your searches.
  • Daniel Benedykt
    Daniel Benedykt about 12 years
    box2d is a physics engine. Can be too much for just collision detection