Sunday, January 10, 2010

svn r246 (2.1.0 branch)

As I mentioned in the previous post, I've been working on the new version of pybox2d, based on the work-in-progress C++ library Box2D 2.1.0.

With all of the upstream changes (see the previous post), it gave me a good opportunity to start anew. SVN r246 offers these benefits over previous versions of pybox2d, in no particular order:
  • Python 3k (3.1 tested) support. Many would argue that you wouldn't want to use it, but I have seen very little fps difference in my tests.
  • Significantly increased performance. I get a steady 55-60fps on test_Pyramid with the menu drawing disabled, on both Python 2.6 and 3.1. I never got above 40fps on 2.0.1.
  • The above is partly related to the new (optional) b2DebugDrawExtended class. It adds slightly to the base b2DebugDrawClass by doing world->screen coordinate conversions in C++ before passing them to Python. Possibly not very useful in the OpenGL case, but very useful for SDL-based systems. I think in my small tests it offered a gain of 5-10fps.
  • DebugDraw flags are also implemented as keywords now, so it's unnecessary to pass a bitmask to it.
  • Callback functions that used to get b2Vec2s now get tuples -- so you don't risk a crash when holding on to a b2Vec2 instance created on the stack.
  • kwarg support for all class __init__ routines. This means you can do things like:

    self.world.CreateBody(
          b2BodyDef(
              type=b2_staticBody,
              position=(0,3),
              fixtures=[b2CircleShape(radius=0.5),
                        b2PolygonShape(box=(0.5, 0.5))],
              )
          )



    Which would create a static body at (0,3) with a circle (of radius 0.5) and a box (with half-dimensions 0.5,0.5). You can still create things the old fashioned way, if you like. Just remember with the new Box2D there are no b2ShapeDefs.
  • kwarg support extended to allow for b2JointDef creation. In the past, you would have had to do b2RevoluteJointDef.Initialize(bodyA, bodyB, anchorpoint) to get all of the necessary elements properly set, but now the same is possible with b2RevoluteJointDef(bodyA=a, bodyB=b, anchor=pt, other_kwargs=blah).
  • Additional properties for polygon shapes, so that b2PolygonShape(box=(2.0, 2.0)) is sugar for:
    s=b2PolygonShape()
    s.SetAsBox( (2.0, 2.0) )

    (or in the current version, s.box = (2.0, 2.0) )
  • You can do: point in b2AABB to see if a vector is inside an AABB.
  • Unit tests. You should be able to tell just after installation whether or not the library is working properly.
  • A mechanism so that you don't have to keep a reference to the callback classes (e.g., b2DebugDraw). In most cases you would want to have access to it, but it shouldn't crash anymore without it being stored on the Python side. [still need to test this more to ensure it works properly]
  • dir() listings are clean for almost every class, so you should be able to see what's possible with a specific
  • Improved vector and matrix classes, with more operator support. b2Vec2s are also indexable -- so: b2Vec2(1,2)[0] = 1
  • b2Color has also been enhanced to allow for operators, and useful things like b2Color.byte to get color components clamped in the [0,255] range are also there.
  • getAsType() is no longer necessary. Joints and shapes are properly downcast in all instances.
  • Plenty of other things from the new C++ code.
  • I've been doing my best to keep everything accessible in properties when possible.
  • Unused classes are also hidden, and the rest have been gone through and at least basically checked for accessibility/usability.
  • [... and all of the other stuff that I've forgotten.]
Note that this version is largely source-incompatible with older versions.

You can try out this SVN version with:

svn checkout http://pybox2d.googlecode.com/svn/branches/box2d_2.1/ pybox2d
cd pybox2d
python setup.py develop test
cd examples
python test_ApplyForce.py

Or just browse the code here.

Keep in mind that this will overwrite your current installed version. The testbed is likely to go through more changes, but the basic API shouldn't be changing much more from now on (unless something unexpected comes from the upstream library).

Like it? Hate it? Lemme know.

No comments:

Post a Comment