How to test if b2Body has stopped moving

Hello,

I have followed this tutorial(http://www.raywenderlich.com/457/intro- ... cing-balls) to create moving balls. Now I want to check if ball has stopped moving. Can anyone please help me with it?

Thank you,
Anks
You can check the ball's linear velocity (speed) in the tick() method or, if you enabled the "sleep" option, you can check if the ball is not awake:

Code:
- (void)tick:(ccTime) dt {
    _world->Step(dt, 10, 10);

    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext())
    {   
        if (b->GetUserData() != NULL)
        {   
              if(!(b->IsAwake()))
             {
                 // body is not moving
             }

            // or
         
             b2Vec2 velocity = b->GetLinearVelocity();
             if(velocity.x == 0 && velocity.y == 0)
             {
                 // body is not moving
             }
        }       
    }
}

You might want to use velocity.x < 0.3 && velocity.y < 0.3 to also include cases where the ball is just moving really slow.
Thanks. It works 🙂. Can you please also tell me, once everything stopped moving, I want to change one of my b2Body object to certain position. I used setTransform, but instead of going to that position it keeps moving. Your help is really appreciated.

Thanks,
Anks
I'm not sure how you used SetTransform but this works for me:
Code:
b->SetTransform(b2Vec2(3, 3), b->GetAngle());
b->SetLinearVelocity(b2Vec2(0,0));


The second line is necessary only if you don't wait for the ball to completely stop.
Thanks for your reply. But actually I want it to be at center position. When I use (3,3) it goes to directly as origin.
Opps.. sorry. I just changed x position and it worked. But actually what happens, when I want to move paddle, i just want it to touch the mainball and all other balls should not interact or affect to paddle collision but if other balls and mainball collide it should work perfectly.

Once I move mainball then mainball should also not interact with paddle. So I used the code, which i have mentioned in my other post, if you can just check that would be really great.

Thanks,
Anks
So you want all the balls to collide with each other but only one ball to collide with the paddle unless you move it, right?

If so then this should work:

Add this line to your main ball's fixture definition:
Code:
ballShapeDef.filter.categoryBits = 0x0002;

and the following lines to the paddle's fixture definition:
Code:
fd.filter.categoryBits = 0x0002;
fd.filter.maskBits = 0xFFFF & ~0x0004;

fd should be a b2FixtureDef (I don't know what it's called in your code)

and to the other balls' fixture definition add:
Code:
ballShapeDef.filter.categoryBits = 0x0004;

This should make sure only the main ball can collide with the paddle but all balls collide with each other. If you want the main ball to not collide with the paddle do this:

in your .h file add:
Code:
b2Fixture* fixture;

and in the .mm file you should have:
Code:
fixture = _body->CreateFixture(&ballShapeDef);

Now whenever you need to stop collision with the paddle add this:
Code:
b2Filter filter = fixture->GetFilterData();
filter.categoryBits = 0x0002;
fixture->SetFilterData(filter);

or filter.categoryBits = 0x0004; to resume collision.
THANKS FOR ALL YOUR REPLIES.

I'm running into one more issue now.

how to calculate vertices position of b2Polygon in box2D??


Hello,

I have one shape when it touches another collision detection should happen, for that i need to draw b2Polygon shape in xcode. I need proper vertices to draw this shape in iPhone. Can anyone please help me how to do that?

Thank you,

Anks


Can you please help me???

Original thread: https://box2d.org/forum/viewtopic.php?t=6746