modifying Gravity in Code

Jul 26, 2010 at 1:08 AM
HI, I need (want?) to modify gravity dynamically, For example I have windows Phone game where i have a ( variable) bunch of objects on the screen and I want them to roll as the the Phone is tilted or rotated? what is the best way to do this? Thanks William
Jul 26, 2010 at 1:57 AM
Hi William, check out the sample below, it does just that (changes gravity depending on accelerometer in WP7)
Jul 26, 2010 at 9:05 AM
 Thanks Andy,
Much appreciate the quick reply.

On 26 July 2010 01:57, AndyBeaulieu <> wrote:

From: AndyBeaulieu

Hi William, check out the sample below, it does just that (changes gravity depending on accelerometer in WP7)

Read the full discussion online.

To add a post to this discussion, reply to this email (

To start a new discussion for this project, email

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at

Dec 10, 2010 at 10:09 PM

Hey Andy, this is Jared Bienz with Microsoft. First I want to say that I'm a huge fan of the library. I've taught a few small Windows Phone gaming courses recently and students absolutely LOVE it.

I'm working on a sample like what you blogged about here:

In fact I'm trying to create a behavior that could easily be added to any application called the PhysicsAccelerometerGravityBehavior. I'm sure you can imagine what it does, but I also want to add code to deal with the User Idel timeout problem in accelerometer games.

Everything is pretty much wrapped up, but I'm having a problem with the physics engine. My gut feel it has nothing to do with the Blend behaviors so I may need to consult the Farseer team, but I wanted to check with you first.

The problem is with updating physicsController.Simulator.Gravity property on acceleromer reading changed. It updates fine for several seconds but eventually the simulation locks. Not the app, just the simulation. If I have a timer on the screen the timer keeps ticking and the accelermeter changed event keeps firing, but the simulation halts. It seems this happens when the accelerometer isn't moving much. Like if I set the phone down on a table and several updates to the Gravity property are made using close numerical values. If I keep moving the phone around I can't seem to make it halt.

I thought I was doing something wrong until I found your blog post above and downloaded your sample. My code does almost exactly what yours does with a few small exceptions:

  1. With the new Farseer engine, physicsController.Simulator.Gravity.X and physicsController.Simulator.Gravity.Y cannot be set. Instead, you must set physicsController.Simulator.Gravity, which is a Vector2.
  2. The numbers I'm setting on physicsController.Simulator.Gravity are significantly smaller, also because of the engine change (around 100 instead of 500).
  3. I'm not using the wrapper classes you showed for using the mouse as an option.

Other than that, the logic is the same.

If I run your sample with the old engine, the simulation never halts.  It just keeps going on forever.  I was hoping I might be able to send you my updated project and see if you could confirm this bug and if you know of a resolution. I realize you may not have a phone to test with and I can't confirm if the issue happens in the emulator since I'm not using your wrapper. But here's the link:

If you'd like to contact me directly, please feel free to use my jbienz at microsoft dot com address.


Dec 13, 2010 at 11:13 PM

Hi Jared,

Sorry I haven't had a chance to try this yet. In fact, my two apps that use the Accelerometer (Talking Ragdoll and DroppyPop) are both using the older Farseer 2.x engine, so it is quite possible you found an issue.

One thing that might be worth a try is to update the Farseer Physics 3 code to the latest version. The version included in the Physics Helper is an older build and I know the Farseer team has put in many fixes since then.


Dec 14, 2010 at 12:29 AM

Hey Andy. I started looking tonight at getting the Farseer engine updated with the PhysicsHelper library. The harder I tried the more I felt like getting deeper into the rabbit hole. :)

Did you manually create the "Farseer Physics Engine 3.0 WinPhone" project that is part of your library source or is that somethig distributed by the Farseer team? I couldn't find it anywhere. When I tried to get Farseer 3.1 building for Windows Phone I ran into several issues.

The first was that one of the decomposers is using HashSet<T> (which is only part of Silverlight 4). I just excluded that decomposer. But the second issue is that the Silverlight build redefines all of the XNA types (Vector2, etc.) and this caused collisions in the PhysicsHelper library.

