Box2d PhysExplosion----FIXED

Ive been trying to port a physexplosion function from C to AS3 and im getting a bit stuck and i was hoping that someone could help me out 🙂 .

Here is my code so far,
Code:
private function PhysExplosion(locationPnt, Radius, Force)
      {
            var Sector:b2AABB;
            Sector.lowerBound.Set(locationPnt.x-Radius, locationPnt.y-Radius);
            Sector.upperBound.Set(locationPnt.x+Radius, locationPnt.y+Radius);
         var k_bufferSize:Number = 10;
         var buffer:Array = new Array(k_bufferSize);
            var count:int = m_world.Query(Sector, buffer, k_bufferSize);
            for (var i:int = 0; i < count; i++)
            {
               var Body:b2Body = buffer[i].GetBody();
               var BodyPos:b2Vec2 = Body.GetWorldCenter();
//ERROR1------------------------------------------------------------------------------------
                        var HitVector:b2Vec2 = (BodyPos.x - locationPnt.x,BodyPos.y - locationPnt.y);
//-------------------------------------------------------------------------------------------
               var Distance = HitVector.Normalize(); //Makes a 1 unit length vector from HitVector
               if ((Body.IsDynamic())&&(Distance<=Radius))
               {
                  var HitForce = (Radius-Distance) * Force; //TODO: This is linear, but that's not realistic.
//ERROR2-----------------------------------------------------------------------------------
                                Body.ApplyImpulse(HitForce * HitVector, Body.GetWorldCenter());
//------------------------------------------------------------------------------------------
              };
            }

      };


My errors seem to be with declaring the hitVector and applying it. Any help would be greatly apreciated 😀
You need to create a new b2Vec2 instance:

new b2Vec2(BodyPos.x - locationPnt.x,BodyPos.y - locationPnt.y);
I did what you said and i thought that would be the end of it but when i was testing it out i hit a runtime error
Code:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
   at Box2d/PhysExplosion()
   at Box2d/Zooming()
   at MethodInfo-687()

Im not sure why this is because this is my PhysExplosion Function
Code:
private function PhysExplosion(locationPnt, Radius, Force)
      {
            var Sector:b2AABB;
            Sector.lowerBound.Set(locationPnt.x-Radius, locationPnt.y-Radius);
            Sector.upperBound.Set(locationPnt.x+Radius, locationPnt.y+Radius);
         var k_bufferSize:Number = 10;
         var buffer:Array = new Array(k_bufferSize);
            var count:int = m_world.Query(Sector, buffer, k_bufferSize);
            for (var i:int = 0; i < count; i++)
            {
               var Body:b2Body = buffer[i].GetBody();
               var BodyPos:b2Vec2 = Body.GetWorldCenter();
               var HitVector:b2Vec2 = new b2Vec2(BodyPos.x - locationPnt.x,BodyPos.y - locationPnt.y);
               var Distance = HitVector.Normalize(); //Makes a 1 unit length vector from HitVector
               if ((Body.IsDynamic())&&(Distance<=Radius))
               {
                  var HitForce = (Radius-Distance) * Force; //TODO: This is linear, but that's not realistic.
               var appForce:b2Vec2 = new b2Vec2((Radius-Distance) * Force,(Radius-Distance) * Force);
                  Body.ApplyImpulse(appForce, Body.GetWorldCenter());
              };
            }

      };

which has no errors on load and this is my function caller
Code:
PhysExplosion(new b2Vec2((segment.p2.x * lambda + (1-lambda) * segment.p1.x) * physScale,(segment.p2.y * lambda + (1-lambda) * segment.p1.y) * physScale),2,10);

Im sorry about this 😳
if you run your swf in debug, it would tell you the line number that the error occured on, plus it would break at that point and you could inspect exactly what was null. a good 80% of porgramming is debugging, so its a great idea to work on those skills.

With that said, you are defining a reference to a b2AABB, but not creating a new object.
Code:
  var Sector:b2AABB;
            Sector.lowerBound.Set(locationPnt.x-Radius, locationPnt.y-Radius);
            Sector.upperBound.Set(locationPnt.x+Radius, locationPnt.y+Radius);


this is going to cause an error because var Sector:b2AABB is just a reference to a null instance of b2AABB.
you need var Sector:b2AABB = new b2AABB();
This is working now after a little bit of work. i have been working on it now for about 3 hours and i havnt progressed far. This is my physExplosion function
Code:
      private function PhysExplosion(locationPnt, Radius, Force){
         var aabb:b2AABB = new b2AABB();
         aabb.lowerBound.Set((locationPnt.x - Radius) / physScale,(locationPnt.y - Radius) / physScale);
         aabb.upperBound.Set((locationPnt.x + Radius) / physScale,(locationPnt.y + Radius) / physScale);
         var k_bufferSize:Number = 10;
         var buffer:Array = new Array();
         var count:int = m_world.Query(aabb, buffer, k_bufferSize);
         for (var i:Number = 0; i < count; ++i)
            {
               var Body:b2Body = buffer[i].GetBody();
               var BodyPos:b2Vec2 = Body.GetWorldCenter();
               var HitVector:b2Vec2 = new b2Vec2(BodyPos.x - locationPnt.x,BodyPos.y - locationPnt.y);
               var radDist = HitVector.Normalize(); //Makes a 1 unit length vector from HitVector
         radDist = (radDist / physScale);
               if ((Body.IsStatic()==false) &&(radDist<=Radius)){
               Body.WakeUp();
               var HitForce = (radDist [b]-[/b] Radius) * Force;
               var appForce:b2Vec2 = new b2Vec2(HitForce,HitForce);
                  Body.ApplyImpulse(appForce, Body.GetWorldCenter());
              };
            }

      };

but it only ever applys the impulse 1 direction depending on the BOLD operand. i had thought that this could have something to do with a negative multiplied by a negative always equalling a positive but this does not seem to be the case. After struggling with this for many hours i have a huge headache and cant seem to get anywhere. i hope someone can see the error in here cause i cant, for the life of me, find it. 🙁
Any ideas?
http://www.eventdispatcher.fr/2009/11/2 ... box2d-as3/

You can find an example here of physExplosion, a quick adaptation of your C to AS3.

Have a nice day !
Glad to see you got it working.

It looks very neat and could be useful in some future games.

Nice work! Kudos!
Hi,

Thanks for your reply.

You can see here :
http://www.eventdispatcher.fr/2009/11/2 ... comment-22

A discution wich seems to start about it


Have a nice friday !

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