patching chipmunk to do velocity verlet
A number of people tried to model chains last week using pymunk and ran into what appeared to be stability issues where chains would pull apart. ldlework suggested in the irc channel that someone should make a verlet physics library and python wrapper. As a result of that discussion I modified pymunk to do just that.Surprisingly it doesn't seem to keep chains from pulling apart. However stability does seem improved, as our chains freak out less (jump around less wildly) when stretched. This behavior makes me think that numerical stability in the integration method is not the issue and that the problem lives somewhere in the way joint augmentation is resolved.
You can find a copy here: http://www.physics.ohio-state.edu/~arossi/pymunk-verlet.tar.gz
You'll have to build the new libchipmunk in the chipmunk_src dir with:
gcc -fPIC -O3 -std=gnu99 -ffast-math -c *.c
gcc -shared -o libchipmunk{ARCH}.so *.o
Where {ARCH} is "32" or "64" on x86 based linux. Build instructions for Windows and OSX are similar but not included. The resulting .so or library needs to be copied into the included pymunk dir to replace the existing ones, which are old. The pymunk dir can then be dropped into any application or the appropriate system location. It may be necessary to disable the system copy to use a local copy of pymunk
A description of the changes follow:
- cpBody.h: the cpBody struct was modified to have a v_old and f_old variables.
- cpBody.c: the functions cpBodyUpdateVelocity, cpBodyUpdatePosition were changed to do velocity verlet
- cpSpace.c: the cpSpaceStep function was rearranged so that the velocity integration happens before position integration. (I am not sure of the full extent of this, it may have undesired consequences)
- _chipmunk.py: cpBody.__fields__ was updated to reflect the new additions to the cpBody struct.
My modifications have effectively disabled space-wide damping. A way to restore damping may be to change the line in cpBodyUpdateVelocity from
body->v = v_nxt; to
body->v = cpvmult(v_nxt, damping);
However I am unsure if this is correct.
Also: Special thanks to phren for reminding me that I should share this here.