Before I try to hack away at this any further, I want to make sure I'm not missing something. I hate to try and go manually get Farseer 3.1 running on Windows Phone if there is an official release somewhere, but I just haven't been able to find it.


Dec 14, 2010 at 12:34 AM

Jared, yes, sorry for the confusion.

There wasn't a WP7 specific build of farseer 3 when I first created version 4 of the physics helper, and it sound like it is still not available.

So yes, I ran into the same issues you are seeing and had to hack through.

Sorry for the frustration. I am busy this week until Thursday but I am hoping to free up to take a look at a few things including this at that time.


Dec 21, 2010 at 9:40 PM

Hello Jared,

the past two days I've been working on a prototype game using the accellerometer (thanks a bunch for the awesome samples both of you posted, coldn't have made it this far without them) and the Farseer engine 3.0 that Andy compiled (also thanks a lot for the physics helper, awesome stuff!) and ran into a similar problem. My "marble" would just stop moving after a while, despite the simulations gravity vector being correctly updated.

Maybe you mean something different and I just understood your description wrong, but is it possible the simulation isn't actually freezing and insteads its the objects that are being put into sleep state because of a lack of movement? At least that's what happened to me. Sleep mode seems to be a big performance boosting feature of Farseer 3.x+. This bit of code in the _physicsController_Initialized EventHandler fixed that behaviour for me:


           PhysicsSprite _playerBall = _physicsController.PhysicsObjects["playerBall"];
           _playerBall.BodyObject.SleepingAllowed = false;

We don't want our main actor to fall asleep, now do we? :)

Let me know if this helps and keep up the great work.



Dec 22, 2010 at 11:54 AM

Just a note that you can change the default Resting settings for the Farseer engine, but they are constants in the code so this requires a rebuild.

If you download the source code for the Physics Helper, and open the "Farseer Physics 3.0 WinPhone" project, go to the Common subfolder and open the Settings file. Change these values if necesssary:


/// <summary>
/// The time that a body must be still before it will go to sleep.
/// </summary>
public const float TimeToSleep = 0.5f; /// <summary>
/// A body cannot sleep if its linear velocity is above this tolerance
/// </summary>
public const float LinearSleepTolerance = 0.90f;  

// *ACK* FP3 DEFAULT IS 0.5f !!!

// *ACK* FP3 DEFAULT IS 0.01f !!!


/// <summary>
/// A body cannot sleep if its angular velocity is above this tolerance.
/// </summary>
public const float AngularSleepTolerance = (16.0f / 180.0f * Pi);

// *ACK* FP3 DEFAULT IS (2.0f / 180.0f * Pi) !!!


Jan 3, 2011 at 10:00 PM

That was exactly it Mark. Really glad you found that. Now I just wish there was an easy way to set SleepingAllowed through the PhysicsObjectBehavior. As it stands currently I have to wait for LayoutRoot_Loaded to get the PhysicsControllerMain, then I have to subscribe to PhysicsControllerMain_Initialized, then I have to know the names of each object I want to disable it on.

Andy, any chance you could add the Sleep related properties to PhysicsObjectBehavior in the next release? (SleepingAllowed, Awake, etc) I can put Marks workaround in my lab for now, but having these properties on PhysicsObjectBehavior would be much more straightforward. Plus, I have a feeling that sleeping will become more of an issue as accelerometer use increases. The accelerometer creates situations where objects move little to no distance at all (and get put to sleep).

Jan 3, 2011 at 10:02 PM

P.S. if you want to make me a contributor I can make the changes. But I don't know if you'd want to roll to a new release number for just that.

Jan 3, 2011 at 10:08 PM

Good idea, SleepingAllowed would be a useful property.

I think that is all that is needed because an object is always Awake at creation time. Let me know if you think there needs to be other properties though.

Jan 3, 2011 at 10:34 PM

Yeah, I was only suggesting adding Awake because it's read / write and because of this thread:

It seems there may be situations where you might want to "wake up" a sleeping body.

Jan 3, 2011 at 10:35 PM

Ahh... but if you wanted to wake a sleeping body you'd probably do it in code...

Whatever you think makes sense Andy.