Squirrel physics!

ohai! Recently I found myself needing some diversion again, so I decided to do some game programming again. Javascript has gotten a lot of nice libs for game programming lately, but physics engines somehow aren't quite there yet imho.

The only engines I know of in JS are ports of either Box2D or Bullet. My thoughts about them so far:

  • There are two Javascript ports of Bullet: (bullet.js and ammo.js) but I don't think they perform so well with many objects due to Bullet being a 3D engine.
  • Box2D also has two ports (Box2dJS and ). I just don't like the Box2D API, that's pretty much all. Box2dJS also is outdated and requires Prototype (for whatever reason).
  • With the exception of bullet.js all of these have been converted from C or Actionscript to Javascript.

The last point is especially important to me, because that means they will most certainly clutter the global namespace. I started using AMD script loaders for almost all of my projects in the last months and I really don't want to go back to this global mess.. (: So I decided to take a shot at doing a physics engine port that's AMD compatible and has a nice API.

The Chipmunk engine has a really nice API, so I chose to port that. After about two months of work besides work and exams I finally reached a point where most parts work and I feel comfortable to put it out.

I still consider it to be an early alpha version, so it's probably not recommend to use it in any serious project yet. Nevertheless, you can check out the code on Github.

It's not a 1:1 port, since Chipmunk is written in C and I wanted an object oriented library. But it's a really close port when it comes to naming things. For instance cpSpaceAddShape(space, shape) in Chipmunk became space.addShape(shape) (space being an instance of cp/Space) in Chipmunk.js. I think I managed to stay as close as possible to the method signatures of Chipmunk.

Alright, here's the current state:

Features:

  • Collision primitives for segments, circles and convex polygons
  • Uses fast contact persistence algorithm by Erin Catto (of Box2D fame)
  • Neat constraints like damped springs or motors.
  • Almost no dependencies. Chipmunk.js only requires (he he he) an AMD script loader.
  • No global namespace pollution. I'm not in your global, polluting your windowz. (o:

To do:

  • Sleeping bodies not supported. The code is already ported, but contains a crucial bug that leads to a freezing browser. More in this issue on Github.
  • Write some unit tests for all classes.
  • Almost no optimization for Javascript in place. Still very close to the original C code. I want to do that once all important features are done and tested.
  • Find a tool for JS code documentation that does not suck that bad and make some nice API docs.

Demos:

I made a page where I will put a few demos (two until now) here and other stuff such as API docs.

TLDR;

OOP-port of Chipmunk to Javascript; still alpha; Code here; Demos here; API docs coming as soon as possible.

blog comments powered by Disqus