descriptionvery simple Box2DLite based physics engine
last changeWed, 2 Nov 2016 09:46:09 +0000 (2 11:46 +0200)
content tags
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

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

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 ;-).

p.p.s. the next things you'll probably want are complex bodies and more
       joints. joints "aka constraints" code can be modelled after Chipmunk
       code, as Chipmunk solver is very similar to the solver used here.
       as for complex bodies, you can decouple shape from body itself, and
       make body out of several shapes. you have to collect and process
       contacts for all shapes (ignoring self-contacts, of course), and
       let the solver do the rest.


Ketmar Dark // Invisible Vector
2016-11-02 Ketmar Darkadded some comments to READMEmaster
2016-10-31 Ketmar Darkcosmetix
2016-10-31 Ketmar Darkfixed box2d url
2016-10-31 Ketmar Darkadded README. lol.
2016-10-31 Ketmar Darkremoved some unused imports
2016-10-30 Ketmar Darkcosmetix
2016-10-30 Ketmar Darkadded some caching and inlining; seems to be slightly...
2016-10-30 Ketmar Darkmore cosmetix, lol
2016-10-30 Ketmar Darkeven more cosmetix
2016-10-30 Ketmar Darkcosmetix
2016-10-29 Ketmar Darkbody position and rotation are hidden behind getters...
2016-10-28 Ketmar Darkvanilized a little
2016-10-28 Ketmar Darkarbiter is a struct now
2016-10-27 Ketmar Darkcosmetix
2016-10-27 Ketmar Darkcosmetix
2016-10-27 Ketmar Darkremoved some unused code
22 months ago k8
22 months ago master