Attract bodies to each other

Official forum for the Chipmunk2D Physics Library.
Post Reply
imsammyd
Posts: 1
Joined: Mon May 23, 2011 3:24 pm
Contact:

Attract bodies to each other

Post 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!
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Attract bodies to each other

Post 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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 15 guests