Search code examples
c++cocos2d-xcocos2d-x-3.0

Cocs2d-x 3 Collision


To start off I am just getting started with game programming so please take it easy on me. I have decided to go with cocos2d-x 3 using c++ so that I can build it for multiple platforms with one code base. I have built a little game with where collision is working but I don't think I am doing it right and most of the tutorials are for old versions and don't quite explain the collision part to my understanding. I have 2 problems:

1.) The objects are hitting in a rectangle but I need them to hit in a circle.

  • I have tried using cocostudio but I cant seem to quite figure out how to get the rectangles to be a circle. I have been able to get the animation running but when they collide you can tell it is in the rectangle and not the actual object.
  • I have also tried using physics editor but with that it requires it to be using Box2D which is ok but that comes to my second point. Also with physics editor I can't run the animation I wanted to without creating a separate resource (I am assuming this).

2.) Detecting collisions.

  • I am not quite sure how to do this correctly. I have seen that you can do it with Box2D but I am just using something like the code below
  • Now from what I have read you can do it with box2d and setup so that they collide only with certain objects but its all confusing to me.

Here is the code:

CCRect ballRect = ball->getBoundingBox();
CCRect enemyRect = enemy->getBoundingBox();
if(ballRect.intersectsRect(enemyRect)){
  //collision
}

If someone could please help me out that would be great. I would be open to some tools to use or tutorials that I could follow along with that explain how all this works. Also the other odd thing that I found is I had to pull in code for physics editor from here https://github.com/CodeAndWeb/PhysicsEditor-Cocos2d-x-Box2d in order to get the "fixtures" in Box2D to load from the plist that physics editor exported. I don't know if that is right I would have thought Box2D could load it itself (I could be wrong here as well because I just don't know). Any help on the matter at all is greatly appreciated. Just trying to get this setup correctly and everything else has been a breeze. Just can't seem to figure out how to correctly define the collision rectangles and the correct way to detect the collisions.


Solution

  • Read though these, they may help.

    1. It has downloadable project file for cocos2d-x. Version may be old. http://www.gmtdev.com/blog/2011/08/19/how-to-use-box2d-for-just-collision-detection-with-cocos2d-x/10

    2. This is is cocos2d, which is obj-c. But he has discussed vertex helper tool that helps to create more precise collision parameter.

    http://www.raywenderlich.com/606/box2d-tutorial-for-ios-how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone

    If you only need a circle, like a perfect circle ? you can use box2d for collision and define your body as b2Circle. Which will give you the perfect callback.

    You can look at test-cpp project that comes with every cocos2d-x download for latest syntax and basic tutorial. They have test for box2d as well.