hi. i was in need of very simple (yet usable for something more than "hey, this is how Big Players doing physics!") 2d physics engine for some of my pet projects, and i decided to use Box2D Lite as base (mostly 'cause it has simple joints implemented). so i ported it, mutilated it and had it working in less than 40 kb of source code. so far, so good. but suddenly...

but suddenly i realised that B2DL can operate only on boxes (no other body shapes were implemented), and it has O(N^2) broad phase. of course, this is perfectly fine for a demo purposes, but little limiting for my projects. so after mutilating the patient, i surgically enhanced it a little[1].

now my port supports the following features:
* convex polygonal bodies with different density
* SAT collistion detection
* hard and soft joints
* friction
* O(N*logN) broad phase
* source size is still ~65 KB

this is not a finished physics engine, of course, and you will have to do some more work to make it usable in real apps (like adding contact callbacks, for example), but it can give you something you can start with, still not really hard to follow, but more complete than original Box2D Lite.

among other things my port has (almost) standalone implementation of Dynamic AABB Tree, which powers new broad phase. even without further optimizations (like frozen bodies), it allows me to process 1100 bodies in less than 30 milliseconds. don't even try that with O(N^2) broad phase! ;-)

even if you aren't interested in physics engine per se, you can rip (and tear ;-) this implementation and use it in your projects. any project that needs some form of fast spatial selection (either 2D or 3D, the implementation supports both) can benefit from using dynamic balanced trees for that. it is based on "real" Box2D AABB Trees, and is fairly efficient (and taken from another project too ;-). it is using malloced pool of nodes internally, so it should be suitable for nogc realtime rendering.

you will need my iv.vmath[2] library for vector math. physics library doesn't draw anything on it's own, but the sample does, so it requires my iv.glbinds[3] and simpledisplay from Adam's arsd[4].

the code itself is not the cleanest one, but it is still readable (i hope), and should be easy to follow and modify.

good luck and happy hacking!

p.s. it looks like Chipmunk library has the same origins (and genesis, at least to some extent ;-).

[1] http://repo.or.cz/b2ld.git
[2] http://repo.or.cz/iv.d.git/blob_plain/HEAD:/vmath.d
[3] http://repo.or.cz/iv.d.git/tree/HEAD:/glbinds
[4] https://github.com/adamdruppe/arsd

Reply via email to