Impulse/Explosion

Hey there,

I've been working on a game that uses explosions/impulses in quickbox2d (you don't have to know how to use quickbox2d, i can read box2d code..) ... (code is below)

The code i'm working with now isn't bad and all, however it's not good
enough. Currently, when you click, it applys a force to objects that are
only within a certain radius of the click. As long as the center of the
object is within the radius, it works.

However, if I get an object that is really long and click at the end of it,
it doesn't do anything because the center point is not within the radius.
What i want to do is be able to apply an impulse to all the boxes in that
radius regardless of if the center is within the radius(the end of the box
is in the radius, the click will not affect boxes outside the radius). I
was thinking about how to do it, and the only why i could come up with shoot
some little circles every 22.5 degrees (so, 16 outwards from the click
equidistantly from eachother). I haven't tried it, and i don't want to, it
sounds really inefficient.

Can you help me out with this?

heres the code i use:
Code:
function doExplosion(pos:b2Vec2):void {
var Body:b2Body = null;
var HitVector:b2Vec2 = new b2Vec2(0,0);
var BodyPos:b2Vec2 = new b2Vec2(0,0);
var HitForce:Number = 0;
var Distance:Number = 0;
for (var i:int = 0; i < boxes.length; ++i) { // holds references to all objects that are not 0 density
  Body = boxes[i].body;
  Body.WakeUp();
  BodyPos = Body.GetWorldCenter();
  HitVector.x = BodyPos.x - pos.x;
  HitVector.y = BodyPos.y - pos.y;
  Distance = HitVector.Normalize();
  if ((Body.IsDynamic()) && (Distance<=Radius)) {
   HitForce=(Radius-Distance)*Force;
   HitVector.x *= HitForce;
   HitVector.y *= HitForce;
   Body.ApplyImpulse(HitVector, Body.GetWorldCenter());
  }
}
}


Also, would my impulse code work for group objects counting them as one
solid object, or would it apply impulses to each component of the group
object?

Also, i can explain further what i'm looking to do if you need.

Thanks.
I'm not completely sure what you mean by group objects. Are you referring to a body with multiple shapes, or a set of bodies linked by joints? In the case of the former, you are applying an impulse at the body level so that group would act as one. In the case of the latter, each body would recieve a seperate impulse, if it was within the threshold you have set up.

There are a few threads on here about explosions and how people have handled it. My favourite is to use a sensor shape to indicate the size of the explosion and apply the force to all objects that are colliding with it. That way you are leveraging the physics engine's broad phase and not duplicating anything.
Of course, some of the things you have to be aware of;
-Its better to cache the collisions with the sensor and deal with them after the world.step, instead of in the Add callback.
-You will have a one frame delay between adding the sensor from user input, until you get collision responses back. Then another one frame delay until explosion impulses get applied. You can limit this by adding the sensor and then calling world.step with a zero timespan... nothing will move but you'll get collision responses. I personally have never done this though, so your mileage may vary.
-Using this you can get the points of contact from the contact manifold. Perhaps you could use that to apply impulse to multiple parts of an object, which might induce spin to your bodies. I don't know if that is any desired behaviour or not.
VengantMjolnir wrote:
I'm not completely sure what you mean by group objects. Are you referring to a body with multiple shapes, or a set of bodies linked by joints? In the case of the former, you are applying an impulse at the body level so that group would act as one.

A body with multiple shapes. And, Yeah, that's what i thought.

VengantMjolnir wrote:
There are a few threads on here about explosions and how people have handled it. My favourite is to use a sensor shape to indicate the size of the explosion and apply the force to all objects that are colliding with it. That way you are leveraging the physics engine's broad phase and not duplicating anything.
Of course, some of the things you have to be aware of;
-Its better to cache the collisions with the sensor and deal with them after the world.step, instead of in the Add callback.
-You will have a one frame delay between adding the sensor from user input, until you get collision responses back. Then another one frame delay until explosion impulses get applied. You can limit this by adding the sensor and then calling world.step with a zero timespan... nothing will move but you'll get collision responses. I personally have never done this though, so your mileage may vary.
-Using this you can get the points of contact from the contact manifold. Perhaps you could use that to apply impulse to multiple parts of an object, which might induce spin to your bodies. I don't know if that is any desired behaviour or not.


I actually thought of using a sensor (just adding a circle body, and doing what im about to say) just before getting onto the forums to see if i had an responses.

I was going to use the b2ContactFilter and get the normal and separation of the object to determine how much force i should apply. Then, go ahead and run through an array or soemthing and apply everything then clear the array.

I'll look into the world.step idea.

Thanks!
I recommend using b2World::Query. It's slightly easier to use than a sensor, as you don't need to step the world. You must just understand that it's a loose approximation to the shapes in a given area, though. Applying forces according to the sensor contact does sound useful though. I am surprised that you get useful positions for deep penetrations in sensors. Probably that only works for circles.

2.1 will support even more flexible use of Query, here are the function signatures to whet your appetite:
Code:
public function QueryAABB(callback:Function, aabb:b2AABB):void;
public function QueryShape(callback:Function, shape:b2Shape, transform:b2Transform = null):void;
public function QueryPoint(callback:Function, p:b2Vec2):void;

callback is called once per fixture.

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