Wednesday, February 25, 2009

2.0.2b1 Released!

Since 2.0.2b0, there have been a good deal of changes to pybox2d:

  • Code structure completely reorganized
  • Doxygen comments converted to docstrings (should be a bit more friendly now, but still a bit C++'ish in places)
  • Lists and tuples may be used anywhere in place of b2Vec2's, and all of the tests have been updated to reflect this
  • Save/load state (pickling support for worlds)
  • Bug fix: Seg faults during DebugDraw/etc callbacks
  • Bug fix: userData reference counting causing leaks
  • Bug fix: getType() didn't work on line joints
  • Bug fix: TestSegment now returns from (-1,0,1) and not a bool
  • b2PolygonDef.setVertices() added, supports either b2Vec2 or list/tuple, so no need to specify. Old setVertices_tuple/b2Vec2() are deprecated.
  • New pretty printing style. It takes up a good deal of space, but it's actually readable.
  • New examples: bezier edges with thin line segments, a simple belt, basic pickling example (might needs some updating)
  • (Optionally compilable) C++ assertion failures turned into Python exceptions
    Additional properties and accessors to make coding easier (see below)
  • Many tests were updated and rewritten to be cleaner
  • Added b2CheckPolygonDef and deprecated the Python ported version. This version adds (optional) additional checks to ensure that your shape is convex and properly sized to not have strange results
  • Added GetVertices() for b2EdgeShapes. Creating one b2ChainDef results in many b2EdgeShapes, so this properly loops through each connected shape and gets the vertices in order
  • Box2D source updated, b2GravityController fixed
  • A fix for not allowing b2Body/Joint/Controller/Shapes as dictionary keys. Don't know how I missed this one. Still won't be picklable unfortunately.
  • Basic iterators have been added: b2World (iterates over bodies), b2Body (iterates over shapes), b2Controller (iterates over bodies), b2PolygonShape (iterates over vertices)
There are several code-breaking features that you might run into:

  • The library is now called Box2D (and not the cumbersome Box2D2)
  • Controllers now follow the factory style (see the buoyancy test for more information)
  • b2Distance updated (see here if this affects you)
  • b2Body.GetShapeList() used to return only first shape, now returns actual list
  • b2World.GetBodyList/Joint() used to return only first body, now returns actual list
  • All occurences of the ugly 'm_' have been removed. This might require some changes in your code, since this applies to all b2Joint.m_* and others, not just testbed stuff.

The following are the additional properties added. Most are just for convenience and make the definition (e.g., b2ShapeDef) symmetric with the output (e.g., b2Shape). Ones with * are changeable; the rest are read-only:
b2Worldgravity*, jointList, bodyList, groundBody, worldAABB, doSleep
b2Shapefilter*, friction*, restitution*, density*
b2Jointtype, userData, body1, body2, collideConnected
b2CircleShape radius, localPosition
b2PolygonShapevertices, coreVertices, normals
b2Body massData*, position*, angle*, linearDamping, angularDamping, allowSleep*, isSleeping, IsRotationFixed, isBullet*, angularVelocity*, linearVelocity*, shapeList

Basic epydoc documentation is now available here. The testbed is no longer included in the installer, so please download it separately here.

Releasing for all these operating systems all by myself is time consuming, confusing, and tough at times. I'm sure I got something wrong, so please go easy on me. :) Do let me know if something doesn't work for you, though.

Enjoy!

Tuesday, January 20, 2009

Update on Pickling

As of this post, the latest svn revision, r148, has pickling support which is working great on the testbed. I'm rather excited about it, as it's somewhat of a powerful and unique feature.

For those who can compile the library, check out the testbed. It works like your regular quicksave (F5)/quickload (F7) system. There's an additional test, test_pickle.py, that shows how easy it is to load a pickled world. As far as the code itself goes, there's still some commenting to be done, but it should be readable.

Monday, January 12, 2009

SWIG and Pickle

I have yet to see any site that explains how to pickle swig objects well enough. SWIG objects exist both on the Python and C++ side.

Without calling the __init__ function on the Python class instance of a SWIG object, it doesn't exist as far as SWIG is concerned. So you'll get a TypeError on the 'self' parameter of your functions.

If we were using old-style classes, __getinitargs__ would work well enough. Since we aren't, as far as I can tell, it's necessary for us to call the initializer ourselves.
def generic_setstate(self, dict):
self.__init__()
for key, value in dict.iteritems():
setattr(self, key, value)

def generic_getstate(self):
return dict((var, getattr(self, var)) for var in
__pickle_vars__[self.__class__.__name__])
If you have the dict __pickle_vars__ set up such as:
__pickle_vars__ = { 'myclass' : ['x', 'y'], 'myclass2' : ['zz'], ... }
You can then set:
myclass.__getstate__=generic_getstate
myclass.__setstate__=generic_setstate


EDIT 2009/1/14:
If you use properties for all those C++ getters/setters (as you should be doing), you can then iterate over all of the properties for a much cleaner solution. It takes even more tweaking to get pickling to work with outputs from factory products (e.g., trying to pickle a b2Body instead of a b2BodyDef). Expect something like this to show up soon in pybox2d. :)

