Positionning of Bodies

I have a dynamic body and its position is perfect. I tried the same thing with a static one and its positionning has gone "wild".They both are in a sprite and both sprites have the same position and they are children of the same MovieClip.

Here is my Ball (dynamic body):
Please Remark that i do not even scale the body's position and it fits perectly.

Code:

public class Ball extends Actor
   {
      public static const PHYSIC_RADIUS:int=5;//in meters
      public static const RADIUS:int=10;
      public static const MASS:int=10;
      
      //private var cam_:Camera;
      
      
      public function Ball(color:uint)
      {
         super();
         color_=color;
         
         this.InitializeActor();
      }
      override protected function InitializeActor():void
      {
         this.DrawActor();
         this.InitializePhysics();
         //UpdatePosition();
      }
      override protected function DrawActor():void
      {
         shape_=new Shape();
         this.addChild(shape_);
         shape_.graphics.beginFill(color_);
         shape_.graphics.drawCircle(0,0,RADIUS);
         shape_.graphics.endFill();
         shape_.x=75;
         shape_.y=50;
      }
      override protected function InitializePhysics():void
      {
       var ball:b2CircleShape=new b2CircleShape(PHYSIC_RADIUS);
       bp_=PhysicManager.GetInstance().CreateDynamicBody(ball,20,0.2,1,this);
       bp_.body_.SetPosition(new b2Vec2(75,50));
       debug_draw();
      }
      


Here is the code for my Walls (static body):
Code:

public function Wall(isHorizontal:Boolean)
      {
         super();
         isHorizontal_=isHorizontal;
         if(isHorizontal)
         {
          height_=WIDTH;
          width_=HEIGHT;
         }
         else
         {
          height_=HEIGHT;
          width_=WIDTH;
         }
         color_= WallFabric.WALLCOLOR;
         InitializeActor();
      }
      override protected function InitializeActor():void
      {
         this.DrawActor();
         this.InitializePhysics();
         
      }
      override protected function DrawActor():void
      {
         shape_=new Shape();
         this.addChild(shape_);
         shape_.graphics.beginFill(color_);
         if(!isHorizontal_)
          height_-=65;
            
            
         shape_.graphics.drawRect(0,0,width_,height_);
         shape_.graphics.endFill();
      }
      override protected function InitializePhysics():void
      {
         
         var rect:b2PolygonShape=new b2PolygonShape();
         if(isHorizontal_)
         {
            rect.SetAsBox(width_/(2*3),height_/(2*3));
         }
         else
            rect.SetAsBox(width_/(2* 3),(height_+65)/(2*3.6));
            
         bp_=PhysicManager.GetInstance().CreateStaticBody(rect,20,0.2,1,this);
         
      }
      public function SetPosition(p:Point):void
      {
        shape_.x=p.x;
        shape_.y=p.y;
         
        if(!isHorizontal_)
        bp_.body_.SetPosition(new b2Vec2(p.x/3+3.5,p.y/3+55));
        else
           bp_.body_.SetPosition(new b2Vec2(p.x/3+68,p.y/3+3));
        trace("p.x: "+p.x.toString()+"p.y: "+p.y.toString());
        debug_draw();
        //UpdatePosition();
      }


The actual AS3 shape is Ok but when you come to the wall it's all messed up!

P.S. I understand b2PolygonShape.SetAsBox might be different from Draw rectangle but in that case i would have at least 2 sides ok.
Might I ask why your wall numbers require so much botching? I see plus 65, minus 65, multiplying by 3, multiplying by 3.6, and lots of extra addition in the SetPosition function.

Navigating that all is a mess, no wonder you can't find the problem. We won't be able to find it either - you haven't been very clear on exactly what the problem is.
That's what it took to place the Body at the right "place" i'm not asking people to correct my code i just want to know if i missed something in Box2D positionning system. Is the Origin of the body top left corner of the stage? That's my problem, i do not understand why the ball has no problem and with the Wall i have to do all sorts of "butching" to obtain the right position.
Oh, okay - the SetPosition function is setting the CENTER OF MASS of the body. In a static rectangle or the ball you built, that's the exact center of the shape.

Which would explain why it required so much botching. If your movieclips are also centered, as opposed to having their registration point in the top left corner, you're going to save yourself a lot of headaches.
Thanks, that helped me a lot to understand the problem. 😀

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