First off, I'm still learning box2d and c++. I also have only 1 month experience with cocos2d and objective-c so I'm still getting lost and confused often. Anyway, I am trying to make a ping pong like game using box2d. Like said game, there's a ball and 2 paddles. One of the paddles is moved by the player using accelerometer. I have this working fine. On the other hand, I want the 2nd paddle to act like some kind of wall that would catch the ball and hit it back to the player.
My problem is the ball is bouncing with the use of gravity and force so I'm not sure how I can tell the 2nd paddle to move where the ball is headed to catch it. Currently, my code looks like this:
UPDATE: Someone suggested that I match the x-coordintates of the tPaddle and the ball so that's what I did. This works okay but I was hoping that I can make the paddle move only when the ball is close to hitting the top in order to catch it. I tried something like this:
The problem with this however is that when the ball's y-coordinate is greater than 300, it gets stuck on top of the paddle and no longer bounces. How can I make the paddle move only when the ball is close to it then keep the ball bouncing?
I've found some sort of workaround for this, it's not pretty but it's not that ugly either so I think I'll settle with this "fix" for now. Since I call the moveObj is scheduled inside my update method, I simply scheduled it once and added a delay of 0.5 seconds like so:
My problem is the ball is bouncing with the use of gravity and force so I'm not sure how I can tell the 2nd paddle to move where the ball is headed to catch it. Currently, my code looks like this:
Code:
- (void)update:(ccTime) dt {
if(_isPaused == FALSE)
{
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
if(sprite.tag == 3)
{
[self schedule:@selector(moveObj:)interval:0.0];
b2Vec2 autoPos = b2Vec2(sprite.position.x/PTM_RATIO, sprite.position.y/PTM_RATIO);
float32 autoAngle = -1 * CC_DEGREES_TO_RADIANS(sprite.rotation);
b->SetTransform(autoPos, autoAngle);
}
}
}
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos)
{
MyContact contact = *pos;
if((contact.fixtureA == _tPaddleFixture && contact.fixtureB == _ballFixture) || (contact.fixtureA == _ballFixture && contact.fixtureB == _tPaddleFixture))
{
_isHit = TRUE;
}
}
}}
-(void)moveObj:(ccTime)dt{
if(_isHit == TRUE)
{
tPaddle.position = ccp(ball.position.x, tPaddle.position.y);
}
else
{
tPaddle.position = ccp(160, tPaddle.position.y);
}}
if(_isPaused == FALSE)
{
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
if(sprite.tag == 3)
{
[self schedule:@selector(moveObj:)interval:0.0];
b2Vec2 autoPos = b2Vec2(sprite.position.x/PTM_RATIO, sprite.position.y/PTM_RATIO);
float32 autoAngle = -1 * CC_DEGREES_TO_RADIANS(sprite.rotation);
b->SetTransform(autoPos, autoAngle);
}
}
}
std::vector<MyContact>::iterator pos;
for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos)
{
MyContact contact = *pos;
if((contact.fixtureA == _tPaddleFixture && contact.fixtureB == _ballFixture) || (contact.fixtureA == _ballFixture && contact.fixtureB == _tPaddleFixture))
{
_isHit = TRUE;
}
}
}}
-(void)moveObj:(ccTime)dt{
if(_isHit == TRUE)
{
tPaddle.position = ccp(ball.position.x, tPaddle.position.y);
}
else
{
tPaddle.position = ccp(160, tPaddle.position.y);
}}
UPDATE: Someone suggested that I match the x-coordintates of the tPaddle and the ball so that's what I did. This works okay but I was hoping that I can make the paddle move only when the ball is close to hitting the top in order to catch it. I tried something like this:
Code:
if(ball.position.y > 300)
{
tPaddle.position = ccp(ball.position.x, tPaddle.position.y)
}
{
tPaddle.position = ccp(ball.position.x, tPaddle.position.y)
}
The problem with this however is that when the ball's y-coordinate is greater than 300, it gets stuck on top of the paddle and no longer bounces. How can I make the paddle move only when the ball is close to it then keep the ball bouncing?
I've found some sort of workaround for this, it's not pretty but it's not that ugly either so I think I'll settle with this "fix" for now. Since I call the moveObj is scheduled inside my update method, I simply scheduled it once and added a delay of 0.5 seconds like so:
Code:
[self scheduleOnce:@selector(moveObj)delay:0.5];