objects do not collide with each other - am new to box2d

hi all,
i was wondering if anyone can help with a box2d project i have.
i'm sure some would find the problem easy to solve.
and i'm definitely missing something with my world set-up to have this problem.

the problem: my objects don't seem to interact (i.e. collide with each other).
i have a b2CircleDef (dynamic with a density of 1) falling onto a b2PolygonDef (static with a density of 0).

the b2CircleDef falls all the way to the edge of the world (upperBound).
but it never hits (or bounces off) the b2PolygonDef.
and when i set the density of the b2PolygonDef to 1, it falls as well: so its set-up in the world seems correct.

also, with the b2DebugDraw, it seems location and dimensions are correctly set-up.

any help or guidance will be appreciated.
thanks, guys!



so everything seems cool, but its obvious that they are not.
here's the swf: http://sites.google.com/site/kuertee2/f ... ation2.swf
and here's the fla: http://sites.google.com/site/kuertee2/f ... ation2.fla
and here's the code:
Code:
package 
{
   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;
   import flash.display.Sprite;
   //
   import flash.display.MovieClip;
   import flash.events.Event;
   
   /**
    * ...
    * @author Jo' Paul Mamuyac
    */
   public class Box2D extends MovieClip
   {
      
      public function Box2D()
      {
         addEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
      }
      private function onAddedToStage (e: Event): void {
         removeEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
         addEventListener (Event.REMOVED_FROM_STAGE, onRemovedFromStage);
         init ();
      }
      private function onRemovedFromStage (e: Event): void {
         removeEventListener (Event.REMOVED_FROM_STAGE, onRemovedFromStage);
         clean ();
      }
      private function init (): void {
         createB2World ();
         setUpWalls ();
         createS ();
         addEventListener (Event.ENTER_FRAME, onThisEnterFrame, false, 0, true);
      }
      private function clean (): void {
         removeEventListener (Event.ENTER_FRAME, onThisEnterFrame);
         destroyB2World ();
      }
      private var _b2TimeStep: Number;
      private var _b2Iterations: int;
      private function onThisEnterFrame (e: Event): void {
         b2WorldUpdate ();
      }
      //{b2World
      private var _b2World: b2World;
      private var _scale: Number;
      private function createB2World (): void {
         if (isNaN (_scale)) {
            _scale = 0.1;
         }
         var worldAABB: b2AABB = new b2AABB ();
         worldAABB.lowerBound.Set (stage.stageWidth * -_scale, stage.stageHeight * -_scale);
         worldAABB.upperBound.Set (stage.stageWidth * _scale, stage.stageHeight * _scale);
         var gravity: b2Vec2 = new b2Vec2 (0, 10);
         var doSleep: Boolean = true;
         _b2World = new b2World (worldAABB, gravity, doSleep);

         // debug draw start
         var m_sprite:Sprite;
         m_sprite = new Sprite();
         addChild(m_sprite);
         var dbgDraw:b2DebugDraw = new b2DebugDraw();
         var dbgSprite:Sprite = new Sprite();
         m_sprite.addChild(dbgSprite);
         dbgDraw.m_sprite=m_sprite;
         dbgDraw.m_drawScale=1/_scale;
         dbgDraw.m_alpha=1;
         dbgDraw.m_fillAlpha=0.5;
         dbgDraw.m_lineThickness=1;
         dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit;
         _b2World.SetDebugDraw(dbgDraw);
      }
      private function destroyB2World (): void {
         
      }
      private function b2WorldUpdate (): void {
         if (isNaN (_b2TimeStep)) {
            _b2TimeStep = 1 / 60;
         }
         if (isNaN (_b2Iterations)) {
            _b2Iterations = 10;
         }
         _b2World.Step(_b2TimeStep, _b2Iterations);
         
         // Go through body list and update sprite positions/rotations
         for (var bb:b2Body = _b2World.m_bodyList; bb; bb = bb.m_next){
            if (bb.m_userData is MovieClip){
               bb.m_userData.x = bb.GetPosition().x / _scale;
               bb.m_userData.y = bb.GetPosition().y / _scale;
               bb.m_userData.rotation = bb.GetAngle() * (180 / Math.PI);
               trace (bb.m_userData + ": " + bb.GetPosition().x + ", " + bb.GetPosition().y);
            }
         }
      }
      //}
      //{walls
      private function setUpWalls (): void {
         var body:b2Body;
         var bodyDef:b2BodyDef;
         var boxDef:b2PolygonDef;
         var circleDef:b2CircleDef;
         var mc: MovieClip;
         
         mc = parent ["mcSpringboard"] as MovieClip;
         boxDef = new b2PolygonDef ();
         boxDef.SetAsBox (mc.width * _scale / 2, mc.height * _scale / 2);
         boxDef.density = 0;
         boxDef.friction = 0.5;
         boxDef.restitution = 1;
         
         bodyDef = new b2BodyDef ();
         bodyDef.position.Set (mc.x * _scale, mc.y * _scale);
         bodyDef.userData = mc;
         
         body = _b2World.CreateBody (bodyDef);
         body.CreateShape (boxDef);
         body.SetMassFromShapes ();
         addChild (bodyDef.userData);
      }
      //}
      //{create S
      private function createS (): void {
         var body:b2Body;
         var bodyDef:b2BodyDef;
         var boxDef:b2PolygonDef;
         var circleDef:b2CircleDef;
         var mc: MovieClip;
         
         mc = parent ["mcS"] as MovieClip;
         circleDef = new b2CircleDef ();
         circleDef.radius = mc.width / 2 * _scale;
         circleDef.density = 1;
         circleDef.friction = 0.5;
         circleDef.restitution = 1;
         
         bodyDef = new b2BodyDef ();
         bodyDef.position.Set ((mc.x + mc.width / 2) * _scale, (mc.y + mc.height / 2) * _scale);
         bodyDef.userData = mc;
         
         body = _b2World.CreateBody (bodyDef);
         body.CreateShape (circleDef);
         body.SetMassFromShapes ();
      }
      //}
      
   }
   
}
Hey matey,

