Simulating N-Body Gravity Interactions: A Challenge in Computational Physics

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 3K views
DrCheeto
Messages
2
Reaction score
0

Homework Statement


Hi for fun, and to understand physics better I have scripted a program that replicate gravity exerted from one force (ex the Earth rotating around the sun) and it works well. But now I want to have mutliple bodies that affect gravity and using the same equations it isn't working. To find net gravity I am just finding the force of gravity between say Earth and sun1 and sun2 then adding them up clearly this is wrong because it does not work at all.

Homework Equations


Here is my equations

Code:
        'Force
        Ftmp = (G * Ball.Mass * Planet.Mass) / (nDist * nDist)
        Force.X = Ftmp * (Ball.Pos.X - Planet.Pos.X) / nDist
        Force.Y = Ftmp * (Ball.Pos.Y - Planet.Pos.Y) / nDist
        
        'Acceleration
        Acceleration.X = Force.X / Ball.Mass
        Acceleration.Y = Force.Y / Ball.Mass
        
        'Velocity
        Ball.Velocity.X = Ball.Velocity.X + Acceleration.X
        Ball.Velocity.Y = Ball.Velocity.Y + Acceleration.Y
        
        'New Position
        Ball.Pos.X = Ball.Pos.X - Ball.Velocity.X
        Ball.Pos.Y = Ball.Pos.Y - Ball.Velocity.Y
        
        'Return new information
        Gravity = Ball

The Attempt at a Solution


My attempt was basically this
Code:
for ncount = 1 to 3
        Ftmp = (G * Ball.Mass * Planet(ncount).Mass) / (nDist * nDist)
        Force.X = force.x + Ftmp * (Ball.Pos.X - Planet(ncount).Pos.X) / nDist
        Force.Y = force.y + Ftmp * (Ball.Pos.Y - Planet(ncount).Pos.Y) / nDist
next
 
Physics news on Phys.org
Can anyone help me?