Few shapes on the same object

Hi,

I'm working with version 2.1of box2d for flash, and I can't figure out how to put more than one shape into an object. I try to add 2 fixtures, but got strange results.
Can anyone please give an example of code that creates body with more than one shape.

Thanks, Shefy 😉
Here you go, I redid some of the box2d example which is way more obfuscated. Just play around with it. And make sure to remember you have to define the verteces in a clockwise manner.

Code:
       
                var fixture:b2FixtureDef = new b2FixtureDef();
                var fixture2:b2FixtureDef = new b2FixtureDef();
               
                var boxDef:b2PolygonShape = new b2PolygonShape();
                boxDef.SetAsArray([
                new b2Vec2(-30.0/30, 0.0),
                new b2Vec2(30.0/30, 0.0),
                new b2Vec2(0.0, 15.0 / 30),
                ]);
                   
                var boxDef2:b2PolygonShape = new b2PolygonShape();   
                boxDef2.SetAsArray([
                new b2Vec2(-30.0/30, 0.0),
                new b2Vec2(0.0, 15.0 / 30),
                new b2Vec2(-30.0/30, 15.0 /30),
                ]);
               
                fixture.shape = boxDef;
                fixture.friction = 0.1;
                fixture.density = 0.5;
                fixture.restitution = 0.1;
               
                fixture2.shape = boxDef2;
                fixture2.friction = 0.1;
                fixture2.density = 0.5;
                fixture2.restitution = 0.1;
               

                body.CreateFixture(fixture);
                body.CreateFixture(fixture2);
10x,

I actually just got it working. I an using a tool I created that help create the shapes verteces (array of "shapes" holding array of verteces). And after I have this code, I
am using it in other projects, to create shapes for complex MCs.

So I wrote this constructor, that now (with your help) can handle array of shapes.

The code might look a bit messy, but it does the work
Code:
      // Ctor
      //    initialVelocity - initial velocity
      //  parent - the copntainor for theActor
      // shapes- array of arrays of points
      public function MultyShapeActor(parent:DisplayObjectContainer, location:Point, initialVelocity:Point, shapes:Array)
      {
         //1. create the custom - later will be tranfered to super
         // taken through parameters
         var custom:Sprite = getSpriteFromShapes(shapes);
         custom.x = location.x;
         custom.y = location.y;
         parent.addChild(custom);
         
         
         //2. vars for body & shape
         var tBody:b2Body;
         var tBodyDef:b2BodyDef;
         var tBodyShape:b2PolygonShape;
         var tBodyFixtureDef:b2FixtureDef;
         
         //3. set bodyDef & position
         tBodyDef  = new b2BodyDef();
         tBodyDef.position.Set(location.x / PhysicsVars.RATIO, location.y / PhysicsVars.RATIO);
         tBodyDef.type = b2Body.b2_dynamicBody;
         
         //6 handling the body
         tBody = PhysicsVars.world.CreateBody(tBodyDef);
         
         //4. set body shape
         var vertsV:Array = new Array();
         var fixtures:Array = new Array();
         var polyShapes:Array = new Array();
         var ind:int = -1;
         for each (var verts:Array in shapes) {
            vertsV = new Array();
            ind++;
            fixtures.push(new b2FixtureDef());
            polyShapes.push(new b2PolygonShape());
            for (var vInd:int = 0; vInd < verts.length; vInd++) {
               vertsV.push(new b2Vec2(verts[vInd].x / PhysicsVars.RATIO, verts[vInd].y / PhysicsVars.RATIO));
            }
            polyShapes[ind].SetAsArray(vertsV, vertsV.length);
         
            //5. setfixtureDef
               
            fixtures[ind].shape = polyShapes[ind];
            fixtures[ind].density = 1;
            fixtures[ind].friction = 0.3;
            
            tBody.CreateFixture(fixtures[ind]);
         }         
                  
         tBody.GetMass();
            
         super(tBody, custom); // transfering the body and custom to super to handle
         
      }
      


10x again, Shefy
Note that most of the objects in that code can be reused (they're just for passing data to Box2D, which makes its own copy), which would clean things up a bit. So rather than:

fixtures.push(new b2FixtureDef());
polyShapes.push(new b2PolygonShape());

...you can just make a single copy of each one outside the loop. In fact, you've already got those as 'tBodyShape' and 'tBodyFixtureDef' -- they're just unused. Set the fixture definition one time, point it to the single 'tBodyShape', and then the inner loop reduces to just reading the shape vertices, setting the poly, and creating the fixture.

Code:
         //5. setfixtureDef
         tBodyShape = new b2PolygonShape();
         tBodyFixtureDef = new b2FixtureDef();
         tBodyFixtureDef.shape = tBodyShape;
         tBodyFixtureDef.density = 1;
         tBodyFixtureDef.friction = 0.3;

         //4. set body shape
         var vertsV:Array;      // removed an excess "new Array()" here that wasn't used
         var ind:int = -1;
         for each (var verts:Array in shapes) {
            vertsV = new Array();
            ind++;
            for (var vInd:int = 0; vInd < verts.length; vInd++) {
               vertsV.push(new b2Vec2(verts[vInd].x / PhysicsVars.RATIO, verts[vInd].y / PhysicsVars.RATIO));
            }
            tBodyShape.SetAsArray(vertsV, vertsV.length);
         
            tBody.CreateFixture(tBodyFixtureDef);
         }         


You can also change 'vertsV' from an Array to a 'Vector.<b2Vec2>', which is faster access and gives you typed objects for each of its members. Worth looking into if you haven't used them before. (When your Array members are all the same type).

--
John
10x for the inputs.

I would look into Vector.<b2Vec>, I'm a good programmer but not that experienced in AS3, so I appriciate any advice that helps me code better.

About the other changes, I dont know why, but before I did it this way with all the extra arrays, it would kinda mix my shapes if I had more than one.
Maybe all the copies are done after the body is created.

If you look at the example I got, he does the same thing - 2 different fixtures.

If you have more simplified example of code that work, I would be happy to see it (and it sure will help others who check this post)

Thanks again, Shefy.
shefyg wrote:
Maybe all the copies are done after the body is created.

The copies are done during CreateFixture(). It winds up calling b2Fixture.Create()...
Code:
   b2internal function Create( body:b2Body, xf:b2Transform, def:b2FixtureDef):void
   {
      // a few things snipped to save space...

      // here it copies things out of the Fixture def
      m_userData = def.userData;
      m_friction = def.friction;
      m_restitution = def.restitution;
      m_filter = def.filter.Copy();

      // and here it makes a copy of the shape   
      m_shape = def.shape.Copy();
   }

OTOH, there's actually a warning in the documentation that says, "you cannot reuse shapes". Whereas, the fixtureDefs are documented as "You can reuse fixture definitions safely."

It's possible that this is an old warning that no longer applies, because with the Box2D code clearly making a copy of the shape, I don't see how changing the shape later would be a problem. I know I made multi-shape bodies in my own program while redefining the shape object.

That said, the fact that you had troubles makes me think the warning is still valid, and I may have just gotten away with it for some reason.

shefyg wrote:
If you look at the example I got, he does the same thing - 2 different fixtures.

If you have more simplified example of code that work, I would be happy to see it (and it sure will help others who check this post)

Fixtures are definitely reusable though. Even the documentation says you can do it.

The code I posted in the previous message is untested, but it should just drop in to your project, and perhaps verify whether or not the shapes can be reused. It replaces the section of your latest post, starting from the "//4. set body shape" comment, up to but not including the last two lines in your version.

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