Page 1 of 1

Attract bodies to each other

Posted: Mon May 23, 2011 3:33 pm
by imsammyd
Good day all.

I have a case where I have a static, horizontal line defined. Based on an event, I push new bodies onto the line so they are arranged horizontally. I use cpBodyApplyImpulse to push the new bodies in from right to left. What I really want is to have all the bodies attracted to each other like magnets so once a new body gets pushed in, they all stick together and the last ones don't drift off. Once too many objects get added, I do want the last ones to fall off the cliff.

Is there a way to define attraction or per-body gravity?

Thanks!

Re: Attract bodies to each other

Posted: Mon May 23, 2011 5:06 pm
by slembcke
Yes, but you have to implement it yourself by either calculating the forces and applying them to the bodies each frame or by overriding the velocity update function to do it. Look at the "Planet" demo in the Chipmunk distribution, it shows how to do gravity like that. To do gravity with multiple objects you just add up the values.