The problem is not with the GetGravEffect method (it gives the correct results), but with the update method. He completely ignores physical laws. You cannot summarize the values ββreturned by GetGravEffect and take it as speed. GetGravEffect returns the strength that one object attracts another. You must summarize these forces, and then perform additional calculations, which include inertia, acceleration and time to calculate the resulting speed. Also, distinguishing between X and Y from int is not very good, because you lose most of the accuracy, especially for slow speeds. Using the following fixed method, it works fine:
public void Update() { Vector Force = new Vector(0, 0); foreach (GravObject g in Form.Objects) { if (this != g) Force += GetGravEffect(g); } double TimeElapsedSinceLastUpdate = Form.Timer.Interval * 0.001; Vector acceleration = Force / Mass; Velocity += acceleration*TimeElapsedSinceLastUpdate; X = (X + Velocity.X * Form.VelocityScale); Y = (Y + Velocity.Y * Form.VelocityScale); if (X + Mass * Form.DrawScale >= Form.Panels.Panel2.Width || X - Mass * Form.DrawScale <= 0) Velocity.X *= -1; if (Y + Mass * Form.DrawScale >= Form.Panels.Panel2.Height || Y - Mass * Form.DrawScale <= 0) Velocity.Y *= -1; }
source share