This project has moved and is read-only. For the latest updates, please go here.

How to emulate hole?

Topics: Developer Forum
Dec 3, 2010 at 9:02 PM

I need to emulate a hole to lock a rolling ball. The hole behavior should be:

  • if moving ball cross the hole with small velocity/applied forces, it should be locked in hole;
  • if forces, applied to the ball, exceed some threshold, ball should be unlocked and continue movement.

What is the best/easy/smart way to do this?

P.S. Andy Beaulieu suggested to use a breakable joints for that ( but I can't find any example...

Dec 3, 2010 at 9:39 PM

I'd suggest using something like the Gravity Controller. Does pretty much exactly what you are trying to achieve. You can set various points, which act as gravity wells and you can specify a max radius around each point, so that bodies further away than the radius are not affected.

Dec 3, 2010 at 9:56 PM

As for the breakable joints. You can use GetReactionForce() on the joint to get the amount of force the joint is subject to. It returns a vector you can get the length from - just check if this length is over a certain threshold. If it is over the limit, you dispose the joint and the ball is free to move again.

Note: This behavior has been implemented in the upcoming FPE 3.2

Dec 3, 2010 at 10:56 PM

To Elsch: As for gravity, hole doesn't have a "gravity" at all (if it's not a black hole! :) ) I've get your point but can't imagine a physical and code implementation to realistically emulate the hole "behavior". Could you provide a simple example? 

To Genbox: Actually, I using the Physics Helpers for WP7 , not sure on which version of Farseer they built. Anyway, could you provide an example? I'm new in Farseer Engine and don't understand completely how the joints can help me...

Dec 3, 2010 at 11:12 PM
Edited Dec 3, 2010 at 11:12 PM

Well actually your holes have gravity. Everything that comes close to a hole is pulled towards the center of the hole and you need to overcome this force to leave the hole again. Just have a look at the GravityController. You could copy the part were the forces between the bodies and the supplied points are calculated. Maybe i try to assemble a simple example until tomorrow.

Dec 4, 2010 at 2:40 AM

I know the PhysicsHelper project and Andy behind it (it uses FPE 3 by the way). He suggested that you place a sensor around the hole and once the body comes in contact with the sensor, you create a joint to make the ball stay in the hole until something hits it.


Your hole is 1 meter in radius. You create a circle body with IsSensor = true to make it a sensor (sensors only reports collisions) and once the ball comes in contact with the sensor, you check if the velocity is below the threshold that you defined. If the ball is below the threshold, you create a fixed distance joint (FixedDistanceJoint) between the hole center (world coordinates) and the ball. Set the length of the distance joint to be 0, and it will automatically pull the ball to the hole (you can even make it pull it slowly to the center of the hole by setting the damping factor on the joint).

Now you need the ball to be released from the hole when something hits it - this can be done by disposing the distance joint once it has reached a specific threshold. You use something like this: DistanceJoint.GetReactionForce().Length > 1000 where 1000 is just a number that you tweak to fit your needs. When that condition is true, you dispose the joint and the ball is free to move again.

If you need some help on how to use FPE, I suggest you take a look at the samples.

Dec 4, 2010 at 3:03 AM
Edited Dec 4, 2010 at 5:43 AM

Genbox, thanks a lot! Will try this weekend and report results.