user3616535
user3616535

Reputation: 119

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?

Upvotes: 0

Views: 941

Answers (2)

user3616535
user3616535

Reputation: 119

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

Upvotes: 0

Following steps can solve your problem-

1) CCRect projectileRect = CCRect(float x, float y, float width, float height);
 if(_ball->boundingBox().intersectsRect(projectileRect))  
{

b2Vec2 force = b2Vec2(0, 550);

_body->ApplyLinearImpulse(force, _body->GetPosition());

 }

2) - Make body of an object and then check their collision.

Upvotes: 0

Related Questions