Code archives/Miscellaneous/Realistic Model of the Solar System

This code has been declared by its author to be Public Domain code.

Download source code

Realistic Model of the Solar System by Krischan2010
This little demo simulates our solar system with realistic mass effect calculations considering the N-body-problem. Here you can play god and change some parameters:

Arrows up/down = Zoom in/out
Arrows left/right = speed increase/decrease
Space = Tracers
F1 = inner system
F2 = complete system
F3 = double sun mass
F4 = half sun mass
F5 = earth = 0.2 sun masses :-)



Please notice that due low precision here you will "lose" planets if the speed increase is too high. In 1989, Jacques Laskar of the Bureau des Longitudes in Paris published the results of his numerical integration of the Solar System over 200 million years. Laskar's work showed that the Earth's orbit (as well as the orbits of all the inner planets) is chaotic and that an error as small as 15 metres in measuring the position of the Earth today would make it impossible to predict where the Earth would be in its orbit in just over 100 million years' time. -> Stability of the solar system
; Realistic 2D-Model of the solar system 1.0
; considering the N-body problem
; with real object data from 01/01/2000

AppTitle "Realistic 2D-Model of the solar system 1.0"

; parameters
scrX	= 800		; screen width
scrY	= 600		; screen height
scrD	= 32		; color depth
scrT	= 2			; windows / fullscreen

pn		= 10		; number of planets
ps		= 3			; size of planets
zoom	= 200		; zoom factor (1=far, 250=near, 200=inner system, 5=complete)
tracers	= 1			; planet tracers on/off
sunmove = 1			; correct sun movement on/off
sunmass#= 1.0		; sun mass (1=standard)
TIM#	= 0			; reset time factor
TIMINT#	= 0.5		; increase x sun day per cycle, higher values means precision loss!
					; ex. >10 mercury gets lost

; gauss gravitional constant
K1# = .01720209895
K2# = K1# * K1#

; fields
Dim X#(pn), Y#(pn), Z#(pn), VX#(pn), VY#(pn), VZ#(pn), MASS#(pn), ro(pn), gr(pn), bl(pn)
Dim name$(pn), oldx#(pn), oldy#(pn), DEG#(pn), MU#(pn), SEMI#(pn), ECCEN#(pn)

; read planet data
Dim pla$(pn,11)
Restore planetdata
For p=0 To pn-1
	For q=0 To 11
		Read pla$(p,q)
		name$(p)=pla$(p,0)							; Name
		X#(p)=pla$(p,1)								; X-Position
		Y#(p)=pla$(p,2)								; Y-Position
		Z#(p)=pla$(p,3)								; Z-Position
		VX#(p)=pla$(p,4)							; Speed X
		VY#(p)=pla$(p,5)							; Speed Y
		VZ#(p)=pla$(p,6)							; Speed Z
		MASS#(p)=Float(pla$(p,7))*10^Int(pla$(p,8))	; Mass
		ro(p)=pla$(p,9)								; Red
		gr(p)=pla$(p,10)							; Green
		bl(p)=pla$(p,11)							; Blue
	Next
Next

; Object Data from 01/01/2000
.planetdata
;    Name              POS X         POS Y         POS Z             Vx             Vy             Vz   Mass*10^X      R     G     B
Data "Sonne"   ,   0.0000000 ,   0.0000000 ,   0.0000000 ,  0.000000000 ,  0.000000000 ,  0.000000000 , 1.991 , 30 , 255 , 255 ,   0
Data "Merkur"  ,  -0.1778023 ,  -0.3863251 ,  -0.1879025 ,  0.020335410 , -0.007559570 , -0.006147710 , 3.191 , 23 , 255 ,  64 ,   0
Data "Venus"   ,   0.1787301 ,  -0.6390267 ,  -0.2987722 ,  0.019469170 ,  0.004915870 ,  0.000978980 , 4.886 , 24 , 255 , 192 , 128
Data "Erde"    ,  -0.3305873 ,   0.8497269 ,   0.3684325 , -0.016483420 , -0.005365460 , -0.002326460 , 5.979 , 24 ,   0 ,   0 , 255
Data "Mars"    ,  -1.5848092 ,  -0.3648638 ,  -0.1244522 ,  0.003821510 , -0.011241840 , -0.005259630 , 6.418 , 23 , 255 ,   0 ,   0
Data "Jupiter" ,   4.1801700 ,  -2.5386080 ,  -1.1900210 ,  0.004106423 ,  0.006125327 ,  0.002525539 , 1.901 , 27 , 128 , 255 ,   0
Data "Saturn"  ,  -4.6197080 ,  -8.2374610 ,  -3.2033610 ,  0.004647751 , -0.002328957 , -0.001161564 , 5.684 , 26 , 255 , 255 , 128
Data "Uranus"  ,  -3.7245900 , -17.1975200 ,  -7.4791700 ,  0.003833665 , -0.000845721 , -0.000424809 , 8.682 , 25 ,   0 , 255 , 255
Data "Neptun"  ,   1.9138100 , -27.9215500 , -11.4762000 ,  0.003118271 ,  0.000233303 ,  0.000017967 , 1.027 , 26 ,   0 , 128 , 255
Data "Pluto"   , -23.2285900 , -18.5272000 ,   1.2167500 ,  0.002066577 , -0.002488884 , -0.001397200 , 1.080 , 24 , 128 , 128 , 128

