Start Gravity on Button Click

Jul 23, 2009 at 4:17 PM

I have a PhysicsControllerBehavior attached to my LayoutRoot Canvas, a static rectangle for the groud with a few UserControls with the PhysicsObjectBehavior.  When I set the GravityVertical to 500 then the objects fall correctly.  What I want is to set the GravityVertical to 0 so that the objects stay "floating" and then when the user clicks a button to set the value to 500 so they fall.

I can get access to the PhysicsController in code:

<font face="Consolas" size="2"><font face="Consolas" size="2">

physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty)

</font></font><font face="Consolas" size="2">



as PhysicsControllerMain;

But if I change the GravityVertical, then noting happens - the objects continue to float.

How do I get the gravity to start on a button click?

Jul 23, 2009 at 5:55 PM

You'll need to use the Simulator property on the physics controller, which gives direct access to the Farseer Physics simulator class. (Unfortunately the PhysicsControllerMain doesn't have setter's wired up for every property)

First make sure you add references to the FarseerPhysics.dll and Spritehand.PhysicsBehaviors.dll.

Then import their namespaces:

using Spritehand.FarseerHelper;
using FarseerGames.FarseerPhysics.Mathematics;

Then, you could do something like so, which sets the Vertical gravity to 500:

PhysicsControllerMain _physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) as PhysicsControllerMain;

_physicsController.Simulator.Gravity = new Vector2(0, 500);