Strange Jitter caused by ApplyLinearImpulse

I have some code used for explosions that is called from the b2ContactListener's BeginContact function. The piece of this code that causes the physics simulation to jitter forever after this code is called is the following:
Code:
b->ApplyLinearImpulse(b2Vec2(cosf(angle) * force, sinf(angle) * force), b->GetPosition());

I ran a profiler on my app (I am using the IOS port of box2d) and found that when the jitter is present, a large amount of time is spent on b2PairLessThan(b2Pair const&, b2Pair const&); When the jitter isn't present, this function doesn't show up anywhere in the top cpu consuming functions. The jitter seems to be worse in maps with more static bodies than maps without as many static bodies. This has stumped me for a few days now. I can't seem to figure it out because of my limited knowledge about the workings of box2d. Any ideas on why this is happening would be appreciated.
You mean you only apply this once and then everything jitters from that point onwards? Or only the body you applied it to is jittering? How fast does this impulse make the body move? Are you looking at the debug draw display?
Exactly. After this force is applied once, the entire simulation jitters from then onwards. What happens is the step function of the b2world takes varying amounts of time and freezes occasionally, which gives the appearance of a jitter by making animations, game logic, etc. irregular. I am using a variable time step so the simulation also takes large leaps occasionally as a result of the varying amounts of time spent on the step function of the b2world. The bodies don't go that fast after the force is applied. Almost all the bodies in the world receive this force though. I loop through all the physics bodies in the b2world and apply different types of forces based on their position in relation to the explosion. Here is the full code:
Code:
for (b2Body*b = currentWorld->GetBodyList(); b!=nil; b = b->GetNext()) {
       
       // if (b->GetType()==b2_staticBody)
       //     continue; //doesn't really change anything when uncommented. Makes the explosion slightly faster.

       
       
        float distance = 0.0f;
        float strength = 0.0f;
        float force = 0.0f;
        float angle = 0.0f;
       
        CGPoint curPos = [Helper toPixels:b->GetWorldCenter()];
       
        distance = ccpDistance(selfPos, curPos);
        if (distance>maxDistance) {
            distance = maxDistance - 0.01;
        }
       
       
        strength = (distance - maxDistance)/maxDistance;
        force = strength * maxForce;
        angle = atan2f(selfPos.y - curPos.y, selfPos.x - curPos.x);
        b->ApplyLinearImpulse(b2Vec2(cosf(angle) * force, sinf(angle) * force), b->GetPosition());
        BodyNode * bn = (BodyNode*)b->GetUserData();
        if ([bn isKindOfClass:[Soldier class]]) {
            ((Soldier*)bn).health-=abs(force*(damage * kRocketDamageFactor)); //used to be 50
        }
        if ([bn isKindOfClass:[BaseAI class]]) {
            ((BaseAI*)bn).health-=abs(force*(damage * kRocketDamageFactor));
        }
        if ([bn isKindOfClass:[BaseLevelObject class]]) {
            if (((BaseLevelObject*)bn).takesDamageFromBullets == YES) {
                ((BaseLevelObject*)bn).health-=abs(force*(damage * kRocketDamageFactor));
            }
        }
    }
I can't see how applying an impulse only once would cause any jitter to continue from that point on... are you sure this impulse is not being called repeatedly somehow?

While I'm here I'll comment on something else that seems strange - that code will make the explosion affect every body in the world, even bodies that are nowhere near the explosion... and bodies outside the maxDistance will actually be given a small tap towards the explosion point. This is another reason I'd suspect that this code is being called repeatedly, because it would give the symptoms you're talking about.

Supposing maxDistance is 10 units, consider what would happen for a body that is 1000 units away from the explosion point (in comments):
Code:
distance = ccpDistance(selfPos, curPos); //distance = 1000
if (distance>maxDistance) { //evaluates to true
    distance = maxDistance - 0.01; //distance is set to 9.99 (why not just 'continue' to ignore this body?)
}
       
strength = (distance - maxDistance)/maxDistance; //strength is set to -0.01/1000

Perhaps you have your reasons for this, but it sure seems odd 🙂
Thanks for the tip. I didn't catch that. Originally, this was a black hole type of explosion that pulled all objects toward it. You are right, I should just add continue there, which would speed up the explosion. But this loop is only getting called once per explosion. Adding the continue where you said to eliminated the jitter. Thank you 🙂 I'm not really sure what was causing the jitter originally.

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