PhysExplosion- Explosion method for Box2D AS3

icon_phyexplosionI just wrote a quick adaptation of the PhysExplosion Method (Box2D c++) in Box2D AS3.

This example works with Box2D and QuickBox2D.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
*
* @param	locationPnt Application point of the explosion
* @param	nRadius Radius of the explosion in pixels
* @param	nForce Force of explosion
*/
private function physExplosion(locationPnt:Point, nRadius:Number, nForce:Number) : void {
 
	var aabb:b2AABB = new b2AABB();
	aabb.lowerBound.Set((locationPnt.x - nRadius) / METER, (locationPnt.y - nRadius) / METER);
	aabb.upperBound.Set((locationPnt.x + nRadius) / METER,(locationPnt.y + nRadius) / METER);
	var k_bufferSize:Number = BODY_NUM;
	var buffer:Array = new Array();
	var count:int = _pSim.w.Query(aabb, buffer, k_bufferSize);
	for (var i:Number = 0; i< count; ++i) {
		var pBody:b2Body = buffer[i].GetBody();
		var pBodyPos:b2Vec2 = pBody.GetWorldCenter();
		var pHitVector:b2Vec2 = new b2Vec2(pBodyPos.x - locationPnt.x/METER, pBodyPos.y - locationPnt.y/METER);
		var radDist:Number = pHitVector.Normalize();
		radDist = (radDist * METER);
		if (!pBody.IsStatic() == (radDist<=nRadius)){
			pBody.WakeUp();
			var nHitForce:Number = ((nRadius-radDist) / nRadius) * nForce;
			var appForce:b2Vec2 = new b2Vec2(pHitVector.x*nHitForce,pHitVector.y*nHitForce);
			pBody.ApplyImpulse(appForce, pBody.GetWorldCenter());
		}
	}
 
}

Source code soon… And other experiments to support the angular velocity caused by the explosion

Share and Enjoy:
  • Google Bookmarks
  • Twitter
  • Facebook
  • LinkedIn
  • RSS
  • Netvibes
  • viadeo FR
  • del.icio.us
  • Tumblr
  • Digg
  • email

Tags: , , , , , ,

8 Responses to “PhysExplosion- Explosion method for Box2D AS3”

  1. Socco says:

    Hello,
    Amazing! Not clear for me, how offen you updating your http://www.eventdispatcher.fr.
    Socco

  2. charlie says:

    That’s very good, I was just about to start working on something very similar…
    But, can you adapt it so that the force doesn’t pass through objects which have density = 0??

  3. charlie says:

    Following on from the previous message it’s possible to use b2Segment to do a “ray-trace” from the origin of the explosion.
    From that it’s possible to determine the density of any shape in between the the explosion and the object being moved…

  4. Tanks for your interesting comment !

    Il will add notes and schema in this post to explain difficulties to render a good explosion (Point of application, center mass, density 0 bodies …)

  5. Een van de interessantste plaatsen die ik heb ontmoet.

  6. charlie says:

    ahhh, good find on the RayCast stuff…

    My implementation works similarly, but is nowhere near as efficient…

    I think it’s a slight shame that the box2d manual is so basic… (and doesn’t include some of the great additions.)

  7. Tony says:

    Salut Germain,

    Ca fait un bail (je n’ai pas ouvert msn depuis 1 an à peu près… :)

    Tu bosses où en ce moment ?
    Moi, je suis chez Bouygues Tel (Applis sur SetTopBox)

    Très intéressant ces travaux sur Box2D !

    Passe le bonjour au collègue edocoien à l’occaz ;)

    ++
    Tony

Leave a Reply