Advanced collision detection

Learn how to detect collission between non-rectangular bodies that move and rotate. Here is a sample project which you can download. Before watching this tutorial you might want to watch: Simple collision detection and also The magic of CGPaths. Don’t forget to take a look at all the other tutorials.


This entry was posted in Cocos2d and tagged , , . Bookmark the permalink.

11 Responses to Advanced collision detection

  1. IronBlade says:

    Hello again … I’m trying to apply this example but with another sprites in shape of human so I open Photoshop to get the path (x,y) of my sprite but when draws it shows upside down hahaha … my other problem is when I try to show point using the +GAP from batchNode Sprite shows error @”CCSprite is not using the same texture id” O_o!!!

    I would appreciate any idea, thanks :)

  2. Cryv says:

    Hi Bob, I really like your tutorials but now i have a problem that I’m not able to solve.
    I implemented your collision detection in my game but it doesn’t work and i dont know why. I’ve been trying for days to solve this. Maybe u can help me.

    Thank you very much for your help

  3. Abhi says:


    I am a newbie in game development, i want to use pixel perfect collision method to detect collision between two animated sprites(i have used sprite-sheet for animation). When i use normal sprites, collision is working fine but when i use animated sprites, my app gets crashed(i also used bounding box but it doesn’t work good with animation). Can you suggest me how to solve this issue? or please provide any other collision method to detect animated sprites.

  4. Arpit says:

    Is there any specific reason not to inherit Actor from CCsprite and instead take a ccsprite instance variable in the class itself ?

  5. Uwe says:

    Hi, how to you connect each single polygon with each single animation? I mean: I have character which is a running person. Now how can I add a parh for each animation frane? Do you have any sourceexample for that? Thanks!!

    • Bob says:

      Hi Uwe, my method is mainly used for rigid bodies. If you have a frame by frame animation like a running person, then you must create a CGPath for each animation frame. If theere is a simple mathematical formula for how CGPath changes with each frame then you could use that, otherwise you have to manually find the corner points of the CGPath. (or using some app like Physics Editor).

  6. Uwe says:

    Hi, looks very interesting!

    Do you see a way to use it with animated CCSprites?


    • Bob says:

      Yes Uwe, there should not be any problems using this with animated CCSprites, if the sprites are treated as rigid bodies, and the animation consists of a moves, scales and rotations. . That is the whole idea! I have just added an invisible path around the sprites, that follows the sprites just as the hair follows the head. You can treat the sprites exactly as you would without having the path there. It goes without saying that the green points were there just for testing purposes, you would not have them in the finished app.

      Now if the animation is a frame by frame animation, you will need to make a CGPath for each animation frame.

      Thanks Bob

      • thom says:

        I guess the question is, since the points for each sprite is “hard coded” then if you had an animation then those points may no longer be “valid” right?

        • Bob says:

          Hi thorn,

          Those points stick to the body and will move with the body as long as the body translates, rotates or scales. You can for instance make an Asteroids game and it will all work. Take a look at the tutorials “Magic of Node Speces” and “The Magic of CGPaths”. But if you´re after a frame by frame animation of a body that is not rigid, then of course you need to move the points so they reflect the new CGPath of the body.

Leave a Reply