This project has moved. For the latest updates, please go here.

GravityController: Linear vs DistanceSquared bug?

Topics: Developer Forum
May 9, 2011 at 6:44 PM
Edited May 9, 2011 at 6:55 PM

Hi! Been playing with Farseer 3.3.1 for a few days now, pretty impressed so far, but i did notice something odd in the GravityController. My assumptions are as follows: For Linear you want to divide strength by distance, and for DistanceSquared you want to divide strength by distance^2. The GravityController actually does the following:


    switch (GravityType)
        case GravityType.DistanceSquared:
            f = Strength / r2 / (float)Math.Sqrt(r2) * body1.Mass * d;
        case GravityType.Linear:
            f = Strength / r2 * body1.Mass * d;


First problem, i don't understand why DistanceSquared is implemented as it is. Second problem is that r2 is defined as follows:

float r2 = d.LengthSquared();


So as i read it, Linear is actually behaving as DistanceSquared should, and DistanceSquared is using strength/distance^3, or behaving as DistanceCubed if you will ;)

May 9, 2011 at 11:57 PM

That bit of code might seem a bit strange, but the math actually works for calculating the expected force. The thing to keep in mind is the numerator includes the non-normalized components of the d vector, which the extra exponent you noticed in the denominator will cancel out resulting in the expected scaling.

For what it's worth I had to think about that a bit when I saw it the first time because it looked odd to me also.


May 10, 2011 at 5:47 AM

Ah, right you are. Thanks for clarifying!