Author Topic: Starfield 2020 - December Version  (Read 6370 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Starfield 2020 - December Version
« Reply #15 on: December 11, 2020, 04:58:01 pm »
Actually Petr, that does add on to my idea.... coming soon!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Starfield 2020 - December Version
« Reply #16 on: December 11, 2020, 08:09:22 pm »
 SierraKen

Your starfield is impressive. The stars go faster as they travel.  I still don't know how to do multiple star paths at the same time.


« Last Edit: December 11, 2020, 08:17:47 pm by NOVARSEG »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Starfield 2020 - December Version
« Reply #17 on: December 11, 2020, 09:28:58 pm »
NOVARSEG, thanks! In order to make many graphics on the screen move at once,  you need an array using DIM statements. If you look closely at my code, you can see that there's a starting position for every star and the (s) counts them up to 950 stars. That puts in memory 950 stars with starx(s) and stary(s). Make sure somewhere in the beginning of your code you add a DIM statement to make memory available first. For example, I use DIM starx(1000), stary(1000) then to move them, you also need a direction for each graphic you are moving and with that you can also make an array for each graphic to use that certain variable in the array. Then to make them move, I added a FOR/NEXT loop that goes to 950 for all the stars. This makes them all go at once. Mine is a bit complicating for beginners so if you wish, you could try 10 drops of snow moving at once at the same speed or whatever you wish to do. B+ taught me most of this last year and I've been programming with BASIC off and on since the 1980's LOL.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Starfield 2020 - December Version
« Reply #18 on: November 23, 2021, 12:06:26 pm »
Here is a question - on my old Mac Classic there was a starfield simulation screensaver where if you pressed the cursor keys up/down/right/left, the stars started moving accordingly, like you were "steering" the spaceship. How would you get that effect, like steering in the old Atari Star Raiders?

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Starfield 2020 - December Version
« Reply #19 on: November 23, 2021, 07:23:07 pm »
Here is a question - on my old Mac Classic there was a starfield simulation screensaver where if you pressed the cursor keys up/down/right/left, the stars started moving accordingly, like you were "steering" the spaceship. How would you get that effect, like steering in the old Atari Star Raiders?

I did a fullscreen star field that you can steer. It's not a proper position transformation but "looks" fairly convincing. I'm not sure how it would compare to any particular game action, having not played many.

Code: QB64: [Select]
  1. _TITLE "OldMoses' 3D StarField V.3 (h to toggle help menu)"
  2.  
  3. TYPE star '                                                     define the star data type
  4.     x AS INTEGER '                                              star x coordinate
  5.     y AS INTEGER '                                              star y coordinate
  6.     z AS INTEGER '                                              star z coordinate
  7.     r AS INTEGER '                                              red component
  8.     g AS INTEGER '                                              green component
  9.     b AS INTEGER '                                              blue component
  10.     xa AS INTEGER '                                             apparent x position on screen
  11.     ya AS INTEGER '                                             apparent y position on screen
  12.  
  13. DIM SHARED scrw AS INTEGER '                                    screen width
  14. DIM SHARED scw2 AS INTEGER '                                    1/2 screen width
  15. DIM SHARED scrh AS INTEGER '                                    screen height
  16. DIM SHARED sch2 AS INTEGER '                                    1/2 screen height
  17. DIM SHARED starcount AS INTEGER: starcount = 4000 '             set number of stars to begin with
  18. DIM SHARED p(starcount) AS star '                               dimension the star data array
  19. DIM speed AS INTEGER: speed = 8 '                               warp factor
  20. DIM vwport AS INTEGER: vwport = 1200 '                          set the viewport setback from viewer's eye
  21. DIM xch AS _BYTE '                                              x turn state
  22. DIM ych AS _BYTE '                                              y turn state
  23. DIM hlptog AS _BYTE: hlptog = 0 '                               help menu display toggle
  24. DIM dattog AS _BYTE: dattog = -1 '                              data bar display toggle
  25. DIM tartog AS _BYTE: tartog = 0 '                               target crosshair
  26. DIM lartog AS _BYTE: lartog = 0 '                               large star toggle
  27. DIM bsrad AS _BYTE: bsrad = 1 '                                 star swell radius
  28.  
  29. scrw = _DESKTOPWIDTH: scrh = _DESKTOPHEIGHT - 40
  30. a& = _NEWIMAGE(scrw, scrh, 32)
  31. scw2 = scrw / 2
  32. sch2 = scrh / 2
  33.  
  34. PopVoid '                                                       OldMoses said "Let there be light" and the universe was, is and ever shall be
  35. WINDOW (-scw2, sch2)-(scw2, -sch2) '                            create viewport window, Cap'n Kirk spends his days staring at this
  36. _PRINTMODE _KEEPBACKGROUND '                                    stars show through help menu
  37.  
  38. DO '                                                            draw the stars
  39.     IF _KEYDOWN(104) THEN hlptog = NOT hlptog: _DELAY .1 '      toggle help menu
  40.     IF _KEYDOWN(100) THEN dattog = NOT dattog: _DELAY .1 '      toggle data bar
  41.     IF _KEYDOWN(116) THEN tartog = NOT tartog: _DELAY .1 '      toggle target reticle
  42.     IF _KEYDOWN(108) THEN lartog = NOT lartog: _DELAY .1 '      toggle large stars on/off
  43.     IF _KEYDOWN(43) THEN speed = speed + 1: _DELAY .1 '         increase speed
  44.     IF _KEYDOWN(45) THEN speed = speed - 1: _DELAY .1 '         decrease speed
  45.     IF _KEYDOWN(19200) THEN xch = 1: _DELAY .001 '               turn left
  46.     IF _KEYDOWN(19712) THEN xch = -1: _DELAY .001 '              turn right
  47.     IF _KEYDOWN(18432) THEN ych = -1: _DELAY .001 '              turn up
  48.     IF _KEYDOWN(20480) THEN ych = 1: _DELAY .001 '               turn down
  49.     IF _KEYDOWN(122) THEN vwport = vwport * 2: _DELAY .1 '      zoom x 2
  50.     IF _KEYDOWN(120) THEN vwport = 1200: _DELAY .1 '            zoom normal
  51.     IF _KEYDOWN(97) THEN '                                      add stars
  52.         starcount = starcount + 1 '
  53.         AddStar starcount '
  54.     END IF
  55.     IF _KEYDOWN(115) THEN
  56.         starcount = starcount - 1 '                             subtract stars
  57.         REDIM _PRESERVE p(starcount) AS star '
  58.     END IF
  59.     CLS
  60.     vp12 = vwport / 1200
  61.     pitch = -(SIN(ych * 0.005236) / 2) * (ych <> 0)
  62.     yaw = -(SIN(xch * 0.005236) / 2) * (xch <> 0)
  63.  
  64.     FOR x% = 1 TO starcount '                                    iterate through all stars
  65.  
  66.         IF xch <> 0 OR ych <> 0 THEN '                          not actually a proper transformation
  67.             p(x%).x = p(x%).x + p(x%).z * yaw '                    slew stars for left/right turns
  68.             p(x%).y = p(x%).y + p(x%).z * pitch '                  slew stars for up/down pitch
  69.         END IF
  70.  
  71.         IF p(x%).z > 0 THEN 'ignore those behind, change to a dot product test if a facing vector is later added
  72.             p(x%).xa = p(x%).x / p(x%).z * vwport '                get relative screen position from absolute position for x & y
  73.             p(x%).ya = p(x%).y / p(x%).z * vwport
  74.  
  75.             IF ABS(p(x%).xa) < scw2 AND ABS(p(x%).ya) < sch2 THEN 'place the star if within the viewport
  76.                 dst& = (_HYPOT(_HYPOT(ABS(p(x%).x), ABS(p(x%).y)), ABS(p(x%).z))) / vp12 'distance to star / zoom
  77.                 IF lartog THEN '                                Larger near field stars
  78.                     SELECT CASE dst&
  79.                         CASE 0 TO 1875 'fade to circle 16
  80.                             FCirc p(x%).xa, p(x%).ya, bsrad + 5, _RGBA32(p(x%).r, p(x%).g, p(x%).b, map!(dst&, 1875, 0, 0, 255)) 'proximity star plot
  81.                             FCirc p(x%).xa, p(x%).ya, bsrad + 3, _RGBA32(p(x%).r, p(x%).g, p(x%).b, 255) 'proximity star plot
  82.                         CASE 1876 TO 3750 'fade to circle 8
  83.                             FCirc p(x%).xa, p(x%).ya, bsrad + 3, _RGBA32(p(x%).r, p(x%).g, p(x%).b, map!(dst&, 3750, 1876, 0, 255)) 'proximity star plot
  84.                             FCirc p(x%).xa, p(x%).ya, bsrad + 1, _RGBA32(p(x%).r, p(x%).g, p(x%).b, 255) 'proximity star plot
  85.                         CASE 3751 TO 7500 'fade to circle 4
  86.                             FCirc p(x%).xa, p(x%).ya, bsrad + 1, _RGBA32(p(x%).r, p(x%).g, p(x%).b, map!(dst&, 7500, 3751, 0, 255)) 'proximity star plot
  87.                             FCirc p(x%).xa, p(x%).ya, bsrad, _RGBA32(p(x%).r, p(x%).g, p(x%).b, 255)
  88.                         CASE 7501 TO 14999 'fade to circle 2
  89.                             FCirc p(x%).xa, p(x%).ya, bsrad, _RGBA32(p(x%).r, p(x%).g, p(x%).b, map!(dst&, 14999, 7501, 0, 255)) 'near star plot
  90.                             PSET (p(x%).xa, p(x%).ya), _RGBA32(p(x%).r, p(x%).g, p(x%).b, 255)
  91.                         CASE IS > 15000 'point fade in
  92.                             PSET (p(x%).xa, p(x%).ya), _RGBA32(p(x%).r, p(x%).g, p(x%).b, map!(dst&, 30000, 15000, 0, 255)) 'far star plot
  93.                     END SELECT
  94.                 ELSE
  95.                     SELECT CASE dst&
  96.                         CASE 0 TO 3999 '                        Smaller near field, using map algorithm
  97.                             FCirc p(x%).xa, p(x%).ya, bsrad + 1, _RGBA32(p(x%).r, p(x%).g, p(x%).b, map!(dst&, 3999, 0, 0, 255)) 'proximity star plot
  98.                             FCirc p(x%).xa, p(x%).ya, bsrad, _RGBA32(p(x%).r, p(x%).g, p(x%).b, 255)
  99.                         CASE 4000 TO 14999
  100.                             FCirc p(x%).xa, p(x%).ya, bsrad, _RGBA32(p(x%).r, p(x%).g, p(x%).b, map!(dst&, 14999, 4000, 0, 255)) 'near star plot
  101.                             PSET (p(x%).xa, p(x%).ya), _RGBA32(p(x%).r, p(x%).g, p(x%).b, 255)
  102.                         CASE IS > 15000
  103.                             PSET (p(x%).xa, p(x%).ya), _RGBA32(p(x%).r, p(x%).g, p(x%).b, map!(dst&, 30000, 15000, 0, 255)) 'far star plot
  104.                     END SELECT
  105.                 END IF '                                        end: large star toggle test
  106.             END IF '                                            end: stars within viewport test
  107.         END IF '                                                end: stars in front of viewer test
  108.  
  109.         IF speed <> 0 THEN
  110.             p(x%).z = p(x%).z - speed '                           move the star relative to the viewer
  111.             IF ABS(p(x%).z) > 30000 THEN '                       recycle stars that leave the limits
  112.                 ReplaceStar x%, p(x%).z - (30000 * -SGN(speed))
  113.             END IF
  114.         END IF
  115.  
  116.     NEXT x%
  117.     xch = 0: ych = 0
  118.     IF hlptog THEN HelpScreen
  119.     IF dattog THEN Footer speed, vp12
  120.     IF tartog THEN XHair
  121.     _DISPLAY '                                                  eliminate screen flicker
  122.     _LIMIT 1000 '                                                smooth out the action
  123.  
  124.  
  125.  
  126. SUB PopVoid '                                                   Do an initial population of stars
  127.  
  128.     FOR x% = 1 TO starcount '                                    place a 'starcount' # of stars randomly in a 3D space
  129.         RANDOMIZE TIMER
  130.         p(x%).x = INT(RND * 60000) - 30000
  131.         p(x%).y = INT(RND * 60000) - 30000
  132.         p(x%).z = INT(RND * 60000) - 30000
  133.  
  134.         t% = INT(RND * 110) - 55 '                              star spectrum/color
  135.         ch = INT(RND * 6)
  136.         IF ch < 5 THEN
  137.             p(x%).r = 200 + t%: p(x%).b = 200 - t%
  138.         ELSE
  139.             p(x%).r = 200 - t%: p(x%).b = 200 + t%
  140.         END IF
  141.         g% = INT(RND * 6)
  142.         IF g% = 0 THEN
  143.             g1% = INT(RND * 7) + 1
  144.             p(x%).g = 200 + gi% ^ 2
  145.         ELSE
  146.             p(x%).g = 200
  147.         END IF
  148.     NEXT x%
  149.  
  150. END SUB 'PopVoid
  151.  
  152.  
  153. SUB ReplaceStar (var AS INTEGER, insert AS INTEGER) '           This replaces any star that goes beyond z limits
  154.  
  155.     p(var).x = INT(RND * 60000) - 30000 '                       New x,y,z but keep old color for simplicity sake
  156.     p(var).y = INT(RND * 60000) - 30000
  157.     p(var).z = p(var).z + insert
  158.  
  159. END SUB 'ReplaceStar
  160.  
  161.  
  162. SUB FCirc (CX AS INTEGER, CY AS INTEGER, RR AS INTEGER, C AS _UNSIGNED LONG)
  163.     'Steve's circle fill
  164.     DIM R AS INTEGER, RError AS INTEGER
  165.     DIM X AS INTEGER, Y AS INTEGER
  166.  
  167.     R = ABS(RR)
  168.     RError = -R
  169.     X = R
  170.     Y = 0
  171.     IF R = 0 THEN PSET (CX, CY), C: EXIT SUB
  172.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  173.     WHILE X > Y
  174.         RError = RError + Y * 2 + 1
  175.         IF RError >= 0 THEN
  176.             IF X <> Y + 1 THEN
  177.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  178.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  179.             END IF
  180.             X = X - 1
  181.             RError = RError - X * 2
  182.         END IF
  183.         Y = Y + 1
  184.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  185.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  186.     WEND
  187. END SUB 'FCirc
  188.  
  189.  
  190. SUB AddStar (x AS INTEGER)
  191.  
  192.     REDIM _PRESERVE p(x) AS star
  193.     p(x).x = INT(RND * 60000) - 30000
  194.     p(x).y = INT(RND * 60000) - 30000
  195.     IF speed > 0 THEN
  196.         p(x).z = 30000
  197.     ELSE
  198.         p(x).z = -30000
  199.     END IF
  200.     t% = INT(RND * 110) - 55
  201.     ch = INT(RND * 6)
  202.     IF ch < 5 THEN
  203.         p(x).r = 200 + t%: p(x).b = 200 - t%
  204.     ELSE
  205.         p(x).r = 200 - t%: p(x).b = 200 + t%
  206.     END IF
  207.     g% = INT(RND * 6)
  208.     IF g% = 0 THEN
  209.         g1% = INT(RND * 7) + 1
  210.         p(x).g = 200 + gi% ^ 2
  211.     ELSE
  212.         p(x).g = 200
  213.     END IF
  214.  
  215. END SUB 'AddStar
  216.  
  217. SUB HelpScreen
  218.  
  219.     mi = (sch2) - (15 * 16 / 2) '                           change ...(x * 16 / 2) to x= number of items in menu
  220.     _PRINTSTRING (scw2 - 150, mi), "<+> increase speed", 0
  221.     _PRINTSTRING (scw2 - 150, mi + 16), "<-> decrease speed", 0
  222.     _PRINTSTRING (scw2 - 150, mi + 32), "<left arrow> turn left", 0
  223.     _PRINTSTRING (scw2 - 150, mi + 48), "<right arrow> turn right", 0
  224.     _PRINTSTRING (scw2 - 150, mi + 64), "<up arrow> pitch up", 0
  225.     _PRINTSTRING (scw2 - 150, mi + 80), "<down arrow> pitch down", 0
  226.     _PRINTSTRING (scw2 - 150, mi + 96), "<z> zoom x 2", 0
  227.     _PRINTSTRING (scw2 - 150, mi + 112), "<x> zoom original", 0
  228.     _PRINTSTRING (scw2 - 150, mi + 128), "<a> add stars", 0
  229.     _PRINTSTRING (scw2 - 150, mi + 144), "<s> subtract stars", 0
  230.     _PRINTSTRING (scw2 - 150, mi + 160), "<h> toggle this list on/off", 0
  231.     _PRINTSTRING (scw2 - 150, mi + 176), "<d> toggle data bar", 0
  232.     _PRINTSTRING (scw2 - 150, mi + 192), "<t> toggle crosshairs", 0
  233.     _PRINTSTRING (scw2 - 150, mi + 208), "<l> toggle large stars on/off", 0
  234.     _PRINTSTRING (scw2 - 150, mi + 224), "<ESC> quit", 0
  235.  
  236. END SUB 'HelpScreen
  237.  
  238. SUB Footer (var AS INTEGER, var2 AS INTEGER)
  239.  
  240.     _PRINTSTRING (5, scrh - 60), "Warp " + STR$(var), 0 '     speed indicator
  241.     IF var > 10 THEN _PRINTSTRING (90, scrh - 60), "Ach! muh poor bairns..."
  242.     _PRINTSTRING (scw2 - 50, scrh - 60), "Stars = " + STR$(starcount), 0
  243.     _PRINTSTRING (scrw - 100, scrh - 60), "Mag. x" + STR$(var2), 0 '   magnification factor
  244.  
  245. END SUB 'Footer
  246.  
  247. SUB XHair
  248.  
  249.     LINE (-50, -50)-(-10, -10), &H7FFF0000
  250.     LINE (50, -50)-(10, -10), &H7FFF0000
  251.     LINE (50, 50)-(10, 10), &H7FFF0000
  252.     LINE (-50, 50)-(-10, 10), &H7FFF0000
  253.  
  254. END SUB 'XHair
  255.  
  256. FUNCTION map! (value!, minRange!, maxRange!, newMinRange!, newMaxRange!)
  257.  
  258.     'called from: various
  259.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  260.  
  261.  
  262.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Starfield 2020 - December Version
« Reply #20 on: November 23, 2021, 08:16:03 pm »
Yeah I gave it a try here: https://www.qb64.org/forum/index.php?topic=4401.msg138417#msg138417

Not sure about the effects being accurate either, but you sure can see turning!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Starfield 2020 - December Version
« Reply #21 on: November 23, 2021, 10:52:12 pm »
Pretty cool starfield.... One minor point... In 'full screen' mode you would be guessing as how fast you were travelling...
Logic is the beginning of wisdom.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
Re: Starfield 2020 - December Version
« Reply #22 on: November 30, 2021, 09:14:36 am »
I did a fullscreen star field that you can steer. It's not a proper position transformation but "looks" fairly convincing. I'm not sure how it would compare to any particular game action, having not played many.

Thanks @OldMoses, that was exactly what I was looking for. Nice program!