Trying to encapsulate my bodies

Hey guys, I asked this on the flashcoders list, but immediately found these forums - and I'll probably get a much better answer here!

So, I like the engine after some playing, but I wanted to get things a little more encapsulated so I don't have to do lines and lines of creating bodies, and setting mass, etc.

What I'd like to do is:
var b:Block = new Block( width, height, etc etc etc), where Block extends b2Body.

It all worked pretty well when it extended b2BodyDef, but just tried to extend b2Body today, and my objects suddenly aren't moving (I'm looking in debug mode).

It kinda seems like creating a new b2Body isn't the same as creating it from the world outside the class? And then I tried super = world.CreateBody(_bodydef as b2BodyDef);, but that's not compiler friendly - I'm not sure how I'd do that!

So any suggestions on how to add my body to the world inside my class?

I'm making it extend b2Body and not b2BodyDef, because when I loop through and get m_bodylist, I'm getting an array of bodies, and want to call body[i].update on them.

thanks!
ben

package objects
{
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.Shapes.b2ShapeDef;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2World;

import flash.display.Sprite;

public class Block extends b2Body implements IObject
{
public function Block( world:b2World, w:Number, h:Number, density:Number = 1.0, friction:Number = .5, restitution:Number = 0.2 ) {
_bodydef = new b2BodyDef();
_bodydef.userData = new Sprite();
_bodydef.userData.graphics.lineStyle(.5,0x000000);
_bodydef.userData.graphics.beginFill(0xFFFF00);
_bodydef.userData.graphics.drawRect(0,0,w,h);
_bodydef.userData.graphics.endFill();

_poly.SetAsBox(w/60, h/60);
_poly.density = density;
_poly.friction = friction;
_poly.restitution = restitution;

super( _bodydef, world );
//super = world.CreateBody(_bodydef as b2BodyDef);
super.CreateShape(this.shape);
super.SetMassFromShapes();
}

public function update():void {} // not implemented yet - doing stuff outside in the actual enterframe handler

public function get display():Sprite { return _bodydef.userData; }
public function get shape():b2ShapeDef { return _poly; }
public function get bodydef():b2BodyDef { return _bodydef; }

public function set x( val:int ):void { _bodydef.position.x = val/60; display.x = val; }
public function set y( val:int ):void { _bodydef.position.y = val/30; display.y = val; }

public function get x():int { return _bodydef.position.x*60; }
public function get y():int { return _bodydef.position.y*60; }

private var _poly:b2PolygonDef = new b2PolygonDef();
private var _bodydef:b2BodyDef;

}
}
If you were to extend b2Body, you'd have to mess around with the factory constructor to support it. CreateBody calls new Body for you, and also adds that body to the world, which is what you are missing out on. Though there's no technical reasons in flash for these not to be seperated, Box2DAS3 has inherited it from the C++ version, where Box2D uses a smarter memory allocation method than you would normally get, so you'll have to live with it.

But you don't really want to extend b2Body. Extending b2BodyDef is sufficient for your Block constructor , and you should follow the recommended approach of putting extra data and methods inside the userData field. Try to think of box2d as an external library, something you instruct and get back feedback from, rather something you should organically build into your own engine. I get the impression that is the ethos it was created for. b2Body already has all the methods it needs (roughly) for controlling bodies; anything else you add is surely to do with your specific game, and thus better kept elsewhere.

Also, you don't need the "as b2BodyDef", you can implicitly cast something to it's ancestor.
Thanks much for that! I figured that since the world is a parameter in the constructor for b2Body, it would add it to the world as well.
But OK, I'll work around this - I'll see what else I can do with userData, it just occurred to me that the same userData you set in the bodyDef might be the same one you get from the world's bodylist - so I could probably just cast the bodylist items as a custom class.

Thanks, I'll rethink how I'm doing this. Oh and, yah I know I didn't have to cast that as b2BodyDef - just didn't take it out as I was restructuring things.

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