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

Planetary Gravity

Topics: User Forum
Aug 5, 2009 at 2:40 AM
Edited Aug 5, 2009 at 4:34 AM

I have looked through the threads and some of the simple tutorials and I have noticed there really is not to much on how the planetary gravity works.  As it stands right now me and a friend are developing a game with the farseer engine.  We managed to get the planetary force working, but there seem to be something off.  In order to get the gravity to actually work our strength value has to be upwards of 500000, which seems a bit extreme to me.  I thought I would raise a quesiton to see if this was off.  


Besides this I was wondering if there was a way to make it so that the planetary force only be exerted on select objects.  For instance I have the player object  which is supposed to be pulled in by the gravity and another object "a bat the players companion" which floats around him but does not collide with anything or have gravity enabled.  Well the bat does not collide with the gravity orb object but it is pulled in by the force.  So would there be a way to just shut off the planetary gravity for just the bat object.


As well as I'm going to just throw up my code for how I got it working so that incase anyone else is looking for how to get it to work, cause had to hunt for a couple hours to find everything I needed.  this code is for xna:

FarseerGames.FarseerPhysics.Controllers.GravityController gravity;

gravity = ComplexFactory.Instance.CreateGravityController(World.Instance.PhysicsSimulator, bodyList, FarseerGames.FarseerPhysics.GravityType.Linear, 750000, 300);


bodyList is a list of type body.


One other thing the guy I'm working with said he couldn't get the distanceSquared function to work instead of the Linear.  Not sure if its a glitch, but any heads up would be nice. thnx

Aug 5, 2009 at 5:47 PM

A user contacted me a while back with some changes to the GravityController that looks similar to what you need. They have been implemented, but only reside in the source control for now. They will be a part of the next release.

As for the distance squared method - there were some calculation problems where the acceleration of gravity was used as the force of gravity, that is fixed. But the distance squared method also needs a lot higher strength value that the linear method. Try downloading the updated gravity controller and see if it works for you.

By the way, body.IgnoreGravity is now used in the gravity controller.

Aug 5, 2009 at 8:45 PM

okay I got the distance squared method working after inputting a bigger value around 50000000.  One other thing when you say body.IgnoreGravity is used you mean if I was to use the said object with a BodyFactory and say body.IgnoreGravity then that would apply to the ComplexFactory for the planetary gravity variable gravity?  Or are you implying something else, cause I tried that already and the planetary gravity still effects the bat object. Thnx for the help, much appreciated.

Aug 5, 2009 at 9:28 PM

The GravityController iterates all the bodies in the PhysicsSimulator.BodyList and applies gravity to them. Even if the bodies have Body.IgnoreGravity = true. However, in the new GravityController found here it does respect the IgnoreGravity property and it does not apply gravity to those bodies.