Phys Explosion

Known for HL2 leveldesigners, this function should blow up bodies in custom radius by custom force (decreasing as the body si farther from the epicentre). This is used mostly with game events such as bomb explosion, but doesn't destroy anything at all, just apply impulses.
It would be nice if such function was a part of box2D
OK i found out a solution, maybe it's not perfect but works well.

Code:
void PhysExplosion(float Xpos, float Ypos, float Radius, float Force)
{
   b2AABB Sector;
   Sector.lowerBound.Set(Xpos-Radius, Ypos-Radius);
   Sector.upperBound.Set(Xpos+Radius, Ypos+Radius);
   const int32 k_bufferSize = 512;
   b2Shape *buffer[k_bufferSize];
   int32 count = PhysWorld->Query(Sector, buffer, k_bufferSize);
   b2Body* Body;
   b2Vec2 HitVector;
   b2Vec2 HitPoint;
   b2Vec2 BodyPos;
   float a;
   float b;
   float c;
   float HitForce;
   float Distance;
   for (int32 i = 0; i < count; ++i)
   {
      Body = buffer[i]->GetBody();
      BodyPos = Body->GetWorldCenter();
      Distance=sqrt(pow((BodyPos.x)-Xpos,2)+pow((BodyPos.y)-Ypos,2));
      if ((Body->IsDynamic()) & (Distance<=Radius))
      {
         HitForce=(Radius-Distance)*Force;
         a=BodyPos.x-Xpos;
         b=BodyPos.y-Ypos;
         c=sqrt(pow(a,2)+pow(b,2));
         HitVector.Set(HitForce*(a/c), HitForce*(b/c));
         HitPoint=Body->GetWorldCenter();
         Body->ApplyImpulse(HitVector, HitPoint);
      };
   }

};
I don't think a linear force falloff would be realistic. (No idea how to do it better, but still, it's not realistic)
And for full realism you might want to count for the surface area directed to the blast (A small narrow box in the blast direction will catch less force then a small narrow box directly 90deg rotated on the blast)

Also, you try to get the length and normal of vectors with your own math, while the b2Vec2 class has functions for that, which are way faster then yours (cause pow() is slow, x*x is way faster then pow(x, 2) while it does the same)

This will be faster (but untested right now)
Code:
void PhysExplosion(b2Vec2 pos, float Radius, float Force)
{
   b2AABB Sector;
   Sector.lowerBound.Set(pos.x-Radius, pos.y-Radius);
   Sector.upperBound.Set(pos.x+Radius, pos.y+Radius);
   const int32 k_bufferSize = 512;
   b2Shape *buffer[k_bufferSize];
   int32 count = PhysWorld->Query(Sector, buffer, k_bufferSize);
   b2Body* Body;
   b2Vec2 HitVector;
   b2Vec2 BodyPos;
   float HitForce;
   float Distance;
   for (int32 i = 0; i < count; ++i)
   {
      Body = buffer[i]->GetBody();
      BodyPos = Body->GetWorldCenter();
      HitVector = (BodyPos-pos);
      Distance = HitVector.Normalize(); //Makes a 1 unit length vector from HitVector, while getting the length.
      if ((Body->IsDynamic()) & (Distance<=Radius))
      {
         HitForce=(Radius-Distance)*Force; //TODO: This is linear, but that's not realistic.
         Body->ApplyImpulse(HitForce * HitVector, Body->GetWorldCenter());
      };
   }

};
Yes i thought about the surface, or at least density, but i cant imaginw how would you calculate the force to look realistic... we'd better spawn a million of small bodies with no friction representing air particles 😛 that would make surface, wind lee and so on 😛
Or better, the world will be filled by these particles and explosion will make them grow bigger (hotter) and they will be also able to transfer heat between each other... the pressure will rise and objects will be damaged when collide with very hot particles 😛 anybody with 16-core?

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