Compound Shapes Issue

Hey everyone,

I am trying to build a new b2body based on other b2bodies.

So the basic set up is that I have an object, and that object is made up of other objects. So to make the object, I need to add all the other object's shapeDefs.

What I would like to do is iterate through the shapeDefs that make up the bodies that I am trying to add to my new body (thus forming a compound shape).

So something like this:

body = Common.getWorld().CreateBody(bodyDef); // the compound body

body.addOtherBody(body2); // 2 bodies that I would like to add to this body
body.addOtherBody(body3);
.
.
private function addOtherBody(otherBody:b2Body):void {
// go through all the shapes in the body and add them to the new compound body
while (otherBody.getShapeList() != null) {
body.CreateShape(otherBody.getShapeList());
// go to next shape in body
}
}


The problem I am having is that CreateShape() takes a b2ShapeDef, and not a b2Shape. Is there anyway to get the shapeDef of a Shape?

Or a better way to do what I am looking to do?

Many thanks!
Alex
No, there isn't, and for no good reason AFAIK. The ostensible logic, I believe, is that Box2D is meant to just be an engine, which you drive from commands from your game. As shapes always originate from defs you should still have the defs handy (typically storing them via userData if you want them a lot).
This has never been convincing to me though.

It should be easy enough to write a function to recreate the defs from shapes, though. You'll have more difficulty if you are using shaktools thin line segments, as then defs to shapes is not one-to-one.
Hey Boris,

Seeing as there is no 'direct' way to do it, I think I'll just do what you mentioned and save the shapes as variables in my Objects. It's kind of like the easy way out of writing a function to generate them (ie: less testing for missed cases, etc). Since this is kind of a little side project, I don't want to spend too much time doing said testing.

Thanks for the info!

Alex
Here's a snippet of code (not very tested, but I've used it a few times) that pulls a shape def from a shape. Not sure how well this will translate to AS3, since I'm not sure if some of the shape variables are private there (at least you may need to switch things to use getters instead of direct access), but here goes:
(Java code)
Code:
   /**
    * Extract a ShapeDef from a Shape so that you can, for instance,
    * copy a shape or recreate it on a new body.
    * <p>
    * You may need to downcast the ShapeDef to the appropriate
    * type - use ShapeDef::getType to see what type is appropriate.
    * </p>
    * <p>
    * Watch out when moving shapes from one body to another - the local
    * positions of shapes are relative to the body centers, so you will
    * need to compensate for that if you don't want the shape to jump
    * from one place to another.
    * @param s Shape to extract definition from
    * @return A ShapeDef that, if used via Body::createShape(shapeDef),
    * should result in exactly the same shape showing up on the body as
    * was passed as the parameter to this method.  Note that the Body mass
    * would still need to be set appropriately to get an identical Body, since this information lives
    * on the body, not the shape.
    */
   public static ShapeDef shapeToShapeDef(Shape s) {
      ShapeType type = s.getType();
      if (type == ShapeType.CIRCLE_SHAPE) {
         CircleDef cd = new CircleDef();
         CircleShape cs = (CircleShape)s;
         cd.filter.categoryBits = cs.m_filter.categoryBits;
         cd.filter.maskBits = cs.m_filter.maskBits;
         cd.filter.groupIndex = cs.m_filter.groupIndex;
         cd.density = cs.m_density;
         cd.friction = cs.m_friction;
         cd.isSensor = cs.isSensor();
         cd.restitution = cs.m_restitution;
         cd.radius = cs.getRadius();
         cd.userData = cs.getUserData();
         cd.localPosition = cs.getLocalPosition();
         return cd;
      } else if (type == ShapeType.POLYGON_SHAPE) {
         PolygonDef pd = new PolygonDef();
         PolygonShape ps = (PolygonShape)s;
         pd.filter.categoryBits = ps.m_filter.categoryBits;
         pd.filter.maskBits = ps.m_filter.maskBits;
         pd.filter.groupIndex = ps.m_filter.groupIndex;
         pd.density = ps.m_density;
         pd.friction = ps.m_friction;
         pd.isSensor = ps.isSensor();
         pd.restitution = ps.m_restitution;
         pd.userData = ps.getUserData();
         int nVerts = ps.getVertexCount();
         Vec2[] vertices = ps.getVertices();
         for (int i=0; i<nVerts; ++i) {
            pd.vertices.add(vertices[i]);
         }
         return pd;
      }
      return null;
   }

Remember, though, that to move a shape from one body to another, you can't just pull the shape def off of one and add it to another, because the bodies have different world positions - make sure you translate the shapedefs by that difference before you add them to the target body (change localPosition for circles, and all the vertices for polygons).

@Erin: Do you think functions to do this type of thing (shapedefs from shapes, bodydefs from bodies, clone bodies, move shapes from one body to another, separate one body into two, etc.) should be part of the shape/body/world bit of the API, or would they be better in a separate utility class? If it's just a matter of being low on the priority list, I've already got code for most of it, let me know and I'll clean it up (make it work with edge shapes, mainly) and put it in; otherwise I'll probably add a couple utility classes to the contrib directory at some point to make some of this a bit easier on people.

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