Search code examples
box2dcocos2d-xcollision-detectionfixturesphysics-engine

Auto jump in Box2d Collsion detection


i have auto player (Computer ) in my bike race game. whenever some hard objects is there like traffic light, road blocker, speed braker. it nee dto jump automatically.

For that , in poarticular object class am checking which fixture is colliding with the object. if it matches with my Auto player body. then am giving jump command.

so jump is not smoothly . because after collided its jumping. i need to jump before the object reaches so jump will look good on screen.

For that how to do in cocos2dx box2d ?

My jump code in road block class

bool Block::HandleBeginContact(b2Fixture *fixtureA,b2Fixture *fixtureB)
{

    b2Body *bodyA =  fixtureA->GetBody();
    b2Body *bodyB =  fixtureB->GetBody();

    b2Fixture *myFixture = RoadBlock->GetFixtureList(); //am getting all list from Road block body
    if(myFixture == fixtureA  || myFixture == fixtureB)
    {
       //am collided with Road block
        if(bodyB == AIRider::g_MainBody || bodyA == AIRider::g_MainBody )
        {            
           AIBike::Direction.y = 50.0f;
        }
   }
}

Solution

  • Create your own rectangle object adjust the box size as u need

    box.SetAsBox(2.0f, 2.5f);
    

    . and place it in before your hard objects like traffic light, road blocker, speed braker.

    thats it there you can check collison and jump. make sure that sensor is turned on.

    bool yourinvisiblebox2dobject::HandleBeginContact(b2Fixture *fixtureA,b2Fixture *fixtureB)
    {
    
        b2Body *bodyA =  fixtureA->GetBody();
        b2Body *bodyB =  fixtureB->GetBody();
    
        b2Fixture *myFixture = RoadBlock->GetFixtureList(); //am getting all list from Road block body
        if(myFixture == fixtureA  || myFixture == fixtureB)
        {
           //am collided with Road block
            if(bodyB == AIRider::g_MainBody || bodyA == AIRider::g_MainBody )
            {            
               AIBike::Direction.y = 50.0f;
            }
       }
    }