Article

Switching to Box2D

Posted by Tim Kerchmar.

PublicCategorized as Public.

Not yet tagged

Hello,


They say, if it ain't broke... But I couldn'thelp it when Box2D 2.0.0 came out with the killer feature, continuouscollision detection. By now, Carrot Run has moved along from a smallprototype to a medium sized project, and the conversion ended up beingmore late nights than I had originally presumed.


Chipmunkwas branched from Box2D, but now I can see how different thedevelopment philosophies are for the two libraries, and how much thatnon-technical stuff that many other software blogs write about actually affected Carrot Run's development.

  • Erin wants to make a full featured 2D physics engine. Scottwants to make an optimized 2D engine, focussing on the features thathis games require.
  • Box2D contains native support for motors, joints, ect. Chipmunk is very easy to integrate with other languages.
  • Box2D makes few assumptions about the type of game that the userwill be creating, and uses broadphase algorithms that are resiliant.Chipmunk is very very fast when carefully tuned.


I don't get the impression that Box2D is bloated.I get the impression that Erin's limited time (remember that he writesBox2D when not asleep or at Blizzard writing cool physicsstuff for the mutalisk) is spent on features and usability. I thinkthat Box2D is Erin's pet project, and its forums are certainlygarnering a passionate following with a few very technically adeptusers among them, who seem to debate approaches and submit largepatches to Box2D.


Occasionally,someone posts on a forum that Chipmunkis more polished than Box2D, which is odd considering Box2D's focus onfeatures and usability. But it is true that Chipmunk's samples lookbetter. As it turns out, its a matter of Chipmunk's optimizationsmaking it look better. I think that Chipmunk's samples shipped with a180Hz timestep, and 15 iterations per update. Box2D is using 60Hz and10 iterations per update. Chipmunk's obsessive use with hashtablescertainly pays off. It seems kind of funny to suggest that otheralgorithms are more resiliant than a hashtable, but it is kind of true.If you do not tune the spatial hashing mechanism in Chipmunk, yourapplication can lose out on the net win.


Onething that surprised me about Box2D was that Erin ships it with its ownsmall object allocator. Chipmunk did seem to use heap allocation alot,perhaps on a per frame basis, so I'm not surprised to see my memoryusage drop. Constant heap allocation in a C++ program generally meansthat memory will get utterly fragmented, which would certainly limitChipmunk's usefulness on the PS3 or XBox. And if you have seen theLittleBigWorld videos, you know how cool 2D physics are. Nothing inLittleBigWorld actually requires the physics engine to know about a Zaxis, even though the game is rendered in 3D and does a few things thatSuper Smash Brothers never did.


I also perused theBox2D forums pretty heavily to learn how Erin did continuous collisiondetection without having to actually read his code. As it turns out,like everything else he does in Box2D, he took a deeply pragmaticapproach (meaning that he does what works well, not what is best in theideal world). I suspect that his experience in the game industry,writing games, taught him the benefits of this. I work for a gamesmiddleware company, so the lesson of "do what works now, right now"isn't as well drilled into my head. We have to consider all thewhat-ifs. Erin doesn't have paying customers, so releasing a patchevery few weeks is just fine. I'm a little sad for ToyBox (my ownattempt at 2D physics, with perfect everything, very idealist, wouldprevent penetrations 100% of the time, ran at 0.1FPS on dual 3.4GHzPC), but kind of happy that I can just use someone else's solution andnot worry about tunnelling ever again.


Box2D's CCDuses a binary search to determine time-of-impact for collisions. ToyBoxused numerically unstable polynomial root finding algorithms that hadO(1), but ended up being unstable enough to cause endless problems. Thebinary search algorithm seems to run just fine and the engine seemsvery tolerable of penetrations, which is also very nice.A spatialhashing broadphase would be easy to add to Box2D. The only change fromChipmunk's spatial hashing is that you have to hash the boundingrectangle for an object's swept motion, not just the object's currentposition.


One place that Chipmunk really shined wasease of integration with a dynamic language. Most languages come with amethod for integrating with C, and the good foreign function interfacesprovide a way to directly access memory in a struct. Because Chipmunkwas written in C, symbols were left unmangled and were easy to wire upto be called from Lisp, and there were no invisible this pointers or vtables hidden in memory and function calls. It was a huge pain in the butt to convert my physics wrapper to work with Box2D.


Scott, best wishes on your game! I always did love the meteor smash videos.


Arrow_down Hide comments

The Night School, LLC, empowering our users to create and play!

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy | Support | Feedback | Help Center |