title: how to use box2d for collision detection in cocos2d-x
body: <p>my two sprites are roll and hero. i want to detect their collision using box2d in cocos2d-x
  i have got both the sprites moving on the screen (roll automatically, hero manually).</p>
  
  <p>all i want to do is call the function intersection();</p>
  
  <p>when the two sprites collide. (sprites are declared globally in the *.h file)</p>
  
  <pre><code>#include "GameScene.h"
  #include "HomeScene.h"
  USING_NS_CC;
  
  CCScene* GameScene::scene()
  {
      // 'scene' is an autorelease object
      CCScene *scene = CCScene::node();
  
      // 'layer' is an autorelease object
      GameScene *layer = GameScene::node();
  
      // add layer as a child to scene
      scene-&gt;addChild(layer);
  
      // return the scene
      return scene;
  }
  
  // on "init" you need to initialize your instance
  bool GameScene::init()
  {
  
      //////////////////////////////
      // 1. super init first
      if ( !CCLayerColor::initWithColor(ccc4(0,0,0,255) ))
      {
          return false;
      }
  
      //////////////////////////////
      // 2. add your codes below...
      count=0;
      life=3;
      CCSize WinSize= CCDirector::sharedDirector()-&gt;getWinSize();
      // background
      CCSprite * bg=CCSprite::spriteWithFile("bg.png");
      bg-&gt;setPosition(CCPointZero);
      bg-&gt;setAnchorPoint(CCPointZero);
      bg-&gt;setScaleX(WinSize.width/460);
      bg-&gt;setScaleY(WinSize.height/325);
      this-&gt;addChild(bg,0,1);
  
      //close button
      CCSprite * back=CCSprite::spriteWithFile("close.png");
      CCSprite * back1=CCSprite::spriteWithFile("closeS.png");
      CCMenuItemSprite *home=CCMenuItemSprite::itemFromNormalSprite(back,back1,this,menu_selector(GameScene::callNext));
      CCMenu *MenuClose=CCMenu::menuWithItem(home);
      MenuClose-&gt;setPosition(ccp(WinSize.width-20,WinSize.height-20));
      this-&gt;addChild(MenuClose,5,1);
  
      //life icon
      CCSprite * rc=CCSprite::spriteWithFile("rc.png");
      rc-&gt;setPosition(ccp(20,WinSize.height-20));
      this-&gt;addChild(rc,5,15);
  
      //score icon
      CCSprite * dp=CCSprite::spriteWithFile("dp.png");
      dp-&gt;setPosition(ccp(55,WinSize.height-20));
      this-&gt;addChild(dp,5,18);
  
      //insert roller at a random place
      roll=CCSprite::spriteWithFile("ball.png");
      int minY = (roll-&gt;getContentSize().height/2)+(WinSize.height/(4.5));
      int maxY = (WinSize.height)-  (roll-&gt;getContentSize().height/2);
      int rangeY = maxY - minY;
  
      srand ( time(NULL) );
  
      int actualY = ( rand() % rangeY ) + minY;
      CCLOG("the actualY is oooooooooooooooooooooooooooooooooooooooooooooooooooooooo %d",actualY);
      roll-&gt;setPosition(ccp(20,actualY));
      this-&gt;addChild(roll,5,8);
  
      // Determine speed of the roller
      int minDuration = 3;
      int maxDuration = 7;
      int rangeDuration = maxDuration - minDuration;
      int actualDuration = ( rand() % rangeDuration)+ minDuration;
  
  /*  
  //creating constraints for the roller
  float maxXX = WinSize.width - roll-&gt;getContentSize().width/2;
  float minXX =roll-&gt;getContentSize().width/2;
  if (roll-&gt;getPosition().x &gt; maxXX)
  {
      roll-&gt;setPosition( ccp(maxXX, roll-&gt;getPosition().y)) ;
  }
  else if (roll-&gt;getPosition().x &lt; minXX)
  {
       roll-&gt;setPosition (ccp(minXX,roll-&gt;getPosition().y));
  }
  
  
  float maxYY = WinSize.height - roll-&gt;getContentSize().height/2;
  float minYY = roll-&gt;getContentSize().height/2;
  
  if (roll-&gt;getPosition().y &gt; maxYY)
  {
     roll-&gt;setPosition ( ccp(roll-&gt;getPosition().x, maxYY));
  }
  else if (getPosition().y &lt; minYY)
  {
     roll-&gt;setPosition( ccp(roll-&gt;getPosition().x, minYY));
  }
  */
  
  
      // Create the rolling action   
      roll-&gt;runAction(CCRepeatForever::actionWithAction((CCSequence*) CCSequence::actions(
                      CCJumpTo::actionWithDuration( actualDuration,ccp(WinSize.width,( rand() % rangeY ) + minY ),( rand() % rangeY ) + minY,4 ),
                      CCHide::action(), 
                      CCMoveTo::actionWithDuration(0,ccp(20,( rand() % rangeY ) + minY)),
                      CCShow::action(), 
                      CCCallFunc::actionWithTarget( this,callfunc_selector(GameScene::scores)),
                      NULL)) );
  
      //displaying scores
      sprintf(score,"%d",count);  
      scr= CCLabelBMFont::labelWithString(score , "arial16.fnt");
      scr-&gt;setPosition(ccp(55,WinSize.height-20));
      scr-&gt;setColor(ccc3(0,0,255));
      this-&gt;addChild(scr,50);
  
      //displaying lifes
      sprintf(lifeLabel,"%d",life);   
      lif= CCLabelBMFont::labelWithString(lifeLabel , "arial16.fnt");
      lif-&gt;setPosition(ccp(20,WinSize.height-20));
      lif-&gt;setColor(ccc3(0,0,255));
      this-&gt;addChild(lif,50);
  
      // insert animated hero
      CCSpriteFrameCache::sharedSpriteFrameCache()-&gt;addSpriteFramesWithFile("boyJog.plist","boyJog.png");
  
      CCMutableArray&lt;CCSpriteFrame*&gt; *proFrame = new CCMutableArray&lt;CCSpriteFrame *&gt;;
      hero = CCSprite::spriteWithSpriteFrameName("boyJog1.png");
      hero-&gt;setPosition(ccp(WinSize.width/2-30,WinSize.height/(4.5)));
      hero-&gt;setAnchorPoint(CCPointZero);
  
      char buff[60];
      for(int i=1;i&lt;=13;i++)
      {
          sprintf(buff,"boyJog%d.png",i);
          proFrame-&gt;addObject( (CCSpriteFrameCache::sharedSpriteFrameCache())-&gt;spriteFrameByName(buff));
      }
  
      CCAnimation *progressAnimation=CCAnimation::animationWithFrames(proFrame, 0.05);
  
      //set animation object here
      CCAnimate *animate=CCAnimate::actionWithAnimation(progressAnimation,false); 
      hero-&gt;runAction(CCRepeatForever::actionWithAction(animate));
      hero-&gt;setScale(.5);
  
      this-&gt;addChild( hero);
  
  
  
  
      //left right button
      CCSprite *moveRight= CCSprite::spriteWithFile("arrowRight.png");
      CCSprite *moveLeft= CCSprite::spriteWithFile("arrowLeft.png");
      CCSprite *moveRightS= CCSprite::spriteWithFile("arrowRight.png");
      CCSprite *moveLeftS= CCSprite::spriteWithFile("arrowLeft.png");
  
      CCMenuItemSprite *right=CCMenuItemSprite::itemFromNormalSprite(moveRight,moveRightS,this,menu_selector(GameScene::heroRight));
      CCMenuItemSprite *left=CCMenuItemSprite::itemFromNormalSprite(moveLeft,moveLeftS,this,menu_selector(GameScene::heroLeft));
      CCMenu *RL =CCMenu::menuWithItems(left,right,NULL);
  
      RL-&gt;alignItemsHorizontallyWithPadding(20);
      RL-&gt;setPosition(ccp(WinSize.width-40,20));
      RL-&gt;setOpacity(150);
      this-&gt;addChild(RL);
  
  
      //jump button
      CCSprite *jump= CCSprite::spriteWithFile("arrowUp.png");
      CCSprite *jumpS= CCSprite::spriteWithFile("arrowUp.png");
      CCMenuItemSprite *mmJump=CCMenuItemSprite::itemFromNormalSprite(jump,jumpS,this,menu_selector(GameScene::heroJump));
      CCMenu *menuJump=CCMenu::menuWithItem(mmJump);
      menuJump-&gt;setPosition(ccp(20,20));
  
      menuJump-&gt;setOpacity(150);
  
      this-&gt;addChild(menuJump,5,12);
  
  
  
  
      //////////////////////////////
      return true;
  }
  
  
  void GameScene::callNext(CCObject *sender)
  {
      CCLOG("game played");
  
      CCDirector::sharedDirector()-&gt;replaceScene(CCTransitionFade::transitionWithDuration(1,(HomeScene::scene())));
      //exit(1);
  }
  
  void GameScene::heroJump(CCObject *sender)
  {
      CCLOG(" U");
      CCSize WinSize= CCDirector::sharedDirector()-&gt;getWinSize();
      CCPoint Pt=hero-&gt;getPosition();
      hero-&gt;runAction(CCJumpTo::actionWithDuration(1,ccp(Pt.x,WinSize.height/(4.5)),50,1));
  }
  void GameScene::heroLeft(CCObject *sender)
  {
      CCLOG(" L");
      CCSize WinSize= CCDirector::sharedDirector()-&gt;getWinSize();
      CCPoint Pt=hero-&gt;getPosition();
      hero-&gt;runAction(CCJumpTo::actionWithDuration(1,ccp(Pt.x-30,WinSize.height/(4.5)),0,1));
      hero-&gt;setFlipX(1);
      //hero-&gt;runAction(CCRepeatForever::actionWithAction(animate));
  
  }
  void GameScene::heroRight(CCObject *sender)
  {
      CCLOG(" R");
      CCSize WinSize= CCDirector::sharedDirector()-&gt;getWinSize();
      CCPoint Pt=hero-&gt;getPosition();
      hero-&gt;runAction(CCJumpTo::actionWithDuration(1,ccp(Pt.x+30,WinSize.height/(4.5)),0,1));
      hero-&gt;setFlipX(0);
      //hero-&gt;runAction(CCRepeatForever::actionWithAction(animate));
  }
  
  void GameScene::scores()
  {
  
      count+=10;
      CCLOG("count ==========================%d",count);
      sprintf(score,"%d",count);
      scr-&gt;setString(score);
  }
  
  void GameScene::intersection()
  {
      CCLOG("collided..........................................................");
  }
  </code></pre>