Saturday, January 16, 2010

So, I've gotten this far...

I have spent a good month now redoing everything in the project, but I've hit a bit of a wall. It's pretty much in a "good enough" state for a release, minus a few of the testbed examples I have yet to port. Oh, and all of the documentation. Ugh. Now with Box2D's wiki down, it makes it even more difficult.

There's that old adage "release early; release often", but I find it hard to jump on that bandwagon. Releases are very difficult to do in the first place for multiple platforms and multiple versions of Python. And then support them afterward? Forget it. Oh well. I might continue at a slow pace for a while and wrap some more things up for a release whenever I manage to get a bunch of free time again, or maybe not. There's a bunch more changes on the SVN since the last blog post if you have any interest.

Perhaps this will take over in the meantime?

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:
                        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.SetAsBox( (2.0, 2.0) )

    (or in the current version, = (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 pybox2d
cd pybox2d
python develop test
cd examples

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.