; Normalize Mass to sun
For I = 1 To pn-1
	MASS#(I) = MASS#(I) / MASS(0)
Next
MASS#(0) = sunmass#

; create reduced mass in gauss units
For I = 1 To pn-1
	MU#(I) = K2# * (1 + MASS#(I))
Next

; init screen
Graphics scrX,scrY,scrD,scrT
SetBuffer BackBuffer()

; main loop
While Not KeyHit(1)
	
	; increase time by factor
	TIM# = TIM# + TIMINT#
	
	; draw tracers
	For i=0 To pn-1
		If tracers Then Color 32,32,32 Else Color 0,0,0
		Oval(scrX/2+oldx#(i),scrY/2+oldy#(i),ps,ps,1)
	Next
	
	; calculations and output
	Gosub NewV
	Gosub NewP
	
	Rect 0,0,150,42,1
	Color 255,255,255
	
	Text 0, 0,"Days:  "+Int(TIM#)
	Text 0,10,"Years: "+TIM#/365.25
	Text 0,20,"Time:  "+TIMINT#
	Text 0,30,"Zoom:  "+zoom
	
	; Keyboard shortcuts
	; ------------------
	; Arrows up/down		Zoom in/out
	; Arrows left/right		speed increase/decrease
	; Space					Tracers
	; F1					inner system
	; F2					complete system
	; F3					double sun mass
	; F4					half sun mass
	; F5					earth = 0.2 sun masses :-)
	
	kp=0
	If KeyDown(200) Then zoom=zoom+1:If zoom>250 Then zoom=250
	If KeyDown(208) Then zoom=zoom-1:If zoom<1 Then zoom=1
	If KeyDown(203) Then TIMINT#=TIMINT#-.5:If TIMINT#<.5 Then TIMINT#=.5
	If KeyDown(205) Then TIMINT#=TIMINT#+.5:If TIMINT#>25 Then TIMINT#=25
	If KeyHit(57) Then tracers=1-tracers:Cls
	If KeyDown(59) Then zoom=200:Cls
	If KeyDown(60) Then zoom=5:Cls
	If KeyDown(61) Then MASS#(0)=2
	If KeyDown(62) Then MASS#(0)=0.5
	If KeyDown(63) Then MASS#(3)=.2
	
	Flip 1
	
Wend

End


;  calc new speed
.NewV
For I = 0 To pn-1
	
	AX# = 0
	AY# = 0
	AZ# = 0
	
	For J = 0 To pn-1
		If (J = I) Then Goto skip
		XJI# = X#(J) - X#(I)
		YJI# = Y#(J) - Y#(I)
		ZJI# = Z#(J) - Z#(I)
		R# = Sqr(ZJI# * ZJI# + YJI# * YJI# + XJI# * XJI#)
		R3# = R# * R# * R#
		COEFF# = K2# * MASS#(J) / R3#
		AX# = COEFF# * XJI# + AX#
		AY# = COEFF# * YJI# + AY#
		AZ# = COEFF# * ZJI# + AZ#
		.skip
	Next
	
	VX#(I) = VX#(I) + AX# * TIMINT#
	VY#(I) = VY#(I) + AY# * TIMINT#
	VZ#(I) = VZ#(I) + AZ# * TIMINT#
	
Next

Return

; calc new positions and draw planets
.NewP
For i=0 To pn-1
	
	; calc new position
	X#(i) = X(i) + VX#(i) * TIMINT#
	Y#(i) = Y(i) + VY#(i) * TIMINT#
	Z#(i) = Z(i) + VZ#(i) * TIMINT#
	
	; include zoom factor
	sx#=X#(i)*zoom
	sy#=Y#(i)*zoom
	
	; correct sun movement
	If i=0 And sunmove=1 Then
		sunx#=sx#
		suny#=sy#
	EndIf
	
	; new position with zoom and sun movement
	sx#=sx#-sunx#
	sy#=sy#-suny#
	
	; save old position for tracers
	oldx#(i)=sx#
	oldy#(i)=sy#
	
	; draw planets
	Color ro(i),gr(i),bl(i)
	Oval(scrX/2+sx#,scrY/2+sy#,ps,ps,1)
Next

Return

Comments

slenkar2010
you could use this for astrology, to know where the planets are at a persons time of birth


epiblitikos2010
SWEET! I'm gonna play with this when I'm bored.


Filax2012
another gravity code :) blitzmax




Rick Nasher2014
Hi,
Does anybody, or perhaps Krischan himself if snooping around here(btw: great fan of all your code), know how the distances can be scaled up to reflect 'realistic' sizes of the planets?

I'm redoing my own little planetary, but the orbits and calculations are apparently sort of realistic in this one, so looking to see how to encorporate the math into my own stuff. Unfortunately I have to admit the math is beyond me, and don't see how to adjust the code for 3d 'real' distances without messing up the accuracy.

Below a 3D-ified version of the code, in which I had to mess with the sizes as the size of the sun alone would be so big it would cover the entire inner planet system.







Code + Gfx here:
http://www.mediafire.com/download/k7xwdq232kdxwvs/Simplenatary.rar


Rick Nasher2014
Anybody, no math/astronomer wizards around?


Krischan2014
Realistic? You could use Blitzmax using Doubles. But even these large numbers are too small to represent the distances correct. So you need a visual trick. Take a look at my old project, my last post there. There is a codebox with a ready-2-run demo. Just fly away from the planet and you'll see how the planets shrink and you're getting the feeling of being very, very far away from them (the fake Range is from 1km to 10 Billion kilometers)

There is a simple line of code for the sun scale adjustment, it could be better but it works:
; adjust sunscale
sunscale=((1-(1.0/Exp(EntityDistance(Player,Sun0)*0.0000001)))*500000)
ScaleEntity Sun0,sunscale,sunscale,sunscale

And you should take a look at the "Homekeeping" function used there, too to avoid the jitter effect in large distances.

I've experimented with other formulas like above but I don't know exactly where I stored it on my NAS archive right now. But 1.0/Exp(x) has been always an important part of it :-) Or 1.0/Entitydistance(x,y) if you want linear scaling.