Hmm no one has replied. I am sure i had a similar problem once but the details of that experiment are hazy.

Try adding .0s to the ends of your constant values. 1/60.0 for example. I am sure some compilers treat stuff differently.

Cheers.
hey clark!
thanks for the reply!
i found the problem.
it had nothing to do with Box2D at all!
i think its a quirk in Flash itself:

when an Number is declared, e.g.:
Code:
private var _b2Timestamp: Number
its value is initially Number.NaN.

but when an int is declared, e.g.:
Code:
private var _b2Iterations: int
its value is initially 0.

so...when i check its initial value in the code below,
_b2Iterations is not NaN and so is never set to 10.
Code:
if (isNaN (_b2Iterations)) {
   _b2Iterations = 10;
}


so, the new code now works!
Code:
package 
{
   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;
   import flash.display.Sprite;
   //
   import flash.display.MovieClip;
   import flash.events.Event;
   
   /**
    * ...
    * @author Jo' Paul Mamuyac
    */
   public class Box2DWorld extends MovieClip
   {
      
      public function Box2DWorld()
      {
         addEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
      }
      private function onAddedToStage (e: Event): void {
         removeEventListener (Event.ADDED_TO_STAGE, onAddedToStage);
         addEventListener (Event.REMOVED_FROM_STAGE, onRemovedFromStage);
         init ();
      }
      private function onRemovedFromStage (e: Event): void {
         removeEventListener (Event.REMOVED_FROM_STAGE, onRemovedFromStage);
         clean ();
      }
      private function init (): void {
         createB2World ();
         setUpWalls ();
         createS ();
         //setUpFromHelloWorld ();
         addEventListener (Event.ENTER_FRAME, onThisEnterFrame, false, 0, true);
      }
      private function clean (): void {
         removeEventListener (Event.ENTER_FRAME, onThisEnterFrame);
         destroyB2World ();
      }
      private function onThisEnterFrame (e: Event): void {
         b2WorldUpdate ();
      }
      //{b2World
      private var _b2World: b2World;
      private var _scale: Number;
      private function createB2World (): void {
         if (isNaN (_scale)) {
            _scale = 0.1;
         }
         var worldAABB: b2AABB = new b2AABB ();
         worldAABB.lowerBound.Set (stage.stageWidth * -_scale, stage.stageHeight * -_scale);
         worldAABB.upperBound.Set (stage.stageWidth * _scale, stage.stageHeight * _scale);
         var gravity: b2Vec2 = new b2Vec2 (0.0, 10.0);
         var doSleep: Boolean = true;
         _b2World = new b2World (worldAABB, gravity, doSleep);

         // debug draw start
         var m_sprite:Sprite;
         m_sprite = new Sprite();
         addChild(m_sprite);
         var dbgDraw:b2DebugDraw = new b2DebugDraw();
         var dbgSprite:Sprite = new Sprite();
         m_sprite.addChild(dbgSprite);
         dbgDraw.m_sprite=m_sprite;
         dbgDraw.m_drawScale=1/_scale;
         dbgDraw.m_alpha=1;
         dbgDraw.m_fillAlpha=0.5;
         dbgDraw.m_lineThickness=1;
         dbgDraw.m_drawFlags=0xFFFFFFFF;//b2DebugDraw.e_shapeBit;
         _b2World.SetDebugDraw(dbgDraw);
      }
      private function destroyB2World (): void {
         
      }
      private var _b2TimeStep: Number;// = 1.0 / 30.0;
      private var _b2Iterations: int;// = 10;
      private function b2WorldUpdate (): void {
         if (isNaN (_b2TimeStep)) {
            _b2TimeStep = 1.0 / 30.0;
         }
         if (isNaN (_b2Iterations) || _b2Iterations == 0) {
            _b2Iterations = 10;
         }
         _b2World.Step(_b2TimeStep, _b2Iterations);
         
         // Go through body list and update sprite positions/rotations
         for (var bb:b2Body = _b2World.m_bodyList; bb; bb = bb.m_next){
            if (bb.m_userData is MovieClip){
               bb.m_userData.x = bb.GetPosition().x / _scale;
               bb.m_userData.y = bb.GetPosition().y / _scale;
               bb.m_userData.rotation = bb.GetAngle() * (180 / Math.PI);
               trace (bb.m_userData + ": " + bb.GetPosition().x + ", " + bb.GetPosition().y);
            }
         }
      }
      //}
      //{walls
      private function setUpWalls (): void {
         var body:b2Body;
         var bodyDef:b2BodyDef;
         var boxDef:b2PolygonDef;
         var circleDef:b2CircleDef;
         var mc: MovieClip;
         
         mc = this ["mcSpringboard"] as MovieClip;
         boxDef = new b2PolygonDef ();
         boxDef.SetAsBox (mc.width * _scale / 2, mc.height * _scale / 2);
         boxDef.density = 0.0;
         boxDef.friction = 0.5;
         boxDef.restitution = 1.0;
         
         bodyDef = new b2BodyDef ();
         bodyDef.position.Set (mc.x * _scale, mc.y * _scale);
         bodyDef.userData = mc;
         
         body = _b2World.CreateBody (bodyDef);
         body.CreateShape (boxDef);
         body.SetMassFromShapes ();
         addChild (bodyDef.userData);
      }
      //}
      //{create S
      private function createS (): void {
         var body:b2Body;
         var bodyDef:b2BodyDef;
         var boxDef:b2PolygonDef;
         var circleDef:b2CircleDef;
         var mc: MovieClip;
         
         mc = this ["mcS"] as MovieClip;
         circleDef = new b2CircleDef ();
         circleDef.radius = mc.width / 2 * _scale;
         circleDef.density = 1.0;
         circleDef.friction = 0.5;
         circleDef.restitution = 1.0;
         
         bodyDef = new b2BodyDef ();
         bodyDef.position.Set ((mc.x + mc.width / 2) * _scale, (mc.y + mc.height / 2) * _scale);
         bodyDef.userData = mc;
         
         body = _b2World.CreateBody (bodyDef);
         body.CreateShape (circleDef);
         body.SetMassFromShapes ();
      }
      //}
      
   }
   
}











those scratching their heads on how to convert Box2D units to actual stage pixels should look at the code above.
note that the bodyDef.userData MovieClips are MovieClip already laid out the on stage.

so...to layout an initial state for my Box2D world, i'd set-up the stage as normal:
i.e. placing movie clips on the stage as i would a normal project.

i just use the method above to convert the movie clips' width, height, x, y, rotations with the _scale value into Box2D units.
i also use this method to get the optimal b2AABB.lowerBound and b2AABB.upperBound values.

when i need to tweak Box2D unit scale for optimum performance, i simply change the _scale value.

with the above method, i can keep working in pixel units rather than doing guess work on the fractional units of Box2D.

to fully test this:
  1. create a new FLA
  2. set its Document Class as Box2DWorld
  3. add movie clips to the stage with the names "mcSpringboard" and "mcS"
  4. copy the code above to the class filename called Box2DWorld.as

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