n-body-gravity solution
Fri May 01, 2020 12:55 am
The following article is the solution to n-body-gravity:
http://www.flight-light-and-spin.com/n-body/gravity.htm
If you want source code for n-body-gravity, use this link:
http://www.flight-light-and-spin.com/n-body/n-body-build.htm
http://www.flight-light-and-spin.com/n-body/gravity.htm
If you want source code for n-body-gravity, use this link:
http://www.flight-light-and-spin.com/n-body/n-body-build.htm
Re: n-body-gravity solution
Fri May 01, 2020 12:58 am
Source code for n-body gravity in 3d:
'GRate is a time-scale variable that is a square of TimeRate!
100
GRate = TimeRate^2
'set both rates to = 1 for simplest solution.
For Ss3 = 0 to Planets 'zero is the sun's number.
TraVX = 0: TraVY = 0: TraVZ = 0 'planet velocity Traversed XYZ
For Ss2 = 0 to Planets
If Ss3 = Ss2 Then GoTo 200 'no gravity on planet itself.
Ss2 is the effective planet, Ss3 is is the effected planet.
'PoX PoY PoZ are real positions in km for XYZ axes.
'Preset the positions yourself prior to this routine.
Xx = PoX(Ss2) - PoX(Ss3)'Get the distance between the bodies.
Yy = PoY(Ss2) - PoY(Ss3)
Zz = PoZ(Ss2) - PoZ(Ss3)
'3d pythagorus, Dist = distance between bodies.
Dist = ((Xx * Xx) + (Yy * Yy) + (Zz * Zz)) ^ (0.5)
'Radbody = radius of body. Exclude close encounters here:
If Dist > RadBody Then
'3d Newton's equation * time-scale variable:
Gfor = ((Mass(Ss2)*Gee) / (Dist^2)) * GRate
'GRate = time-scale.
'Gfor = virtual gravity force.
Zp = Zz / Dist 'Zp is Z-proportion from Zz distance, ditto Xx & Yy.
Yp = Yy / Dist
Xp = Xx / Dist
GravX = Gfor * Xp 'Gravity distributed proportionally.
GravY = Gfor * Yp
GravZ = Gfor * Zp
'GraVX GraVY GraVZ are individual shifts in velocity caused by
'gravity-force for each isolated interaction in quantum time.
End If
TraVX = TraVX + GraVX
TraVY = TraVY + GraVY
TraVZ = TraVZ + GraVZ
'TraVX TraVY TraVZ are accumulated velocities traversing.
'for each planet in quantum time.
200
Next Ss2
'TotalX TotalY TotalZ are combined shifts in velocity.
'for a single step in time from all planets.
TotalX(Ss3) = TraVX
TotalY(Ss3) = TraVY
TotalZ(Ss3) = TraVZ
Next Ss3
'The first pair of for...next loops are now complete.
'They have determined what the changes to the planetary velocities will be.
'Now this is where the velocities are actually changed for each time unit.
'MoX MoY MoZ are motion variables for all time, so do not set them to zero.
'Starting motion/velocities not here included, define those yourself prior to this routine.
For Ss4 = 0 to Planets
MoX(Ss4) = MoX(Ss4) + TotalX(Ss4)
MoY(Ss4) = MoY(Ss4) + TotalY(Ss4)
MoZ(Ss4) = MoZ(Ss4) + TotalZ(Ss4)
Next Ss4
'The velocity has changed, now we move the positions of each planet.
For Ss5 = 0 to Planets 'positions moved by motion variables.
PoX(Ss5) = PoX(Ss5) + MoX(Ss5)
PoY(Ss5) = PoY(Ss5) + MoY(Ss5)
PoZ(Ss5) = PoZ(Ss5) + MoZ(Ss5)
Next Ss5
'now put the actual dots on the screen:
For Ss6 = 0 To Planets
'Zoom is distance scale,
'balX balY balZ balance position on screen. Figure these yourself.
PsX = (PoX(Ss6) / Zoom) + balX
PsY = (PoY(Ss6) / Zoom) + balY
'PsX PsY PsZ are screen positions in pixels.
balX, balY, balZ are your screen position variables balanced to center-screen.
PSet (PsX, PsY), PlanetColor 'draw pixel: top-view, main screen.
'zxaxisZ will also require your zoom and balance adjustments
zxaxisZ = (PoZ(Ss6) / Zoom) + balZ
'zxaxisY and zxaxisX are similar to PsX & PsY
ViewXZ.PSet (zxaxisX, zxaxisZ), PlanetColor 'draw side-view (XZ-axis).
ViewZY.PSet (zyaxisZ, zyaxisY), PlanetColor 'draw side-view (ZY-axis).
Next Ss6
Goto 100 'not required if using an event-timer that loops itself.
'GRate is a time-scale variable that is a square of TimeRate!
100
GRate = TimeRate^2
'set both rates to = 1 for simplest solution.
For Ss3 = 0 to Planets 'zero is the sun's number.
TraVX = 0: TraVY = 0: TraVZ = 0 'planet velocity Traversed XYZ
For Ss2 = 0 to Planets
If Ss3 = Ss2 Then GoTo 200 'no gravity on planet itself.
Ss2 is the effective planet, Ss3 is is the effected planet.
'PoX PoY PoZ are real positions in km for XYZ axes.
'Preset the positions yourself prior to this routine.
Xx = PoX(Ss2) - PoX(Ss3)'Get the distance between the bodies.
Yy = PoY(Ss2) - PoY(Ss3)
Zz = PoZ(Ss2) - PoZ(Ss3)
'3d pythagorus, Dist = distance between bodies.
Dist = ((Xx * Xx) + (Yy * Yy) + (Zz * Zz)) ^ (0.5)
'Radbody = radius of body. Exclude close encounters here:
If Dist > RadBody Then
'3d Newton's equation * time-scale variable:
Gfor = ((Mass(Ss2)*Gee) / (Dist^2)) * GRate
'GRate = time-scale.
'Gfor = virtual gravity force.
Zp = Zz / Dist 'Zp is Z-proportion from Zz distance, ditto Xx & Yy.
Yp = Yy / Dist
Xp = Xx / Dist
GravX = Gfor * Xp 'Gravity distributed proportionally.
GravY = Gfor * Yp
GravZ = Gfor * Zp
'GraVX GraVY GraVZ are individual shifts in velocity caused by
'gravity-force for each isolated interaction in quantum time.
End If
TraVX = TraVX + GraVX
TraVY = TraVY + GraVY
TraVZ = TraVZ + GraVZ
'TraVX TraVY TraVZ are accumulated velocities traversing.
'for each planet in quantum time.
200
Next Ss2
'TotalX TotalY TotalZ are combined shifts in velocity.
'for a single step in time from all planets.
TotalX(Ss3) = TraVX
TotalY(Ss3) = TraVY
TotalZ(Ss3) = TraVZ
Next Ss3
'The first pair of for...next loops are now complete.
'They have determined what the changes to the planetary velocities will be.
'Now this is where the velocities are actually changed for each time unit.
'MoX MoY MoZ are motion variables for all time, so do not set them to zero.
'Starting motion/velocities not here included, define those yourself prior to this routine.
For Ss4 = 0 to Planets
MoX(Ss4) = MoX(Ss4) + TotalX(Ss4)
MoY(Ss4) = MoY(Ss4) + TotalY(Ss4)
MoZ(Ss4) = MoZ(Ss4) + TotalZ(Ss4)
Next Ss4
'The velocity has changed, now we move the positions of each planet.
For Ss5 = 0 to Planets 'positions moved by motion variables.
PoX(Ss5) = PoX(Ss5) + MoX(Ss5)
PoY(Ss5) = PoY(Ss5) + MoY(Ss5)
PoZ(Ss5) = PoZ(Ss5) + MoZ(Ss5)
Next Ss5
'now put the actual dots on the screen:
For Ss6 = 0 To Planets
'Zoom is distance scale,
'balX balY balZ balance position on screen. Figure these yourself.
PsX = (PoX(Ss6) / Zoom) + balX
PsY = (PoY(Ss6) / Zoom) + balY
'PsX PsY PsZ are screen positions in pixels.
balX, balY, balZ are your screen position variables balanced to center-screen.
PSet (PsX, PsY), PlanetColor 'draw pixel: top-view, main screen.
'zxaxisZ will also require your zoom and balance adjustments
zxaxisZ = (PoZ(Ss6) / Zoom) + balZ
'zxaxisY and zxaxisX are similar to PsX & PsY
ViewXZ.PSet (zxaxisX, zxaxisZ), PlanetColor 'draw side-view (XZ-axis).
ViewZY.PSet (zyaxisZ, zyaxisY), PlanetColor 'draw side-view (ZY-axis).
Next Ss6
Goto 100 'not required if using an event-timer that loops itself.
Permissions in this forum:
You cannot reply to topics in this forum