Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically move a sprite to catch another sprite
    primarykey
    data
    text
    <p>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. </p> <p>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:</p> <pre><code>- (void)update:(ccTime) dt { if(_isPaused == FALSE) { _world-&gt;Step(dt, 10, 10); for(b2Body *b = _world-&gt;GetBodyList(); b; b=b-&gt;GetNext()) { if (b-&gt;GetUserData() != NULL) { CCSprite *sprite = (CCSprite *)b-&gt;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-&gt;SetTransform(autoPos, autoAngle); } } } std::vector&lt;MyContact&gt;::iterator pos; for(pos = _contactListener-&gt;_contacts.begin(); pos != _contactListener-&gt;_contacts.end(); ++pos) { MyContact contact = *pos; if((contact.fixtureA == _tPaddleFixture &amp;&amp; contact.fixtureB == _ballFixture) || (contact.fixtureA == _ballFixture &amp;&amp; contact.fixtureB == _tPaddleFixture)) { _isHit = TRUE; } } }} -(void)moveObj:(ccTime)dt{ if(_isHit == TRUE) { //I need the code here NSLog(@"HIT"); } else { tPaddle.position = ccp(160, tPaddle.position.y); }} </code></pre> <p>Currently, I'm just printing a message that says hit if the paddle and the ball should collide (only at the center) but I'm not sure how to tell tPaddle to move wherever the ball is headed to, to catch it. Can somebody please give me an idea how I can implement this? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload