Author Topic: First attempt for a solar system 2D  (Read 5837 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
First attempt for a solar system 2D
« on: May 30, 2019, 06:51:46 pm »
Hi guys

here I first attempt to port in QB64 what had been coded in Java with OOP  (classes and instances of them)

At a moment of the instructions video I loose my mind in my imagination activity so this came out as result:

Code: QB64: [Select]
  1. ' Solar System 2D: code developed following this tutorial
  2. '[youtube]https://www.youtube.com/watch?v=l8SiJ-RmeHU&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH[/youtube]&index=7
  3. TYPE Planet
  4.     Radius AS SINGLE
  5.     Angle AS SINGLE
  6.     Distance AS SINGLE
  7.     Names AS STRING * 3
  8.     Parent AS INTEGER
  9.  
  10. CONST WScreen = 900, HScreen = 600, Black = _RGBA32(0, 0, 0, 255), True = -1, False = 0
  11. CONST r = 50, CSun = _RGBA32(222, 194, 61, 255), MaxPlanet = 5, MaxMoon = 5
  12. CONST CPlanet = _RGBA32(238, 111, 89, 255): CONST CMoon = _RGBA32(100, 227, 11, 255)
  13. CONST CBlack = _RGBA32(0, 0, 10, 255)
  14.  
  15. DIM SHARED A AS LONG, Sun AS Planet, Planets(1 TO MaxPlanet * MaxMoon) AS Planet
  16. DIM SHARED ShowName AS INTEGER
  17.  
  18.  
  19. Setup
  20.     Draws
  21.     MovePlanets
  22.     IF _KEYDOWN(13) THEN ShowName = NOT ShowName
  23.     _LIMIT 10
  24.  
  25. SUB MovePlanets
  26.     DIM w AS INTEGER
  27.     FOR w = 1 TO MaxPlanet + MaxMoon
  28.         IF Planets(w).Radius > 0 THEN Planets(w).Angle = Planets(w).Angle + .1
  29.     NEXT w
  30.  
  31. SUB Setup
  32.     DIM w AS INTEGER, radius AS INTEGER, distance AS INTEGER
  33.     A = _NEWIMAGE(WScreen, HScreen, 32)
  34.     IF A < -1 THEN SCREEN A ELSE PRINT "Error handle image": EXIT SUB
  35.     _TITLE "Solar System 2D"
  36.     _SCREENMOVE 10, 10
  37.     Sun.Radius = 0
  38.     Sun.Angle = 0
  39.     Sun.Distance = 0
  40.     Sun.Names = "SUN"
  41.     Sun.Parent = 0
  42.     FOR w = 1 TO MaxPlanet STEP 1
  43.         radius = INT(RND * 20) + 10
  44.         distance = INT(RND * 200) + 100
  45.         '          Index, radius, distance from parent, parent
  46.         MakePlanet w, radius, distance, 0
  47.         RandomMoon w
  48.     NEXT w
  49.     ShowName = False
  50.  
  51. SUB RandomMoon (Index AS INTEGER)
  52.     DIM z AS INTEGER
  53.     IF INT(RND * 3) + 1 > 1 THEN
  54.         FOR z = 1 TO INT(RND * (MaxMoon - 1)) + 1
  55.             ' index moon, 1/2 radius, Parent
  56.             MakePlanet z + MaxPlanet, INT(Planets(Index).Radius / 2), INT(RND * 20) + 10, Index
  57.         NEXT
  58.     END IF
  59.  
  60. SUB DrawSun
  61.     CIRCLE (WScreen / 2, HScreen / 2), 100, CBlack
  62.     PAINT STEP(0, 0), CSun, CBlack
  63.     IF ShowName = True THEN COLOR _RGBA(70, 200, 200, 255): _PRINTSTRING (WScreen / 2, HScreen / 2), Sun.Names
  64.  
  65. SUB Draws
  66.     DIM w AS INTEGER
  67.     CLS , Black
  68.     DrawSun
  69.     FOR w = 1 TO MaxPlanet + MaxMoon
  70.         IF Planets(w).Radius > 0 THEN ShowPlanet w
  71.     NEXT w
  72.     _DISPLAY
  73.  
  74. SUB MakePlanet (Index AS INTEGER, r AS SINGLE, d AS SINGLE, p AS INTEGER)
  75.     Planets(Index).Radius = r
  76.     Planets(Index).Angle = INT(RND * 360)
  77.     Planets(Index).Distance = d
  78.     Planets(Index).Names = STR$(Index)
  79.     Planets(Index).Parent = p
  80.  
  81. SUB ShowPlanet (Index AS INTEGER)
  82.     DIM Sinus AS SINGLE, Cosinus AS SINGLE, Colr AS _UNSIGNED LONG
  83.     DIM Xcircle AS SINGLE, Ycircle AS SINGLE, Rcircle AS INTEGER
  84.     DIM Cosinus2 AS SINGLE, Sinus2 AS SINGLE
  85.     Sinus = SIN(Planets(Index).Angle)
  86.     Cosinus = COS(Planets(Index).Angle)
  87.     ' parent is an important Flag both to choose X,y of circle
  88.     ' both to understand if it is a planet around the sun or a moon around a planet
  89.  
  90.     IF Planets(Index).Parent = 0 THEN
  91.         Colr = CPlanet
  92.         Xcircle = (WScreen / 2) + Sun.Radius
  93.         Ycircle = (HScreen / 2) + Sun.Radius
  94.     ELSEIF Planets(Index).Parent > 0 THEN
  95.         Colr = CMoon
  96.         Sinus2 = SIN(Planets(Planets(Index).Parent).Angle)
  97.         Cosinus2 = COS(Planets(Planets(Index).Parent).Angle)
  98.  
  99.         Xcircle = ((Planets(Planets(Index).Parent).Distance * Cosinus2) + (WScreen / 2) + Sun.Radius)
  100.         Ycircle = ((Planets(Planets(Index).Parent).Distance * Sinus2) + (HScreen / 2) + Sun.Radius)
  101.     END IF
  102.     Rcircle = Planets(Index).Radius
  103.     Xcircle = Xcircle + (Planets(Index).Distance * Cosinus)
  104.     Ycircle = Ycircle + (Planets(Index).Distance * Sinus)
  105.     CIRCLE (Xcircle, Ycircle), Rcircle, CBlack
  106.     PAINT STEP(0, 0), Colr, CBlack
  107.  
  108.     IF ShowName = True THEN COLOR _RGBA(70, 200, 200, 255): _PRINTSTRING (Xcircle, Ycircle), Planets(Index).Names

Help:
press Spacebar to stop and quit
Enter to toggle Name On/OFF

Quote
In coming....
one color for one planet
one speed for one planet
a better graphic output using FillCircle taken here  https://www.qb64.org/forum/index.php?topic=1069.0
Programming isn't difficult, only it's  consuming time and coffee

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: First attempt for a solar system 2D
« Reply #1 on: May 31, 2019, 03:07:53 am »
Good one!
BTW, I see some on the planets overlapping each other and sometime a planet being revolving inside the sun!
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: First attempt for a solar system 2D
« Reply #2 on: May 31, 2019, 04:58:35 am »
Tempo, I assume that you will adjust each planet speed so that the orbital period is longer the further out from the Sun you are.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: First attempt for a solar system 2D
« Reply #3 on: May 31, 2019, 12:18:26 pm »
Thanks
@ Ashish
yes i'll set a greater min distance from Sun for planets in Setup


@Qwerkey
yes it is one of the further steps to give to each planet an own speed related to distance to sun

Programming isn't difficult, only it's  consuming time and coffee

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: First attempt for a solar system 2D
« Reply #4 on: May 31, 2019, 03:57:46 pm »
Very cool demonstration. You have started a new revolution.
QB64 is the best!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: First attempt for a solar system 2D
« Reply #5 on: June 01, 2019, 09:34:15 am »
Here a more little step:

Code: QB64: [Select]
  1. ' Solar System 2D: code developed following this tutorial
  2. '[youtube]https://www.youtube.com/watch?v=l8SiJ-RmeHU&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH[/youtube]&index=7
  3. TYPE Planet
  4.     Radius AS SINGLE
  5.     Angle AS SINGLE
  6.     Distance AS SINGLE
  7.     Names AS STRING * 3
  8.     Parent AS INTEGER
  9.     Speed AS SINGLE
  10.     Colors AS _UNSIGNED LONG
  11.  
  12. CONST WScreen = 900, HScreen = 600, True = -1, False = 0
  13. CONST r = 50, MaxPlanet = 5, MaxMoon = 5
  14.  
  15. CONST Black = _RGBA32(0, 0, 0, 255)
  16. CONST NameColor = _RGBA32(70, 200, 200, 255)
  17.  
  18. DIM SHARED A AS LONG, Sun AS Planet, Planets(1 TO MaxPlanet * MaxMoon) AS Planet
  19. DIM SHARED ShowName AS INTEGER
  20.  
  21. Setup
  22.     k = _KEYHIT
  23.     IF k = 8 THEN Setup
  24.     Draws
  25.     MovePlanets
  26.     IF k = 13 THEN ShowName = NOT ShowName
  27.     _DISPLAY
  28.     _LIMIT 15
  29. LOOP UNTIL k = 32
  30.  
  31. SUB MovePlanets
  32.     DIM w AS INTEGER
  33.     FOR w = 1 TO MaxPlanet + MaxMoon
  34.         IF Planets(w).Radius > 0 THEN Planets(w).Angle = Planets(w).Angle + Planets(w).Speed
  35.     NEXT w
  36.  
  37. SUB Setup
  38.     DIM w AS INTEGER, radius AS INTEGER, distance AS INTEGER
  39.     A = _NEWIMAGE(WScreen, HScreen, 32)
  40.     IF A < -1 THEN SCREEN A ELSE PRINT "Error handle image": EXIT SUB
  41.     _TITLE "Solar System 2D"
  42.     _SCREENMOVE 10, 10
  43.     Sun.Radius = 0
  44.     Sun.Angle = 0
  45.     Sun.Distance = 0
  46.     Sun.Names = "SUN"
  47.     Sun.Parent = 0
  48.     Sun.Colors = _RGBA32(222, 194, 61, 255)
  49.     FOR w = 1 TO MaxPlanet STEP 1
  50.         radius = INT(RND * 15) + 10
  51.         distance = INT(RND * 3 * r) + (3 * r)
  52.         '          Index, radius, distance from parent, parent
  53.         MakePlanet w, radius, distance, 0
  54.         RandomMoon w
  55.     NEXT w
  56.     ShowName = False
  57.  
  58. SUB RandomMoon (Index AS INTEGER)
  59.     DIM z AS INTEGER
  60.     IF INT(RND * 3) + 1 > 1 THEN
  61.         FOR z = 1 TO INT(RND * (MaxMoon - 1)) + 1
  62.             ' index moon, 1/2 radius, Parent
  63.             MakePlanet z + MaxPlanet, INT(Planets(Index).Radius / 2), INT(RND * 20) + 10, Index
  64.         NEXT
  65.     END IF
  66.  
  67. SUB DrawSun
  68.     CircleFill WScreen / 2, HScreen / 2, 100, Sun.Colors
  69.     IF ShowName = True THEN COLOR NameColor: _PRINTSTRING (WScreen / 2, HScreen / 2), Sun.Names
  70.  
  71. SUB Draws
  72.     DIM w AS INTEGER
  73.     CLS , Black
  74.     DrawSun
  75.     FOR w = 1 TO MaxPlanet + MaxMoon
  76.         IF Planets(w).Radius > 0 THEN ShowPlanet w
  77.     NEXT w
  78.  
  79. SUB MakePlanet (Index AS INTEGER, r AS SINGLE, d AS SINGLE, p AS INTEGER)
  80.     Planets(Index).Radius = r
  81.     Planets(Index).Angle = INT(RND * 360)
  82.     Planets(Index).Distance = d
  83.     Planets(Index).Names = STR$(Index)
  84.     Planets(Index).Parent = p
  85.     Planets(Index).Speed = 0.01 + (RND * 0.03)
  86.     IF p = 0 THEN Planets(Index).Colors = _RGBA(0, 100 + (RND * 150), 100 + (RND * 150), 255) ELSE Planets(Index).Colors = _RGBA(100 + (RND * 150), 100 + (RND * 150), 100 + (RND * 150), 255)
  87.  
  88. SUB ShowPlanet (Index AS INTEGER)
  89.     DIM Sinus AS SINGLE, Cosinus AS SINGLE
  90.     DIM Xcircle AS SINGLE, Ycircle AS SINGLE, Rcircle AS INTEGER
  91.     DIM Cosinus2 AS SINGLE, Sinus2 AS SINGLE
  92.     Sinus = SIN(Planets(Index).Angle)
  93.     Cosinus = COS(Planets(Index).Angle)
  94.     ' parent is an important Flag both to choose X,y of circle
  95.     ' both to understand if it is a planet around the sun or a moon around a planet
  96.  
  97.     IF Planets(Index).Parent = 0 THEN
  98.         Xcircle = (WScreen / 2) + Sun.Radius
  99.         Ycircle = (HScreen / 2) + Sun.Radius
  100.     ELSEIF Planets(Index).Parent > 0 THEN
  101.         Sinus2 = SIN(Planets(Planets(Index).Parent).Angle)
  102.         Cosinus2 = COS(Planets(Planets(Index).Parent).Angle)
  103.         Xcircle = ((Planets(Planets(Index).Parent).Distance * Cosinus2) + (WScreen / 2) + Sun.Radius)
  104.         Ycircle = ((Planets(Planets(Index).Parent).Distance * Sinus2) + (HScreen / 2) + Sun.Radius)
  105.     END IF
  106.     Rcircle = Planets(Index).Radius
  107.     Xcircle = Xcircle + (Planets(Index).Distance * Cosinus)
  108.     Ycircle = Ycircle + (Planets(Index).Distance * Sinus)
  109.     CircleFill Xcircle, Ycircle, Rcircle, Planets(Index).Colors
  110.     IF ShowName = True THEN COLOR NameColor: _PRINTSTRING (Xcircle, Ycircle), Planets(Index).Names
  111.  
  112.  
  113. SUB CircleFill (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  114.     ' CX = center x coordinate
  115.     ' CY = center y coordinate
  116.     '  R = radius
  117.     '  C = fill color
  118.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  119.     DIM X AS INTEGER, Y AS INTEGER
  120.     Radius = ABS(R)
  121.     RadiusError = -Radius
  122.     X = Radius
  123.     Y = 0
  124.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  125.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  126.     WHILE X > Y
  127.         RadiusError = RadiusError + Y * 2 + 1
  128.         IF RadiusError >= 0 THEN
  129.             IF X <> Y + 1 THEN
  130.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  131.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  132.             END IF
  133.             X = X - 1
  134.             RadiusError = RadiusError - X * 2
  135.         END IF
  136.         Y = Y + 1
  137.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  138.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  139.     WEND

Quote
Help:
     Key             action
 SpaceBar         Quit
  Enter              Toggle ON/OFF name
BackSpace        Generate New Solar System

News:
each planet or moon has its own color, name, speed, distance from sun
you can change the solar system ad libitum

In coming...
no overlapping planets with their moons and/or other planets
increasing speed of revolution from external to inner planets
Programming isn't difficult, only it's  consuming time and coffee

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: First attempt for a solar system 2D
« Reply #6 on: June 02, 2019, 11:11:36 am »
Are you intending to make an actual 2D model of the Solar System or a 2D simulation? Just curious.
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: First attempt for a solar system 2D
« Reply #7 on: June 02, 2019, 11:38:01 am »
Hi TempodiBasic,

I try a recursive MakeBodies Sub so that moons can have moons too.
Code: QB64: [Select]
  1. _TITLE "3 Planets with 3 Moons with 3 Moons System"
  2. 'B+ started 2019-06-01  with recursive MakeBodies sub
  3. CONST xmaxScreen = 1000, ymaxscreen = 740
  4.  
  5. SCREEN _NEWIMAGE(xmaxScreen, ymaxscreen, 32)
  6.  
  7. TYPE bodyType
  8.     ThisI AS INTEGER
  9.     ParentI AS INTEGER '     gives the x,y origin of center of orbit
  10.     OrbitI AS INTEGER
  11.     Distance AS SINGLE '       R from center origin
  12.     Angle AS SINGLE '          current location around center
  13.     AngleChange AS SINGLE 'change of angle each frame
  14.     X AS SINGLE 'x, y location from above info needed for child planets
  15.     Y AS SINGLE
  16.     R AS SINGLE
  17.     C AS _UNSIGNED LONG
  18.  
  19. REDIM SHARED B(0) AS bodyType
  20.  
  21. makeBodies 0, 0, 0
  22.     CLS
  23.     LOCATE 1, 1: PRINT "Body count:"; BC
  24.     drawBodies
  25.     _DISPLAY
  26.     _LIMIT 10
  27.  
  28. SUB makeBodies (generation AS INTEGER, OrbitI AS INTEGER, ParentI AS INTEGER)
  29.     DIM oi AS INTEGER, pI AS INTEGER, r AS INTEGER, g AS INTEGER, b AS INTEGER
  30.  
  31.     IF generation > 3 THEN
  32.         EXIT SUB
  33.     ELSE
  34.         BC = BC + 1
  35.         REDIM _PRESERVE B(BC) AS bodyType
  36.         pI = BC '< save non shared value
  37.         IF BC = 1 THEN 'the sun
  38.             B(BC).ThisI = BC
  39.             B(BC).ParentI = 0
  40.             B(BC).Distance = 350
  41.             B(BC).AngleChange = _PI(1 / 1440)
  42.             B(BC).X = xmaxScreen / 2
  43.             B(BC).Y = ymaxscreen / 2
  44.             B(BC).R = 20
  45.             B(BC).C = &HFFFFFFAA
  46.         ELSE 'the body has a parent from which we decide some numbers for location
  47.             B(BC).ThisI = BC
  48.             B(BC).ParentI = ParentI
  49.             B(BC).OrbitI = OrbitI
  50.             B(BC).Distance = B(ParentI).Distance / 10
  51.             B(BC).Angle = RND * _PI(2)
  52.             B(BC).AngleChange = 10 / OrbitI ^ 2 * B(ParentI).AngleChange
  53.             B(BC).X = B(ParentI).X + (3 * B(BC).OrbitI + 2) * B(BC).Distance * COS(B(BC).Angle)
  54.             B(BC).Y = B(ParentI).Y + (3 * B(BC).OrbitI + 2) * B(BC).Distance * SIN(B(BC).Angle)
  55.             B(BC).R = B(ParentI).R / 8
  56.             IF B(BC).R < 1 THEN B(BC).R = 1
  57.             IF generation = 1 THEN
  58.                 B(BC).C = _RGB32(RND * 55 + 200, RND * 155 + 100, RND * 155 + 100)
  59.             ELSE
  60.                 r = _RED32(B(B(BC).ParentI).C)
  61.                 g = _GREEN32(B(B(BC).ParentI).C)
  62.                 b = _BLUE32(B(B(BC).ParentI).C)
  63.                 B(BC).C = _RGB32(r - OrbitI * 15, g - OrbitI * 15, b - OrbitI * 15)
  64.             END IF
  65.         END IF
  66.         FOR oi = 1 TO 3
  67.             makeBodies generation + 1, oi, pI
  68.         NEXT
  69.     END IF
  70.  
  71. SUB drawBodies
  72.     DIM i AS INTEGER
  73.     FOR i = 1 TO BC
  74.         IF i > 1 THEN
  75.             B(i).Angle = B(i).Angle + B(i).AngleChange
  76.             B(i).X = B(B(i).ParentI).X + (3 * B(i).OrbitI + 2) * B(i).Distance * COS(B(i).Angle)
  77.             B(i).Y = B(B(i).ParentI).Y + (3 * B(i).OrbitI + 2) * B(i).Distance * SIN(B(i).Angle)
  78.         END IF
  79.         fcirc B(i).X, B(i).Y, B(i).R, B(i).C
  80.     NEXT
  81.  
  82. SUB fcirc (CX AS INTEGER, CY AS INTEGER, RR AS INTEGER, C AS _UNSIGNED LONG)
  83.     DIM R AS INTEGER, RError AS INTEGER
  84.     DIM X AS INTEGER, Y AS INTEGER
  85.  
  86.     R = ABS(RR)
  87.     RError = -R
  88.     X = R
  89.     Y = 0
  90.     IF R = 0 THEN PSET (CX, CY), C: EXIT SUB
  91.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  92.     WHILE X > Y
  93.         RError = RError + Y * 2 + 1
  94.         IF RError >= 0 THEN
  95.             IF X <> Y + 1 THEN
  96.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  97.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  98.             END IF
  99.             X = X - 1
  100.             RError = RError - X * 2
  101.         END IF
  102.         Y = Y + 1
  103.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  104.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  105.     WEND
  106.  
  107.  

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: First attempt for a solar system 2D
« Reply #8 on: June 02, 2019, 11:44:03 am »
Well, despite the added orbiting lunar bodies, I like Temp's model better. My eyesight isn't what it used to be and, no offense Mark, your balls are too small. Temp has bigger balls, and I find that an attractive quality in a solar system.

Actually, I'm going to ask Odin to lock this thread. I'm pretty sure this is how The Matrix got started! :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: First attempt for a solar system 2D
« Reply #9 on: June 02, 2019, 07:51:57 pm »
Hi

@Johno56
it is an attempt to make a model of a solar system, not just the our solar system.
As you can see the physic is out of model for now, I must see the whole original video to see if physic comes out.

@Bplus
interesting model with moons having moons, I cannot imagine how is complicated if the second generation of moons have moon too.
:)

@Pete
Matrix? Do you talk about Neo (Mr Anderson)?
Programming isn't difficult, only it's  consuming time and coffee

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: First attempt for a solar system 2D
« Reply #10 on: June 02, 2019, 11:07:07 pm »
Yes, Neo. God forbid I ever have to live in a manufactured computer simulati.... God forbid I ever hav...

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: First attempt for a solar system 2D
« Reply #11 on: June 03, 2019, 10:29:56 am »
Code: QB64: [Select]
  1. ' Solar System
  2.  
  3. DEFDBL A-Z
  4. t = 10
  5. DIM sx(t), sy(t), ox(t), oy(t), p$(t), pm(t), pd(t), pr(t), pc(t), pv(t), pt(t), interval$(t), interval(t)
  6. GOSUB init
  7.     n& = n& + interval(ts)
  8.     PSET (0, 0), 15
  9.     FOR sat = 1 TO np
  10.         dd = pd(sat)
  11.         v = SQR((gc * pm(0)) / dd)
  12.         pv(sat) = v
  13.         t = SQR(4 * pi * pi * (dd ^ 3) / (gc * pm(0)))
  14.         pt(sat) = t
  15.         de = (360 / t * n&) MOD 360
  16.         r = (de - 90) * dr
  17.         sx(sat) = (pd(0) + pd(sat)) * COS(r)
  18.         sy(sat) = (pd(0) - pd(sat)) * SIN(r)
  19.         z = pr(sat) * q * 2
  20.         LINE (ox(sat) - z, oy(sat) - z)-(ox(sat) + z, oy(sat) + z), 0, BF ' erase old
  21.         CIRCLE (sx(sat), sy(sat)), pr(sat) * q, pc(sat) '                   plot new
  22.         'PAINT (sx(sat), sy(sat)), pc(sat) '                                messes up sometimes, so
  23.         ox(sat) = sx(sat)
  24.         oy(sat) = sy(sat)
  25.     NEXT sat
  26.     IF tflag = 0 THEN tflag = 1: GOSUB text
  27.     i$ = INKEY$
  28.     IF LEN(i$) = 1 THEN
  29.         ip = INSTR("3456789", i$)
  30.         IF ip THEN
  31.             np = ip + 2
  32.             GOSUB init2
  33.         END IF
  34.     END IF
  35.     IF LEN(i$) = 2 THEN
  36.         a = ASC(RIGHT$(i$, 1))
  37.         ts = ts + (a = 72) - (a = 80)
  38.         IF ts < 1 THEN ts = 1
  39.         IF ts > 7 THEN ts = 7
  40.         GOSUB showint
  41.     END IF
  42. LOOP UNTIL i$ = CHR$(27)
  43. ' ------------------------------------------------------------------------
  44. showint:
  45. LOCATE 12, 1: PRINT "  INTERVAL"
  46. FOR i = 1 TO 7
  47.     IF i = ts THEN COLOR 4 ELSE COLOR 8
  48.     LOCATE 13 + i, 3
  49.     PRINT interval$(i)
  50. ' ------------------------------------------------------------------------
  51. init:
  52. sos = 1
  53. pi = ATN(1) * 4
  54. dr = ATN(1) / 45 '                angle to radian conversion
  55. xy = 640 / 480 '                  aspect ratio
  56. gc = 6.67E-11 '                   gravitational constant
  57. mk = 1 / .621371 '                miles to km
  58.  
  59. em = 5.98E+24: er = 6370000! '    Earth mass, radius (m)
  60. mm = 7.4E+22: mr = 1740000! '     Moon mass, radius (m)
  61. sm = 2E+30: sr = 7E+08 '          Sun mass, radius
  62. sd = 1.5E+11 '                    Sun to Earth
  63. min = 356410.3 * 1000 '           Earth to Moon in meters
  64. max = 406697.5 * 1000
  65.  
  66. RESTORE planets
  67. FOR p = 0 TO 9 '                  Sun + 9 planets
  68.     READ g, p$, m, d, r
  69.     p$(p) = p$ '                  name
  70.     pm(p) = m * em '              mass
  71.     pd(p) = d * mk * 1000000000 ' distance
  72.     pr(p) = r * mk * 1000 '       radius
  73.  
  74. FOR p = 1 TO 9
  75.     '   color          0  1  2  3  4  5  6  7  8  9
  76.     pc(p) = VAL(MID$("14 07 15 10 04 11 14 06 01 08", p * 3 + 1, 2))
  77.     '                  S  M  V  E  M  J  S  U  N  P
  78.  
  79. ts = 4: z = 1
  80. RESTORE intervals
  81. FOR i = 1 TO 7
  82.     READ q, interval$(i)
  83.     z = z * q
  84.     interval(i) = z
  85. q = 1000 '                        magnification of planets
  86. np = 8 '                          planets to show
  87. GOSUB init2
  88. ' ------------------------------------------------------------------------
  89. init2:
  90. t = pd(np) * 2
  91. WINDOW (-t, -t / xy)-(t, t / xy)
  92. PSET (0, 0), 15
  93. GOSUB showint
  94. tflag = 0
  95.  
  96. t$ = "THE SOLAR SYSTEM"
  97. LOCATE 2, 40 - LEN(t$) \ 2: PRINT t$;
  98. t$ = "Planets magnified 1000 times"
  99. LOCATE 28, 40 - LEN(t$) \ 2: PRINT t$;
  100. t$ = "Press 3-9 to select planets, up/down to change interval"
  101. LOCATE 29, 40 - LEN(t$) \ 2: PRINT t$;
  102. ' ------------------------------------------------------------------------
  103. text:
  104. LOCATE 1, 1: PRINT "  km/s     Y";
  105. FOR i = 1 TO np
  106.     COLOR pc(i)
  107.     LOCATE i + 1, 1: PRINT LEFT$(p$(i), 1);
  108.     PRINT USING " ##.#"; pv(i) / 1000;
  109.     tm = pt(i) / 60
  110.     th = tm / 60
  111.     td = th / 24
  112.     ty = td / 365
  113.     PRINT USING " ###.#  "; ty;
  114. ' ------------------------------------------------------------------------
  115. intervals:
  116. DATA 1,Second
  117. DATA 60,Minute
  118. DATA 60,Hour
  119. DATA 24,Day
  120. DATA 7,Week
  121. DATA 4,Month
  122. DATA 12,Year
  123. ' ------------------------------------------------------------------------
  124. planets:
  125. '                        miles  radius
  126. '                  mass  (mil)    (mi)
  127. DATA 0,"Sun    ",334448,0,435960
  128. DATA 1,"Mercury",0.05,36.2,1500
  129. DATA 2,"Venus  ",0.816,67.2,3800
  130. DATA 3,"Earth  ",1,93.2,3958
  131. DATA 4,"Mars   ",0.15,141.6,2090
  132. DATA 5,"Jupiter",318,483,43000
  133. DATA 6,"Saturn ",95,886,37000
  134. DATA 7,"Uranus ",14.6,1790,15000
  135. DATA 8,"Neptune",17.2,2800,14000
  136. DATA 9,"Pluto  ",.5,4600,1979
  137. ' ------------------------------------------------------------------------
  138.  
It works better if you plug it in.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: First attempt for a solar system 2D
« Reply #12 on: June 03, 2019, 11:53:09 am »
That's very nice implementation of solar system, Richard Frost. :)
I do remember that once upon a time, I tried to code 3D a solar system but it was without the sun and the planets only rotate (does not revolve).
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: First attempt for a solar system 2D
« Reply #13 on: June 03, 2019, 02:04:23 pm »
Optical illusion occurs switching to "year" interval with 3 selected. It looks like the orbits reverse, much the way wheels on cars appear to move backwards when filmed, at least in the old days.

I'm not sure about the intervals. Seems fast to me. Is it that a day interval means for every real second that passes, a days worth of orbit is achieved? If so, it seems to run too fast on my system. I would think Earth should complete an orbit in 364+ seconds, but it completes in about 14 seconds. What is an eye opener is how in real time it seems like nothing is moving. It proves as humans, we crave action over reality. I'm sure God looks at it another way. What? Pluto completed another full orbit already? Wow, seems like just yester-century...

Neat demo!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: First attempt for a solar system 2D
« Reply #14 on: June 03, 2019, 02:29:38 pm »
Trace paths of 3 moons about 3 planets:
Code: QB64: [Select]
  1. _TITLE "Trace 3 Moons around 3 Planets Paths"
  2. 'B+ started 2019-06-01  with recursive MakeBodies sub
  3. CONST xmaxScreen = 1000, ymaxscreen = 740
  4.  
  5. SCREEN _NEWIMAGE(xmaxScreen, ymaxscreen, 32)
  6.  
  7. TYPE bodyType
  8.     ThisI AS INTEGER
  9.     ParentI AS INTEGER '     gives the x,y origin of center of orbit
  10.     OrbitI AS INTEGER
  11.     Distance AS SINGLE '       R from center origin
  12.     Angle AS SINGLE '          current location around center
  13.     AngleChange AS SINGLE 'change of angle each frame
  14.     X AS SINGLE 'x, y location from above info needed for child planets
  15.     Y AS SINGLE
  16.     R AS SINGLE
  17.     C AS _UNSIGNED LONG
  18.  
  19. REDIM SHARED B(0) AS bodyType
  20.  
  21. makeBodies 0, 0, 0
  22.     LINE (0, 0)-(xmaxScreen, ymaxscreen), _RGBA32(0, 0, 0, 5), BF
  23.     LOCATE 1, 1: PRINT "Body count:"; BC
  24.     drawBodies
  25.     _DISPLAY
  26.     _LIMIT 10
  27.  
  28. SUB makeBodies (generation AS INTEGER, OrbitI AS INTEGER, ParentI AS INTEGER)
  29.     DIM oi AS INTEGER, pI AS INTEGER, r AS INTEGER, g AS INTEGER, b AS INTEGER
  30.  
  31.     IF generation > 2 THEN
  32.         EXIT SUB
  33.     ELSE
  34.         BC = BC + 1
  35.         REDIM _PRESERVE B(BC) AS bodyType
  36.         pI = BC '< save non shared value
  37.         IF BC = 1 THEN 'the sun
  38.             B(BC).ThisI = BC
  39.             B(BC).ParentI = 0
  40.             B(BC).Distance = 350
  41.             B(BC).AngleChange = _PI(1 / 1440)
  42.             B(BC).X = xmaxScreen / 2
  43.             B(BC).Y = ymaxscreen / 2
  44.             B(BC).R = 20
  45.             B(BC).C = &HFFFFFFAA
  46.         ELSE 'the body has a parent from which we decide some numbers for location
  47.             B(BC).ThisI = BC
  48.             B(BC).ParentI = ParentI
  49.             B(BC).OrbitI = OrbitI
  50.             B(BC).Distance = B(ParentI).Distance / 10
  51.             B(BC).Angle = RND * _PI(2)
  52.             B(BC).AngleChange = 10 / OrbitI ^ 2 * B(ParentI).AngleChange
  53.             B(BC).X = B(ParentI).X + (3 * B(BC).OrbitI + 2) * B(BC).Distance * COS(B(BC).Angle)
  54.             B(BC).Y = B(ParentI).Y + .85 * (3 * B(BC).OrbitI + 2) * B(BC).Distance * SIN(B(BC).Angle)
  55.             B(BC).R = B(ParentI).R / 8
  56.             IF B(BC).R < 1 THEN B(BC).R = 1
  57.             IF generation = 1 THEN
  58.                 B(BC).C = _RGB32(RND * 55 + 200, RND * 155 + 100, RND * 155 + 100)
  59.             ELSE
  60.                 r = _RED32(B(B(BC).ParentI).C)
  61.                 g = _GREEN32(B(B(BC).ParentI).C)
  62.                 b = _BLUE32(B(B(BC).ParentI).C)
  63.                 B(BC).C = _RGB32(r - OrbitI * 45, g - OrbitI * 45, b - OrbitI * 45)
  64.             END IF
  65.         END IF
  66.         FOR oi = 1 TO 3
  67.             makeBodies generation + 1, oi, pI
  68.         NEXT
  69.     END IF
  70.  
  71. SUB drawBodies
  72.     DIM i AS INTEGER
  73.     FOR i = 1 TO BC
  74.         IF i > 1 THEN
  75.             B(i).Angle = B(i).Angle + B(i).AngleChange
  76.             B(i).X = B(B(i).ParentI).X + (3 * B(i).OrbitI + 2) * B(i).Distance * COS(B(i).Angle)
  77.             B(i).Y = B(B(i).ParentI).Y + .85 * (3 * B(i).OrbitI + 2) * B(i).Distance * SIN(B(i).Angle)
  78.         END IF
  79.         fcirc B(i).X, B(i).Y, B(i).R, B(i).C
  80.     NEXT
  81.  
  82. SUB fcirc (CX AS INTEGER, CY AS INTEGER, RR AS INTEGER, C AS _UNSIGNED LONG)
  83.     DIM R AS INTEGER, RError AS INTEGER
  84.     DIM X AS INTEGER, Y AS INTEGER
  85.  
  86.     R = ABS(RR)
  87.     RError = -R
  88.     X = R
  89.     Y = 0
  90.     IF R = 0 THEN PSET (CX, CY), C: EXIT SUB
  91.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  92.     WHILE X > Y
  93.         RError = RError + Y * 2 + 1
  94.         IF RError >= 0 THEN
  95.             IF X <> Y + 1 THEN
  96.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  97.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  98.             END IF
  99.             X = X - 1
  100.             RError = RError - X * 2
  101.         END IF
  102.         Y = Y + 1
  103.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  104.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  105.     WEND
  106.