Search code examples
cocos2d-xbox2dcollision-detectiongame-physics

detect collision only on top of the object in box2d & cocos2dx


I am creating a game like bounce ball using cocos2d-x and box2d. I have different objects like rectangle, square etc.. I am able to detect collision, but i want to detect collision only on top of the objects. What exactly i want is, when the ball is on the top of the object, then only i want to jump the ball.

But, when the ball is collide on remaining side(bottom or left or right) i don't want to jump the ball.

In touchbegan, i am using the following code to the bounce ball. So every touch it is jumping when it collide with remaining side.

if(_ball->boundingBox().intersectsRect(rect->boundingBox()))
{
b2Vec2 force = b2Vec2(0, 550);
_body->ApplyLinearImpulse(force, _body->GetPosition());
}

Any advice?


Solution

  • I got the solution for my question from the below link.

    http://www.raywenderlich.com/28606/how-to-create-a-breakout-game-with-box2d-and-cocos2d-2-x-tutorial-part-2