Im trying to create a (poor) simulation of the movement of planets, using newtons law of universal gravity to calculate the velocities and then adding all of them together to calculate the final movement direction. however, when trying to implement the formula, my planets just dont move and when outputting the calculated velocity float on the console, it says +infinity. heres the algorithm i use:
private void calculateVelocity() { List<Vector2> Velocities = new List<Vector2>(); foreach (Planet p in Game1.Planets) { Vector2 dir = Vector2.Subtract(p.Position, p.Position); float radius = dir.Length(); float power = G * ((Mass * p.Mass) / radius); float acceleration = power / Mass; float velocity = acceleration * deltaTime * 0.1f; //Console.WriteLine(velocity) -- Outputs random numbers and often +infinity !!! dir.Normalize(); dir = Vector2.Multiply(Position, velocity); Velocities.Add(dir); } foreach (Vector2 v in Velocities) { Vector2.Add(Velocity, v); } } i hope u can help me solve this problem. thx in advance, Daniel
EDIT
Heres the (hopefully) working version of this, in case anyone needs this.
private void calculateVelocity() { List<Vector2> Velocities = new List<Vector2>(); foreach (Planet p in Game1.Planets) { if(p.Identifier != this.Identifier) { Vector2 dir = Vector2.Subtract(p.Position, Position); float radius = dir.Length(); float force = G * ((Mass * p.Mass) / ((float)Math.Pow(radius,2)); float acceleration = force / Mass; float velocity = acceleration * deltaTime * 0.1f; //Console.WriteLine(velocity) -- Outputs random numbers and often +infinity !!! dir.Normalize(); dir = Vector2.Multiply(dir, velocity); Velocities.Add(dir); } } foreach (Vector2 v in Velocities) { Vector2.Add(Velocity, v); } }