3

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); } } 
0

3 Answers 3

5

You have a zero-length direction:

Vector2 dir = Vector2.Subtract(p.Position, p.Position); 

should probably be

Vector2 dir = Vector2.Subtract(p.Position, Position); 

or just

Vector2 dir = p.Position - Position; 
Sign up to request clarification or add additional context in comments.

4 Comments

if i apply this, velocity still outputs very strange numbers, still including +infinity.
Are the positions different from each other? Are the masses valid (i.e. != 0)? Btw, the acceleration is independent from the own mass. = G * p.Mass / radius. Does deltaTime have valid values?
yes the Positions are different. The Masses are 1000.0f each. No, not in my case, because if u solve the formula F = m * a , you will get a = F / m, which i later use to calculate the speed. deltaTime is lower than 1, but is never 0 or negative.
Then you should log radius, Mass, p.Mass and velocity to see in which cases the values become unreasonable. The formulae are correct. If a = F / m and F = G * m * m' / r then a = G * m * m' / (r * m) and therefore a = G * m' / r. The acceleration is independent of the own mass.
2

You probably need to skip the current planet. You need to test inside the foreach loop to skip this in the calculations. Otherwise, dir will be the zero vector. Then radius will be 0.

Also, you are confusing power and force. Those are very distinct concepts in physics. For everyone, please change that.

Also,

float power = G * ((Mass * p.Mass) / radius); 

Should be:

float force = G * ((Mass * p.Mass) / radius / radius); 

Since gravity falls off inversely proportional to the square of the distance.

Then you:

dir.Normalize(); dir = Vector2.Multiply(Position, velocity); // This overwrites dir... why normalize it first? 

I am guessing you meant:

dir.Normalize(); dir = Vector2.Multiply(dir, velocity); 

These should get you up and running.

3 Comments

God, its always those mistakes that i oversee... Thank you very much :)
sry for that one. the german word "Kraft" translates to either "Power" or "Force", i went with power obviously. didnt think of it as a synonym to performance/effort.
Its not a problem. English is hard.
1

You could be getting an infinity value on a float when a divide by zero occurs.. Check to see if either radius or Mass has a 0 value first...

1 Comment

sometimes, velocity has a valid value, like 1.6643543 or something, and in between theres always +infinity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.