Repulsive forces

Jun 19, 2010 at 10:03 AM


I'm new to Box2D (XNA). What I want to do is connect bodies to their ideal positions by using springs. Then I want to add some kind of repulsive force to each body, to avoid overlapping bodies as much as possible. How do I create such forces?


Jun 19, 2010 at 6:01 PM

I'm not sure what your overall intent is, but the physics engine itself will reply repulsive forces to keep bodies from overlapping (assuming you haven't modified their collision group).  The engine will iteratively try to move bodies away from each other (slightly) if theya re overlapping until they are no longer overlapping wich is the same as having a force pushing the bodies outside of each other.

As for connecting bodies, you'll want to look at using a joint.  It sounds like you might want a distance joint but other kinds of joints may be more appropriate.  You can connect bodies together using joints and the engine will respond based on the joint type and other forces on the bodies.  In terms of getting a spring-like feel for the connection you may need to play around with the various joints and parameters until something feels right.

Jun 20, 2010 at 7:46 PM

Yeah, just noticed that too. Thanks for your answer, anyway.