I am trying to create a custom solar system for a research project, to test different numerical methods and see their difference.
We are making our own solar system instead of trying to simulate earth/sun since that seemed harder.
For now we have a sun M1 and earth M2.
- M1 = 3500
- M2 = 500
- distance from M1 to M2 = 200
- We use a different G = 0.2
When we calculate the gravitional force it should be, F = 8.75 or -8.75 (F = GM1M2/r^2)
When we calculate the constant speed the earth should have to orbit around earth we find v = 2 (v = sqrt(G(M1+M2)/r))
To calculate the vec2 of the force we use the follow code
static double GravitationalConstant = 0.2f;//6.674e-11;
static public Vector2f GravitationalForce(SolarObject onObj, SolarObject fromObj)
{
Vector2f result = fromObj.OldPosition - onObj.OldPosition;
float distance = result.Lenght();
Console.WriteLine(distance);
result = new Vector2f(result.X / distance, result.Y / distance); // Normalized, but I've the lenght al ready so
result *= (float)((GravitationalConstant * onObj.Mass * fromObj.Mass) / (distance * distance));
return result;
}
And to update the position/velocity we use this
public void Update(float dt, List<SolarObject> objects)
{
foreach(SolarObject s in objects.Skip(1))
{
Vector2f f = Utility.GravitationalForce(s, objects[0]);
Vector2f a = f / s.Mass;
s.OldPosition = s.NewPosition;
s.NewPosition += dt * s.Velocity
s.Velocity += dt * a;
}
}
The object does fly around the sun, but it's not an orbit at all. The distance between M1/M2 is not constant <-> the force is also not always equal to 8.75f. We know euler has an 'error', but this seems to big, since without even orbiting one circle 25% of the distance is al ready increased. So there has to be a bug somewhere.