0
votes

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?

2
compare the two body's position.y perhaps?LearnCocos2D

2 Answers

0
votes

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.