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

PhysicsSimulator.Gravity doesn't assign properly?

Topics: Developer Forum
Oct 23, 2008 at 6:08 PM

This is probably something really obvious, but I'm having problems setting the PhysicsSimulator.Gravity during the game. After some point, the assignment doesn't seem to "take."

physicsSimulator = new PhysicsSimulator(new Vector2(0, 100));

works fine, as well as immediately after the instantiation:

physicsSimulator = new PhysicsSimulator();

physicsSimulator.Gravity = new Vector2(0, 100);

BUT, later, during the game, if i try to assign Gravity like so:

myScreen.PhysicsSimulator.Gravity = new Vector2(0, 100);

Nothing happens. Debugging shows the _gravity returns to (0, 0) somehow. What am I missing?


Oct 23, 2008 at 6:39 PM
Edited Oct 23, 2008 at 7:01 PM

Are you sure that you are not resetting the gravity somewhere? I've just tested it to make sure, and it seems to work fine.
If you open Demo2 from the XNA samples, and change the HandleKeyboardInput() method to contain this:

PhysicsSimulator.Gravity =
new Vector2(-100,0);
if (input.CurrentKeyboardState.IsKeyDown(Keys.S))
PhysicsSimulator.Gravity =
new Vector2(0, 100);
if (input.CurrentKeyboardState.IsKeyDown(Keys.D))
PhysicsSimulator.Gravity =
new Vector2(100, 0);
if (input.CurrentKeyboardState.IsKeyDown(Keys.W))
PhysicsSimulator.Gravity =
new Vector2(0, -100);

Instead of the original if statements, you will have it working too.


Oct 23, 2008 at 7:56 PM
hmm, i set a breakpoint inside the Gravity setter, but was pretty sure it was only being set by my gravity controller. is there anything that would change the local variable inside PhysicsSimulator? i did a solution-wide search for "gravity" and, like you said, it seemed pretty straightforward - nothing hidden playing with it.

i'll look more when my code is in front of me, but thanks! just making sure gravity didn't have to only be set at instantiation time.
Oct 23, 2008 at 8:00 PM
What version are you using? I'm using Farseer Physics 2.0 alpha for this test.

Hope you will find your problem.
Oct 23, 2008 at 10:34 PM
Ok, i fixed it! I had upgraded to 2.0 Alpha as well - but just the engine, not the demos (which i had already personalized to my liking). Anyway, apparently the GameScreen class has its own PhysicsSimulator for some reason, so the DemoScreen has two PhysicsSimulators - only the public property refers to the one that's not actually in use. This may have been fixed in the 2.0 alpha demos, but i didn't recieve the benefits of it : ). I simply stripped out anything to do with PhysicsSimulator from GameScreen, because why does the MenuScreen or PauseScreen (which both extend GameScreen) need a simulator anyway?

Hope I have helped anyone else who has been having the same sort of problem...
Oct 23, 2008 at 10:45 PM
I'm glad that your problem got solved. We fixed a lot of bugs in 2.0.

The samples from 2.0 has been cleaned up and made easier to understand. The PhysicsSimulator was moved to the GameScreen class, and as the note above it states, it should have been in a GameEngine class along with all the other resources.

If I remember correctly, the physics simulator was in each of the demos before. We just moved it up one level to make the demos more simple. Even if the physics simulator is instanciated before it's used, does no real harm. It has no workload.

It could even be a good thing since you then are able to pre-load bodies/geometries already in the main menu.

The samples are only examples of how Farseer Physics can be used. We do realize that a lot of people use the samples stucture in their own games, so we try to make it as flexible as possible. You are welcome to edit all you want :)