1 hi. i was in need of very simple (yet usable for something more than
2 "hey, this is how Big Players doing physics!") 2d physics engine for some
3 of my pet projects, and i decided to use Box2D Lite as base (mostly
4 'cause it has simple joints implemented). so i ported it, mutilated it
5 and had it working in less than 40 kb of source code. so far, so good.
8 but suddenly i realised that B2DL can operate only on boxes (no other
9 body shapes were implemented), and it has O(N^2) broad phase. of course,
10 this is perfectly fine for a demo purposes, but little limiting for my
11 projects. so after mutilating the patient, i surgically enhanced it a
14 now my port supports the following features:
15 * convex polygonal bodies with different density
16 * SAT collistion detection
17 * hard and soft joints
19 * O(N*logN) broad phase
20 * source size is still ~65 KB
22 this is not a finished physics engine, of course, and you will have to do
23 some more work to make it usable in real apps (like adding contact
24 callbacks, for example), but it can give you something you can start
25 with, still not really hard to follow, but more complete than original
28 among other things my port has (almost) standalone implementation of
29 Dynamic AABB Tree, which powers new broad phase. even without further
30 optimizations (like frozen bodies), it allows me to process 1100 bodies
31 in less than 30 milliseconds. don't even try that with O(N^2) broad
34 even if you aren't interested in physics engine per se, you can rip (and
35 tear ;-) this implementation and use it in your projects. any project
36 that needs some form of fast spatial selection (either 2D or 3D, the
37 implementation supports both) can benefit from using dynamic balanced
38 trees for that. it is based on "real" Box2D AABB Trees, and is fairly
39 efficient (and taken from another project too ;-). it is using malloced
40 pool of nodes internally, so it should be suitable for nogc realtime
43 you will need my iv.vmath[2] library for vector math. physics library
44 doesn't draw anything on it's own, but the sample does, so it requires my
45 iv.glbinds[3] and simpledisplay from Adam's arsd[4].
47 the code itself is not the cleanest one, but it is still readable (i
48 hope), and should be easy to follow and modify.
50 good luck and happy hacking!
53 p.s. it looks like Chipmunk library has the same origins (and genesis, at
54 least to some extent ;-).
56 p.p.s. the next things you'll probably want are complex bodies and more
57 joints. joints "aka constraints" code can be modelled after Chipmunk
58 code, as Chipmunk solver is very similar to the solver used here.
59 as for complex bodies, you can decouple shape from body itself, and
60 make body out of several shapes. you have to collect and process
61 contacts for all shapes (ignoring self-contacts, of course), and
62 let the solver do the rest.
65 [1] http://repo.or.cz/b2ld.git
66 [2] http://repo.or.cz/iv.d.git/blob_plain/HEAD:/vmath.d
67 [3] http://repo.or.cz/iv.d.git/tree/HEAD:/glbinds
68 [4] https://github.com/adamdruppe/arsd
71 Ketmar Dark // Invisible Vector
72 ketmar@ketmar.no-ip.org