Rick Nasher2014
Hey Krischan, thanks very much for your response.

My goal is to do this in Blitz3d, as way back I did a 3D planetary demo myself of the 9 planets + moons, but I want to do it bit more realistic in the sense of calculations of orbits, distances, speeds and planet sizes. (well, as far as can be of course, scaling is ok, but not too far off, ideally a full realistic mode and a game/demo mode)

Then I saw your calculation stuff and quickly realized it was far more superior. I'm not a great mathematician but thought was to use your calculation code and make this into a 3d version.

Problem I have is, besides that obviously distances need to be scaled, is that I don't know how to size up the movement of your calculations and adjust the speed too so it be sort of every day speed (or scaled to gametime speed where 1 day is being run in 2 hours for instance). I do have some planetary data including sizes and orbit speeds and could just implement that, but I know it wouldn't be as good as the math you're using.


I admire both your pieces of code. Would be great to implement the planetary orbit stuff into your other project(the Spacegame tech demo), cos only thing that one is lacking I feel is movement/orbits of the planets. I added some rough media to see what it would look like and I must say it's rather pretty! :-)




Would really by great if your 2 pieces of code could be combined, however dunno if possible at all.


Krischan2014
Hmm, perhaps my Interplanetary Travel Code could give you some help about the movements and scaling - and it's in Blitz3D. And you should study my code archive postings here and perhaps all my other space related postings, too. I'm sure you'll find answers for problems you'll encounter later.

By the way, here is a small outlook of my new Planet Creation Code but it's all in Blitzmax now and currently I'm working on my RPG project again. See yourself (click for fullscreen), it is always the same code generating completely different planets.




Rick Nasher2014
Wow, the detail is absolutely stunning. Can't wait to see your RPG. Always thought was a shame you didn't put all this to use in a game(or did you already and I'm not aware? ). That Interplanetary Travel Code indeed should proof to be very useful. Many thanks. You are a true GFX/Math wizz.


Krischan2014
Umm it's not a space RPG game, just a planet creation algorithm. Perhaps I'll post a demo of it if I find time to clean up the code, its very messy due many changes. No, I mean the oldschool RPG game I'm working right now, see Graphics Showcase category from today:

http://www.blitzbasic.com/Community/posts.php?topic=103075

I'm working parallel on two games. When I'm tired of one, I'm working on the other one. It is challenging but I hope I'll finish them both in a few years ;-)


Rick Nasher2014
Ah, same here hehehe. But I should focus more on the one.


Krischan2014
see last post here:

http://www.blitzbasic.com/Community/posts.php?topic=97336


Rick Nasher2014
Absolutely stunning stuff. You just HAVE to put all that genius into a game. You owe it to the world. I'm thinking Daft Punk meats Star Trek / merchants and mercenaries..


Rick Nasher2014
Eventhough Interplanetary travel is a fully moving solar system(impressive sun avoiding code), I've noticed that it suffers from the same issue I've had with my own version: the movements are at times jurky, cos the whole thing operates somewhat like a clockwork.

This causes planets to jump to their new positions when being updated. This is especially noticeable at lower time speeds. I've never found a good solution for this.


Krischan2014
Then you should give a try to Blitzmax+Doubles for better precision. You can translate the B3D project to Blitzmax+MiniB3D in most cases without any serious problems. And here is a whole external thread about the precision problem:

http://www.gamedev.net/topic/466331-floating-point-precision-problem-for-3d-universe/

"inside a solar system, coordinates are represented as 3x doubles, giving an accuracy of 1 millimeter at 100 AU."

Perhaps 1x double is already sufficient for your problem (according to the BlitzMax docs the 64bit floating point Double ranges are (+/-)10^-308)

Further reading:
http://en.wikipedia.org/wiki/Double-precision_floating-point_format

http://www.gamasutra.com/view/feature/131393/a_realtime_procedural_universe_.php?print=1


Code Archives Forum