Friday, January 9, 2009

pybox2d svn r136

Added something I've wanted to for a long time.

Anywhere there's a b2Vec2, you should now be able to pass in a [length-2] list, tuple, or None -- (0,0). As I learn more about SWIG every time I try to throw some more functionality into Box2D.i, I'm more and more impressed with how much can be done with it.

Changes can be seen in test_ApplyForce.py and test_Web.py. I'll eventually update the rest of the testbed examples.

Oh yeah, also fixed a longtime standing bug with unset userData causing seg faults.

Any other things you can think of that would be convenient? I'll probably allow matrices in list format.

If you notice any side-effects, please let me know as always.

Friday, January 2, 2009

distutils, oh how I hate you...

It took me forever and a day to get the original setup.py working for pybox2d, and then some people came along and told me how it was unnecessarily complex. And that it didn't work in the standard way and was somewhat confusing. Great.

With a push from the user snickell, I worked on restructuring the code-base to get it to conform to standard distribution methods.

I've decided that I will no longer install the testbeds and things to c:\Python2x\Box2D for Windows users and will simply just offer them in a separate archive as I see some other libraries do.

*EDIT*
My problem has been resolved and everything is apparently working now. I'm ready for another release -- but with nothing really new on the Box2D end, I guess there's nothing to do but wait.

A few questions for you faithful few who reload this page every once in a while:
Windows users: Do you prefer MSI's or executables for your libraries?
Linux users: Any interest in an RPM distribution?

Any other comments/etc. about the releases are always welcome.

Thursday, October 23, 2008

2.0.2b0 Released!


With nearly no response from the community, this time-consuming project might end at this version.

I'm also hoping that the OS X installers work well enough for everyone. It took a good amount of time to figure out how to (properly?) release for it.

In any case, here's the changelog:
First release that's not based on a major Box2D release. It combines Box2D SVN r177 and contributions from shaktool (thin line segment) and BorisTheBrave (controllers/buoyancy).

* Thin line segment support (forum post)
* Buoyancy with generic controller support (forum post)
* Pyglet 1.1 testbed (still has some issues. run convert-from-pygame.py to convert the tests from pygame)
* OS X Installer
* Python 2.6 support
* Line joints (see LineJoint test)
* Raycasts (see RayCast test and BoxCutter test)
* TestSegment support
* BreakableBody test (forum post)
* Fixed: == was working, but != comparisons weren't.
* Access to polygon normals, core vertices, etc.
* cvar list fixed
* Off by one bug fixed for vertices

New time step will require a few minor updates to your code:

Old:
world.Step(timeStep, iterationCount)

New:
world.Step(timeStep, velocityIterations, positionIterations)

velocityIterations is usually 10, and positionIterations is usually 8.

Friday, October 10, 2008

pybox2d r86 - Thin Line Segment Shapes

 


Thanks to shaktool, thin line segments are now available for Box2D! I've ported them to pybox2d, which you can see if you use svn r86. It's still in beta in the C++ form, so API changes can be expected.

This is great news for creating platformer games, and even terrain in general. No longer will you have to worry about tesselating non-convex shapes to get the slopes and such that you want. See the new testbed demos,
test_DynamicEdges.py
test_PyramidStaticEdges.py
test_StaticEdges.py

They are very simple to use, also:

(Assuming a b2Body, body, at the origin)
verts = [ (50.0,0.0), (-50.0,0.0) ]
edgeDef = box2d.b2EdgeChainDef()
edgeDef.setVertices(verts)
edgeDef.isALoop = False
body.CreateShape(edgeDef)

Creates a big, static line at the bottom of your world. If you want to make a dynamic one, simply use body.SetMassFromShapes() afterward. However, there are some problems with the dynamic versions (see the forum post)