Rotation from explosion force?

FYI, I have implemented the method you propose and it seems to work well, causing both linear and angular acceleration. As one slight refinement I calculate the force based on the closest position, and then divide the force between the two points when I apply it. In my program I currently only have rectangular shapes, and that's reflected in the code below, but I think it should work for most common cases of shapes.

Code: Select all

OrientedBoundingBox2 obb = rigidBody.OrientedBoundingBox2;

Vector2 closestPos;
if (obb.IntersectsCircle(position, radius, out closestPos))
{
   double distance = closestPos.Distance(position);
   double force = (radius - distance) * forceMultiplier;

   // OK, we're exploding, now we need to apply the force to the two furthest away points, one on the positive
   // side of the line between our explosion point and the center of the object, and the other on the negative side.
   Line2 positionToBodyCenter = new Line2(position, rigidBody.position - position);
   Vector2 furthestPositiveSideVertex = rigidBody.Vertices.Where(v => positionToBodyCenter.GetSide(v) == Line2.Side.Right).ArgMax(v => v.DistanceSq(position));
   Vector2 furthestNegativeSideVertex = rigidBody.Vertices.Where(v => positionToBodyCenter.GetSide(v) == Line2.Side.Left).ArgMax(v => v.DistanceSq(position));
   double positiveDistance = furthestPositiveSideVertex.Distance(position);
   double negativeDistance = furthestNegativeSideVertex.Distance(position);
   double positiveForce = (positiveDistance / (positiveDistance + negativeDistance)) * force;
   double negativeForce = (negativeDistance / (positiveDistance + negativeDistance)) * force;

   Vector2 positiveDirection = (furthestPositiveSideVertex - position).NormalizedCopy();
   Vector2 negativeDirection = (furthestNegativeSideVertex - position).NormalizedCopy();
   rigidBody.AddForce(furthestPositiveSideVertex, positiveDirection * positiveForce);
   rigidBody.AddForce(furthestNegativeSideVertex, negativeDirection * negativeForce);
}

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