Attract bodies to each other

Official forum for the Chipmunk2D Physics Library.

Attract bodies to each other

Postby imsammyd » Mon May 23, 2011 3:33 pm

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?

Posts: 1
Joined: Mon May 23, 2011 3:24 pm

Re: Attract bodies to each other

Postby slembcke » Mon May 23, 2011 5:06 pm

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! ->
User avatar
Site Admin
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 2 guests