Author Topic: Help with Starfield  (Read 14170 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #45 on: August 11, 2019, 12:17:24 pm »
SMcNeill, thanks for your math demonstration, I finally noticed it. Since there's no SQR command in the program, the programmer just decided to leave it out and have a GIGANTIC circle after all. LOL Still works though. :) Thanks, I'll keep your demonstration for future programs.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #46 on: August 11, 2019, 12:21:51 pm »
LOL Johno. Well you are right, usually... except for 2 episodes I can think of. There's the one where the weird mind alien takes Picard and his crew past the known universe. Then there's this Voyager episode, where they do go to warp 10... https://en.wikipedia.org/wiki/Threshold_(Star_Trek:_Voyager)

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Help with Starfield
« Reply #47 on: August 11, 2019, 12:57:54 pm »
Um... According to Trek-lore* warp speed of 10 cannot be achieved. If it could, then you could occupy every location in the universe, at the same time.
I wonder how that could be represented using QB64's graphics commands.... Now THAT would be cool....

J

* "The Star Trek Encyclopedia - A Reference Guide to the Future" page 372

Some of the old canon lore had warp factors being a cube root of the apparent velocity of warp speed. Warp 1 being light speed, 2 being 8 x c, warp 3 being 27 x c, etc. Even at high warp the show played pretty fast and loose with travel times. Warp 1 wouldn't get you anywhere worth going in less than several years... at warp 6 Proxima Centauri is a week away.

Most of our star field programs are really bookin'.

UPDATE:
Instead of just appearing, stars now do an apparent magnitude 'fade in', and stars in the near field increase in size.

Fixed it, now it's fully bidirectional.

Code: QB64: [Select]
  1. _TITLE "OldMoses' 3D StarField (+ & - speed) (left/right arrows turn)"
  2.  
  3. a& = _NEWIMAGE(600, 600, 32)
  4. TYPE star '                                                     define the star data type
  5.     x AS INTEGER
  6.     y AS INTEGER
  7.     z AS INTEGER
  8.     r AS INTEGER
  9.     g AS INTEGER
  10.     b AS INTEGER
  11.     xa AS INTEGER
  12.     ya AS INTEGER
  13.  
  14. DIM SHARED max AS INTEGER: max = 500
  15. DIM SHARED vprt AS INTEGER: vprt = 600 '                        set the viewport setback from viewer's eye
  16. DIM SHARED p(max) AS star '                                     dimension the star data array
  17. DIM SHARED speed AS INTEGER: speed = 2 '                        warp factor
  18.  
  19. PopVoid '                                                       OldMoses said "Let there be light" and the universe was, is and ever shall be
  20. WINDOW (-300, 300)-(300, -300) '                                create viewport window, Cap'n Kirk spends his days staring at this
  21.  
  22. DO '                                                            draw the stars
  23.     x$ = INKEY$
  24.     IF x$ = "" THEN
  25.         xch = 0: ych = 0
  26.     END IF
  27.     IF x$ = CHR$(43) THEN speed = speed + 1
  28.     IF x$ = CHR$(45) THEN speed = speed - 1
  29.     IF x$ = CHR$(0) + CHR$(75) THEN xch = 1
  30.     IF x$ = CHR$(0) + CHR$(77) THEN xch = -1
  31.     'IF x$ = CHR$(0) + CHR$(72) THEN ych = 1
  32.     'IF x$ = CHR$(0) + CHR$(80) THEN ych = -1
  33.     CLS
  34.     FOR x = 1 TO max '                                          iterate through all stars
  35.         dst = Pythagorus(p(x)) '                                distance to star
  36.         p(x).x = p(x).x + SinCalc(p(x).z, xch * 0.3)
  37.         'p(x).y = p(x).y + CosCalc(p(x).z, ych * 0.3) '          <<<this just doesn't work
  38.         p(x).xa = p(x).x / dst * vprt '                         get relative screen position from absolute position for x & y
  39.         p(x).ya = p(x).y / dst * vprt
  40.         IF ABS(p(x).xa) < 301 AND ABS(p(x).ya) < 301 THEN '     place the star if within the viewport
  41.             fdf& = (dst - 12952) / 8
  42.             IF fdf& < 0 THEN fdf& = 0
  43.             SELECT CASE dst
  44.                 CASE IS > 7500
  45.                     PSET (p(x).xa, p(x).ya), _RGBA32(p(x).r, p(x).g, p(x).b, 255 - fdf&)
  46.                 CASE ELSE
  47.                     FCirc p(x).xa, p(x).ya, 1, _RGBA32(p(x).r, p(x).g, p(x).b, 255)
  48.             END SELECT
  49.         END IF
  50.         p(x).z = p(x).z - speed '                               move the star closer to the viewer
  51.         IF speed < 0 THEN
  52.             IF p(x).z > 15000 THEN ReplaceStar x, 0
  53.         ELSE
  54.             IF p(x).z < 0 THEN ReplaceStar x, 15000 '                      add new stars as existing ones go behind the viewer
  55.         END IF
  56.     NEXT x
  57.     _DISPLAY '                                                  eliminate screen flicker
  58.     _LIMIT 500 '                                                smooth out the action
  59.  
  60.  
  61. SUB PopVoid '                                                   Do an initial population of stars
  62.  
  63.     FOR x = 1 TO max '                                          place a 'max' # of stars randomly in a 3D space
  64.         RANDOMIZE TIMER
  65.         p(x).x = INT(RND * 15000) - 7500
  66.         p(x).y = INT(RND * 15000) - 7500
  67.         p(x).z = INT(RND * 15000) + 1
  68.         t% = INT(RND * 110) - 55
  69.         ch = INT(RND * 6)
  70.         IF ch < 4 THEN
  71.             p(x).r = 200 + t%: p(x).b = 200 - t%
  72.         ELSE
  73.             p(x).r = 200 - t%: p(x).b = 200 + t%
  74.         END IF
  75.         p(x).g = 200
  76.     NEXT x
  77.  
  78. END SUB 'PopVoid
  79.  
  80.  
  81. FUNCTION Pythagorus (var1 AS star)
  82.  
  83.     horizontal = _HYPOT(ABS(var1.x), ABS(var1.y)) '             Use to find distance between star and origin (viewer)
  84.     Pythagorus = _HYPOT(horizontal, ABS(var1.z))
  85.  
  86. END FUNCTION 'Pythagorus
  87.  
  88.  
  89. SUB ReplaceStar (var AS INTEGER, insert AS INTEGER) '                              This replaces any star that goes behind the viewer
  90.  
  91.     p(var).x = INT(RND * 15000) - 7500 '                        New x,y,z but keep old color for simplicity sake
  92.     p(var).y = INT(RND * 15000) - 7500
  93.     p(var).z = insert
  94.  
  95. END SUB 'ReplaceStar
  96.  
  97. FUNCTION SinCalc (var1 AS _INTEGER64, var2 AS SINGLE)
  98.  
  99.     'Polar coordinate X finder
  100.     'used to find X coordinate of a speed/distance (var1) and heading/azimuth (var2)
  101.     SinCalc = var1 * SIN(_D2R(var2))
  102.  
  103. END FUNCTION 'SinCalc
  104.  
  105.  
  106. FUNCTION CosCalc (var1 AS _INTEGER64, var2 AS SINGLE)
  107.  
  108.     'Polar coordinate Y finder
  109.     'used to find Y coordinate of a speed/distance (var1) and heading/azimuth (var2)
  110.     CosCalc = var1 * COS(_D2R(var2))
  111.  
  112. END FUNCTION 'CosCalc
  113.  
  114.  
  115. SUB FCirc (CX AS INTEGER, CY AS INTEGER, RR AS INTEGER, C AS _UNSIGNED LONG)
  116.     DIM R AS INTEGER, RError AS INTEGER
  117.     DIM X AS INTEGER, Y AS INTEGER
  118.  
  119.     R = ABS(RR)
  120.     RError = -R
  121.     X = R
  122.     Y = 0
  123.     IF R = 0 THEN PSET (CX, CY), C: EXIT SUB
  124.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  125.     WHILE X > Y
  126.         RError = RError + Y * 2 + 1
  127.         IF RError >= 0 THEN
  128.             IF X <> Y + 1 THEN
  129.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  130.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  131.             END IF
  132.             X = X - 1
  133.             RError = RError - X * 2
  134.         END IF
  135.         Y = Y + 1
  136.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  137.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  138.     WEND
  139. END SUB 'FCirc
  140.  
« Last Edit: August 11, 2019, 04:08:22 pm by OldMoses »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #48 on: August 11, 2019, 03:21:50 pm »
Hi Andy,

That has very nice 3D look! You have (probably) seen what happens when press - too long? :)

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Help with Starfield
« Reply #49 on: August 11, 2019, 03:48:20 pm »
Hi Andy,

That has very nice 3D look! You have (probably) seen what happens when press - too long? :)

Yep, it swallows my universe into oblivion and it doesn't come back. I may have to work on that one. ;)

Update: simple fix, now it goes both ways.
« Last Edit: August 11, 2019, 04:09:03 pm by OldMoses »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #50 on: August 11, 2019, 04:38:24 pm »
That's really cool looking OldMoses!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Help with Starfield
« Reply #51 on: August 11, 2019, 05:02:11 pm »
That's really cool looking OldMoses!

Thanks, when I wrote it, I was shocked at how simple the math was. It's basically a star "wind tunnel", where the stars stay locked in the x & y position and only their z axis position is advanced toward the viewer. Their actual x & y are translated to an apparent x & y that is plotted on a virtual viewport at z=600, while the stars themselves go as far out as z=15,000. It's the ratio of the distance to the star and the distance to the viewport that gives the "parallax" effect of nearer stars moving faster, when in fact they are all advancing at the same speed. I learned that trick of perspective while researching ray tracing algorithms recently.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #52 on: August 11, 2019, 06:40:57 pm »
Amazing OldMoses.

Well, for the fun of it and the Star Trek geek I am, I added the mouse to mine where you can move the mouse left or right and see what the stars look like flying by on the side of you. Like when it shows the Star Trek people looking out a side window and the stars fly by. I had to add some numbers for the stars to last longer, but it was pretty easy.
Check it out:

Code: QB64: [Select]
  1. 'Thank you to B+ and other guys from the QB64.org forum for their code examples to make this program!
  2.  
  3. _TITLE "Warp Speed  - Use Up and Down Arrow Keys"
  4. DIM x(800), y(800), dx(800), dy(800), sz(800), c(800)
  5.  
  6. SCREEN _NEWIMAGE(800, 800, 32)
  7.  
  8. 'Set the default speed, amount of stars, and then make the first stars.
  9. speed = .001
  10. amount = 200
  11. xx = 0
  12. FOR stars = 1 TO amount
  13.     GOSUB make:
  14. NEXT stars
  15.  
  16.     _LIMIT 1000
  17.     FOR stars = 1 TO amount
  18.         a$ = INKEY$
  19.         IF a$ = CHR$(27) THEN END
  20.         DO WHILE _MOUSEINPUT
  21.             mouseX = _MOUSEX
  22.             mouseY = _MOUSEY
  23.             mouseLeftButton = _MOUSEBUTTON(1)
  24.             mouseRightButton = _MOUSEBUTTON(2)
  25.             mouseMiddleButton = _MOUSEBUTTON(3)
  26.             mouseWheel = mouseWheel + _MOUSEWHEEL
  27.         LOOP
  28.         xx = (mouseX - 400) * 8
  29.         xx = xx * -1
  30.         'Detect the arrow keys to make it go faster or slower.
  31.         IF a$ = CHR$(0) + CHR$(72) THEN speed = speed + .001: warp = warp + 1
  32.         IF a$ = CHR$(0) + CHR$(80) THEN speed = speed - .001: warp = warp - 1
  33.         IF warp < .002 THEN warp = 0
  34.         IF warp > 10 THEN warp = 10
  35.         IF warp > 0 THEN LOCATE 1, 1: PRINT "Warp: "; warp
  36.         IF warp = 0 THEN LOCATE 1, 1: PRINT "Cruise Speed"
  37.         IF speed < .001 THEN speed = .001
  38.         IF speed > .01 THEN speed = .01
  39.  
  40.         'Move the stars.
  41.         x(stars) = x(stars) + dx(stars) * speed
  42.         y(stars) = y(stars) + dy(stars) * speed
  43.  
  44.         'Get a new star if one goes off the screen.
  45.         IF x(stars) < -6400 OR x(stars) > 6400 OR y(stars) < -400 OR y(stars) > 400 THEN GOSUB make:
  46.  
  47.         'Draw the stars, c(stars) is the choice of color.
  48.         IF sz(stars) > 2 THEN sz(stars) = 2
  49.         IF c(stars) < 65 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(255, 255, 255)
  50.         IF c(stars) > 64 AND c(stars) < 85 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(100, 10, 10)
  51.         IF c(stars) > 84 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(10, 10, 200)
  52.  
  53.         'If the size of the star is less than 1, skip paint.
  54.         IF sz(stars) < 1 THEN GOTO nopaint:
  55.  
  56.         'Paint the stars.
  57.         IF c(stars) < 65 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(255, 255, 255)
  58.         IF c(stars) > 64 AND c(stars) < 85 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(100, 10, 10)
  59.         IF c(stars) > 84 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(10, 10, 200)
  60.         nopaint:
  61.     NEXT stars
  62.  
  63.     'This stops flickering.
  64.     _DISPLAY
  65.  
  66.     'Erase the stars for motion but keep a transparent black for the trails.
  67.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF
  68.  
  69. make:
  70.  
  71. 'Get a random X number using 2 decimal points - RND and .5 and times it by the width of the screen.
  72.  
  73. x(stars) = (RND - .5) * _WIDTH
  74.  
  75. 'Get a random Y number using 2 decimal points - RND and .5 and times it by the height of the screen.
  76.  
  77. y(stars) = (RND - .5) * _HEIGHT
  78.  
  79. 'Get a random star size between .25 and 2.
  80.  
  81. sz(stars) = (RND * 2) + .25
  82.  
  83. 'Get a random number between 1 and 100 to pick 1 of 3 different star colors in the main loop.
  84.  
  85. c(stars) = INT(RND * 100) + 1
  86.  
  87. 'Get any random number between 5 and 10, used for both X and Y, to use in the star movement in the main loop.
  88.  
  89. v = 5 + RND * 5
  90. dx(stars) = x(stars) * v
  91. dy(stars) = y(stars) * v
  92.  
  93.  
  94.  


Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Help with Starfield
« Reply #53 on: August 11, 2019, 07:11:20 pm »
Hi SierraKen

fine and with a speedy change of direction by mouse!

if you like starfield and 4 direction of navigation take a look at this my code porting in QB64 a Java demo
Code: QB64: [Select]
  1. 'starfield in processing
  2. TYPE Star
  3.     x AS DOUBLE
  4.     y AS DOUBLE
  5.     z AS DOUBLE
  6. CONST True = -1, False = NOT True, radStar = 4, countStar = 800, HScreen = 500, WScreen = 800
  7. DIM SHARED a AS LONG, b AS LONG, Zvel AS INTEGER, WarpVel AS INTEGER, Yvel AS INTEGER, Xvel AS INTEGER
  8. DIM Stars(1 TO countStar) AS Star
  9.  
  10.  
  11.  
  12. ' we need two surface to avoid flickering that rises up painting directly on the screen
  13. a = _NEWIMAGE(WScreen, HScreen, 32) 'main screen
  14. b = _NEWIMAGE(WScreen, HScreen, 32) ' panel to draw screen before displaying it
  15.  
  16. WINDOW SCREEN(-WScreen / 2, -HScreen / 2)-(WScreen / 2, HScreen / 2)
  17.  
  18. InitStar Stars()
  19. Zvel = 1
  20. Xvel = 0
  21. Yvel = 0
  22. WarpVel = 1
  23. t# = TIMER
  24.     _LIMIT 120
  25.     DrawStars Stars()
  26.     UpDate Stars()
  27.  
  28.     ' Enter key = reset Key
  29.     IF _KEYDOWN(13) THEN
  30.         Xvel = 0
  31.         Yvel = 0
  32.         Zvel = 5
  33.         WarpVel = 1
  34.     END IF
  35.  
  36.     '19200
  37.     ' Left arrow
  38.     IF _KEYDOWN(19200) AND ABS(TIMER - t#) > .1 AND Xvel > -15 THEN
  39.         Xvel = Xvel - 1
  40.         t# = TIMER
  41.     END IF
  42.  
  43.     '19712
  44.     ' Right arrow
  45.     IF _KEYDOWN(19712) AND ABS(TIMER - t#) > .1 AND Xvel < 15 THEN
  46.         Xvel = Xvel + 1
  47.         t# = TIMER
  48.     END IF
  49.  
  50.  
  51.     '20480
  52.     ' Down arrow
  53.     IF _KEYDOWN(20480) AND ABS(TIMER - t#) > .1 AND Yvel < 15 THEN
  54.         Yvel = Yvel + 1
  55.         t# = TIMER
  56.     END IF
  57.  
  58.  
  59.     '18432
  60.     ' Up arrow
  61.     IF _KEYDOWN(18432) AND ABS(TIMER - t#) > .1 AND Yvel > -15 THEN
  62.         Yvel = Yvel - 1
  63.         t# = TIMER
  64.     END IF
  65.  
  66.     '+
  67.     IF _KEYDOWN(43) AND ABS(TIMER - t#) > .1 AND Zvel < 15 THEN
  68.         Zvel = Zvel + 1
  69.         t# = TIMER
  70.     END IF
  71.  
  72.  
  73.     '-
  74.     IF _KEYDOWN(45) AND ABS(TIMER - t#) > .1 AND Zvel > 0 THEN
  75.         Zvel = Zvel - 1
  76.         t# = TIMER
  77.     END IF
  78.  
  79.     ' WarpVel activate/deactivate the wake effect
  80.     ' w W
  81.     IF _KEYDOWN(87) OR _KEYDOWN(119) THEN
  82.         IF ABS(TIMER - t#) > .1 THEN
  83.             WarpVel = WarpVel * -1
  84.             t# = TIMER
  85.         END IF
  86.     END IF
  87.  
  88.     ' Zvel is min when _mouseX is min on the left and is max when _mouseX is max on the right
  89.     WHILE _MOUSEINPUT = -1
  90.         IF ABS(TIMER - t#) > .1 THEN
  91.             Zvel = map(_MOUSEX, 0, WScreen, 0, 15)
  92.             t# = TIMER
  93.         END IF
  94.     WEND
  95. LOOP UNTIL _KEYHIT = 32 ' stop pressing spacebar
  96.  
  97. SUB UpDate (starField() AS Star)
  98.     DIM i AS INTEGER
  99.  
  100.     FOR i = 1 TO countStar
  101.         starField(i).x = starField(i).x + Xvel
  102.         starField(i).y = starField(i).y + Yvel
  103.         starField(i).z = starField(i).z - Zvel
  104.         IF starField(i).z < 1 OR starField(i).y > HScreen OR starField(i).x > WScreen THEN
  105.             starField(i).z = WScreen
  106.             starField(i).x = MinMax(-WScreen, WScreen) ' new position x
  107.             starField(i).y = MinMax(-HScreen, HScreen) ' new position y
  108.         END IF
  109.     NEXT
  110.  
  111. SUB InitStar (starField() AS Star)
  112.     DIM i AS INTEGER
  113.  
  114.     FOR i = 1 TO countStar
  115.         starField(i).x = MinMax(-WScreen, WScreen)
  116.         starField(i).y = MinMax(-HScreen, HScreen)
  117.         starField(i).z = INT(RND * WScreen) + 1
  118.     NEXT
  119.  
  120. SUB DrawStars (starField() AS Star)
  121.     DIM i AS INTEGER
  122.     DIM sx AS DOUBLE, sy AS DOUBLE, sr AS DOUBLE, px AS DOUBLE, py AS DOUBLE
  123.     _DEST b
  124.     CLS
  125.     FOR i = 1 TO countStar
  126.         ' the ratio x/z is min when x is min and is max when x is max
  127.         sx = map(starField(i).x / starField(i).z, 0, 1, 0, WScreen)
  128.         ' the ratio y/z is min when y is min and is max when y is max
  129.         sy = map(starField(i).y / starField(i).z, 0, 1, 0, HScreen)
  130.         ' radius in warp speed is a point of 1 pixel
  131.         IF WarpVel = True THEN
  132.             sr = 1
  133.         ELSE
  134.             ' radius of circle is max when z is near 0 and min when z is max
  135.             sr = map(starField(i).z, 0, WScreen, radStar, 0)
  136.         END IF
  137.  
  138.         CIRCLE (sx, sy), sr, _RGB(255, 255, 255)
  139.         ' if radius of circle is more than 1 you can fill it else no
  140.         IF sr > 1 THEN PAINT STEP(0, 0), _RGB(255, 255, 255), _RGB(255, 255, 255)
  141.  
  142.         ' if we want draw the wake of the stars when we travel in warp speed
  143.         IF WarpVel = True THEN
  144.             px = map(starField(i).x / (starField(i).z + 25), 0, 1, 0, WScreen)
  145.             py = map(starField(i).y / (starField(i).z + 25), 0, 1, 0, HScreen)
  146.             LINE (sx, sy)-(px, py), _RGB(255, 255, 255)
  147.         END IF
  148.     NEXT
  149.     info
  150.     _DEST a
  151.     _SOURCE b
  152.     _PUTIMAGE
  153.  
  154. FUNCTION MinMax (Max%, Min%)
  155.     IF Max% < Min% THEN SWAP Max%, Min%
  156.     MinMax = INT(RND * (Max% - Min% + 1)) + Min%
  157.  
  158. ' p5.js Functions
  159. FUNCTION map! (value!, mianRange!, maxRange!, newMinRange!, newMaxRange!)
  160.     map! = ((value! - minRange!) / (maxRange! - minRange!)) * (newMaxRange! - newMinRange!) + newMinRange!
  161.  
  162. SUB info
  163.     LOCATE 1, 1: PRINT "Stop";
  164.     LOCATE 1, 98: PRINT "Run";
  165.  
  166.     LOCATE 2, 1: PRINT "Speed:"; Zvel;
  167.     IF WarpVel = True THEN PRINT "Mode Warp ON" ELSE PRINT "Mode Warp OFF"
  168.     LOCATE 2, 91: PRINT "Yvel "; Yvel;
  169.     LOCATE 2, 82: PRINT "Xvel "; Xvel;
  170.     LOCATE 30, 1: PRINT " Help: Spacebar = quit, Enter = reset, + = vel up , - = vel down, Arrows= directions, W = warp mode";
  171.  
  172.  

Thanks to share your code

PS is color of star random (with not specific meaning)?

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

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Help with Starfield
« Reply #54 on: August 11, 2019, 07:31:36 pm »
Pretty cool use of the mouse. It took me a minute figure out that it was working with the width of screen, but after that there was no issues controlling it.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #55 on: August 11, 2019, 08:42:07 pm »
Thanks OldMoses.

TempoidBasic, that's a really cool example! I am working on something similar but not that advanced. I'm going to change from the mouse to the arrow keys if I can figure it out. The mouse is just too touchy and is hard to get back to center, for the general public anyway.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #56 on: August 11, 2019, 10:01:34 pm »
I changed mine to use the arrow keys inside. Arrow keys make it easier to control. Also I made it so the Space Bar puts it back to center. :) One interesting thing I found out is that the xx variable I use to add or subtract from the center, can make it do weird things if it's in the negative lol. Like if you make xx negative, it will show the stars coming from each side of the screen instead and overlapping. lol I removed that part because it just didn't look right. LOL

This version is on my website now along with thanks to B+ and the guys on this forum, as well as a link to this forum. http://www.KensPrograms.com/

Code: QB64: [Select]
  1. 'Thank you to B+ and other guys from the QB64.org forum for their code examples to make this program!
  2.  
  3. _TITLE "Warp Speed  - Use Up and Down Arrow Key for speed and Right and Left Arrows to view sides and Space Bar to center."
  4. DIM x(800), y(800), dx(800), dy(800), sz(800), c(800)
  5.  
  6. SCREEN _NEWIMAGE(800, 800, 32)
  7.  
  8. 'Set the default speed, amount of stars, and then make the first stars.
  9. speed = .001
  10. amount = 200
  11. xx = 0
  12. FOR stars = 1 TO amount
  13.     GOSUB make:
  14. NEXT stars
  15.  
  16.     _LIMIT 1000
  17.     FOR stars = 1 TO amount
  18.         a$ = INKEY$
  19.         IF a$ = CHR$(27) THEN END
  20.         IF a$ = CHR$(0) + CHR$(77) THEN xx = xx + 50
  21.         IF a$ = CHR$(0) + CHR$(75) THEN xx = xx - 50
  22.         IF a$ = " " THEN xx = 0
  23.  
  24.         IF xx > 1200 THEN xx = 1200
  25.         IF xx < -1200 THEN xx = -1200
  26.  
  27.         'Detect the arrow keys to make it go faster or slower.
  28.         IF a$ = CHR$(0) + CHR$(72) THEN speed = speed + .001: warp = warp + 1
  29.         IF a$ = CHR$(0) + CHR$(80) THEN speed = speed - .001: warp = warp - 1
  30.         IF warp < .002 THEN warp = 0
  31.         IF warp > 10 THEN warp = 10
  32.         IF warp > 0 THEN LOCATE 1, 1: PRINT "Warp: "; warp
  33.         IF warp = 0 THEN LOCATE 1, 1: PRINT "Cruise Speed"
  34.         IF speed < .001 THEN speed = .001
  35.         IF speed > .01 THEN speed = .01
  36.  
  37.         'Move the stars.
  38.         x(stars) = x(stars) + dx(stars) * speed
  39.         y(stars) = y(stars) + dy(stars) * speed
  40.  
  41.         'Get a new star if one goes off the screen.
  42.         IF x(stars) < -6400 OR x(stars) > 6400 OR y(stars) < -400 OR y(stars) > 400 THEN GOSUB make:
  43.  
  44.         'Draw the stars, c(stars) is the choice of color.
  45.         IF sz(stars) > 2 THEN sz(stars) = 2
  46.         IF c(stars) < 65 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(255, 255, 255)
  47.         IF c(stars) > 64 AND c(stars) < 85 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(100, 10, 10)
  48.         IF c(stars) > 84 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(10, 10, 200)
  49.  
  50.         'If the size of the star is less than 1, skip paint.
  51.         IF sz(stars) < 1 THEN GOTO nopaint:
  52.  
  53.         'Paint the stars.
  54.         IF c(stars) < 65 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(255, 255, 255)
  55.         IF c(stars) > 64 AND c(stars) < 85 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(100, 10, 10)
  56.         IF c(stars) > 84 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(10, 10, 200)
  57.         nopaint:
  58.     NEXT stars
  59.  
  60.     'This stops flickering.
  61.     _DISPLAY
  62.  
  63.     'Erase the stars for motion but keep a transparent black for the trails.
  64.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF
  65.  
  66. make:
  67.  
  68. 'Get a random X number using 2 decimal points - RND and .5 and times it by the width of the screen.
  69.  
  70. x(stars) = (RND - .5) * _WIDTH
  71.  
  72. 'Get a random Y number using 2 decimal points - RND and .5 and times it by the height of the screen.
  73.  
  74. y(stars) = (RND - .5) * _HEIGHT
  75.  
  76. 'Get a random star size between .25 and 2.
  77.  
  78. sz(stars) = (RND * 2) + .25
  79.  
  80. 'Get a random number between 1 and 100 to pick 1 of 3 different star colors in the main loop.
  81.  
  82. c(stars) = INT(RND * 100) + 1
  83.  
  84. 'Get any random number between 5 and 10, used for both X and Y, to use in the star movement in the main loop.
  85.  
  86. v = 5 + RND * 5
  87. dx(stars) = x(stars) * v
  88. dy(stars) = y(stars) * v
  89.  
  90.  
« Last Edit: August 11, 2019, 10:32:09 pm by SierraKen »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Help with Starfield
« Reply #57 on: August 12, 2019, 06:20:29 pm »
Sierraken
good and cool!

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

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Help with Starfield
« Reply #58 on: August 12, 2019, 07:08:43 pm »
Hi SierraKen

please read this second my feedback only if you want go further to the development of your creature otherwise go on another topic.
And thanks to let me read and try your code.

I can pass you some tips that at its time I got from Qb64 masters...

1.  smoother keyboard input with new QB64 function than Inkey$

see a mod of your program

Code: QB64: [Select]
  1. 'Thank you to B+ and other guys from the QB64.org forum for their code examples to make this program!
  2. '2019-08-12 TDB mod introducing _keyhit and _Keyclear to a   smoother response to keyboard
  3.  
  4. _TITLE "Warp Speed  - Use Up and Down Arrow Key for speed and Right and Left Arrows to view sides and Space Bar to center."
  5. DIM x(800), y(800), dx(800), dy(800), sz(800), c(800)
  6.  
  7. SCREEN _NEWIMAGE(800, 800, 32)
  8.  
  9. 'Set the default speed, amount of stars, and then make the first stars.
  10. speed = .001
  11. amount = 200
  12. xx = 0
  13. FOR stars = 1 TO amount
  14.     GOSUB make:
  15. NEXT stars
  16.  
  17.     _LIMIT 30 ' <---- it seems enough in respect of 1000
  18.     FOR stars = 1 TO amount
  19.         ' a$ = INKEY$
  20.  
  21.         u = 0 ' a tricky way to reset the variable of input
  22.         u = _KEYHIT
  23.         IF u = 27 THEN END
  24.         IF u = 19200 THEN xx = xx - 50
  25.         IF u = 19712 THEN xx = xx + 50
  26.         IF u = 32 THEN xx = 0
  27.         ' IF a$ = CHR$(27) THEN END
  28.         ' IF a$ = CHR$(0) + CHR$(77) THEN xx = xx + 50
  29.         ' IF a$ = CHR$(0) + CHR$(75) THEN xx = xx - 50
  30.         ' IF a$ = " " THEN xx = 0
  31.  
  32.         IF xx > 1200 THEN xx = 1200
  33.         IF xx < -1200 THEN xx = -1200
  34.  
  35.         'Detect the arrow keys to make it go faster or slower.
  36.         IF u = 18432 THEN speed = speed + .001: warp = warp + 1
  37.         IF u = 20480 THEN speed = speed - .001: warp = warp - 1
  38.         REM _KEYCLEAR  '<---- you can get the same effect REMming u = 0 and activating this statement here
  39.  
  40.         'IF a$ = CHR$(0) + CHR$(72) THEN speed = speed + .001: warp = warp + 1
  41.         'IF a$ = CHR$(0) + CHR$(80) THEN speed = speed - .001: warp = warp - 1
  42.  
  43.         IF warp < .002 THEN warp = 0
  44.         IF warp > 10 THEN warp = 10
  45.         IF warp > 0 THEN LOCATE 1, 1: PRINT "Warp: "; warp
  46.         IF warp = 0 THEN LOCATE 1, 1: PRINT "Cruise Speed"
  47.         IF speed < .001 THEN speed = .001
  48.         IF speed > .01 THEN speed = .01
  49.  
  50.         'Move the stars.
  51.         x(stars) = x(stars) + dx(stars) * speed
  52.         y(stars) = y(stars) + dy(stars) * speed
  53.  
  54.         'Get a new star if one goes off the screen.
  55.         IF x(stars) < -6400 OR x(stars) > 6400 OR y(stars) < -400 OR y(stars) > 400 THEN GOSUB make:
  56.  
  57.         'Draw the stars, c(stars) is the choice of color.
  58.         IF sz(stars) > 2 THEN sz(stars) = 2
  59.         IF c(stars) < 65 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(255, 255, 255)
  60.         IF c(stars) > 64 AND c(stars) < 85 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(100, 10, 10)
  61.         IF c(stars) > 84 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(10, 10, 200)
  62.  
  63.         'If the size of the star is less than 1, skip paint.
  64.         IF sz(stars) < 1 THEN GOTO nopaint:
  65.  
  66.         'Paint the stars.
  67.         IF c(stars) < 65 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(255, 255, 255)
  68.         IF c(stars) > 64 AND c(stars) < 85 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(100, 10, 10)
  69.         IF c(stars) > 84 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(10, 10, 200)
  70.         nopaint:
  71.     NEXT stars
  72.  
  73.     'This stops flickering.
  74.     _DISPLAY
  75.  
  76.     'Erase the stars for motion but keep a transparent black for the trails.
  77.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF
  78.  
  79. make:
  80.  
  81. 'Get a random X number using 2 decimal points - RND and .5 and times it by the width of the screen.
  82.  
  83. x(stars) = (RND - .5) * _WIDTH
  84.  
  85. 'Get a random Y number using 2 decimal points - RND and .5 and times it by the height of the screen.
  86.  
  87. y(stars) = (RND - .5) * _HEIGHT
  88.  
  89. 'Get a random star size between .25 and 2.
  90.  
  91. sz(stars) = (RND * 2) + .25
  92.  
  93. 'Get a random number between 1 and 100 to pick 1 of 3 different star colors in the main loop.
  94.  
  95. c(stars) = INT(RND * 100) + 1
  96.  
  97. 'Get any random number between 5 and 10, used for both X and Y, to use in the star movement in the main loop.
  98.  
  99. v = 5 + RND * 5
  100. dx(stars) = x(stars) * v
  101. dy(stars) = y(stars) * v
  102.  
  103.  

2. less flickering or blinking of screen   (also with Inkey$ if you move fast to right or to left you got the stars jumping on the screen)
  2.1 an alternative (long?) way to manage output to screen with 2 panel for images
  2.2 an adjust of _limit value
  2.3 an adjust of how to move in the select direction the stars (50 is high, 20 seems better)

Code: QB64: [Select]
  1. 'Thank you to B+ and other guys from the QB64.org forum for their code examples to make this program!
  2. '2019-08-12 TDB mod introducing _keyhit and _Keyclear to a smoother response to keyboard
  3. '2019-08-12 TDB mod introducing the 2 panel tecnique for animation to avoid flickering and loose of images
  4. _TITLE "Warp Speed  - Use Up and Down Arrow Key for speed and Right and Left Arrows to view sides and Space Bar to center."
  5. DIM x(800), y(800), dx(800), dy(800), sz(800), c(800)
  6.  
  7. SCREEN _NEWIMAGE(800, 800, 32)
  8.  
  9. ' another way to avoid flickering is to use 2 panel 1st for screen output and 2nd for drawing
  10. ' so you draw on 2nd and copy to 1st to show
  11. Bpanel& = _NEWIMAGE(800, 800, 32)
  12.  
  13.  
  14. 'Set the default speed, amount of stars, and then make the first stars.
  15. speed = .001
  16. amount = 200
  17. xx = 0
  18. FOR stars = 1 TO amount
  19.     GOSUB make:
  20. NEXT stars
  21.  
  22.     _LIMIT 30 ' <---- it seems enough in respect of 1000
  23.     FOR stars = 1 TO amount
  24.         ' a$ = INKEY$
  25.  
  26.         u = 0 ' a tricky way to reset the variable of input
  27.         u = _KEYHIT
  28.         IF u = 27 THEN END
  29.         IF u = 19200 THEN xx = xx - 20 ' left key
  30.         IF u = 19712 THEN xx = xx + 20 ' right key
  31.         IF u = 32 THEN xx = 0
  32.         ' IF a$ = CHR$(27) THEN END
  33.         ' IF a$ = CHR$(0) + CHR$(77) THEN xx = xx + 50
  34.         ' IF a$ = CHR$(0) + CHR$(75) THEN xx = xx - 50
  35.         ' IF a$ = " " THEN xx = 0
  36.  
  37.         IF xx > 1200 THEN xx = 1200
  38.         IF xx < -1200 THEN xx = -1200
  39.  
  40.         'Detect the arrow keys to make it go faster or slower.
  41.         IF u = 18432 THEN speed = speed + .001: warp = warp + 1 ' up key
  42.         IF u = 20480 THEN speed = speed - .001: warp = warp - 1 ' down key
  43.         REM _KEYCLEAR  '<---- you can get the same effect REMming u = 0 and activating this statement here
  44.  
  45.         'IF a$ = CHR$(0) + CHR$(72) THEN speed = speed + .001: warp = warp + 1
  46.         'IF a$ = CHR$(0) + CHR$(80) THEN speed = speed - .001: warp = warp - 1
  47.  
  48.         IF warp < .002 THEN warp = 0
  49.         IF warp > 10 THEN warp = 10
  50.         'Why do you write on the screen 200 times the same text?
  51.         ' I  move the PRINT instruction in the area of output to screen one time out of for..next
  52.  
  53.         IF speed < .001 THEN speed = .001
  54.         IF speed > .01 THEN speed = .01
  55.  
  56.         'Move the stars.
  57.         x(stars) = x(stars) + dx(stars) * speed
  58.         y(stars) = y(stars) + dy(stars) * speed
  59.  
  60.         'Get a new star if one goes off the screen.
  61.         IF x(stars) < -6400 OR x(stars) > 6400 OR y(stars) < -400 OR y(stars) > 400 THEN GOSUB make:
  62.  
  63.  
  64.         _DEST Bpanel&
  65.         'Draw the stars, c(stars) is the choice of color.
  66.         IF sz(stars) > 2 THEN sz(stars) = 2
  67.         IF c(stars) < 65 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(255, 255, 255)
  68.         IF c(stars) > 64 AND c(stars) < 85 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(100, 10, 10)
  69.         IF c(stars) > 84 THEN CIRCLE ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(10, 10, 200)
  70.  
  71.         'If the size of the star is less than 1, skip paint.
  72.         IF sz(stars) < 1 THEN GOTO nopaint:
  73.  
  74.         'Paint the stars.
  75.         IF c(stars) < 65 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(255, 255, 255)
  76.         IF c(stars) > 64 AND c(stars) < 85 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(100, 10, 10)
  77.         IF c(stars) > 84 THEN PAINT ((x(stars) + _WIDTH / 2) + xx, -y(stars) + _HEIGHT / 2), _RGB32(10, 10, 200)
  78.         nopaint:
  79.     NEXT stars
  80.     ' here print one time info in text format on the screen
  81.     IF warp > 0 THEN LOCATE 1, 1: PRINT "Warp: "; warp
  82.     IF warp = 0 THEN LOCATE 1, 1: PRINT "Cruise Speed"
  83.     _SOURCE Bpanel&: _DEST 0
  84.     _PUTIMAGE
  85.  
  86.     'This stops flickering.
  87.     '   _DISPLAY              <--- the quick method
  88.  
  89.  
  90.     _DEST Bpanel&
  91.     'Erase the stars for motion but keep a transparent black for the trails.
  92.     CLS , _RGBA32(0, 0, 0, 30)
  93.     '    LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF
  94.  
  95. make:
  96.  
  97. 'Get a random X number using 2 decimal points - RND and .5 and times it by the width of the screen.
  98.  
  99. x(stars) = (RND - .5) * _WIDTH
  100.  
  101. 'Get a random Y number using 2 decimal points - RND and .5 and times it by the height of the screen.
  102.  
  103. y(stars) = (RND - .5) * _HEIGHT
  104.  
  105. 'Get a random star size between .25 and 2.
  106.  
  107. sz(stars) = (RND * 2) + .25
  108.  
  109. 'Get a random number between 1 and 100 to pick 1 of 3 different star colors in the main loop.
  110.  
  111. c(stars) = INT(RND * 100) + 1
  112.  
  113. 'Get any random number between 5 and 10, used for both X and Y, to use in the star movement in the main loop.
  114.  
  115. v = 5 + RND * 5
  116. dx(stars) = x(stars) * v
  117. dy(stars) = y(stars) * v
  118.  
  119.  
  120.  

and two glitches...
1 moving to the leftest position or the rightest position you get stars moving in the opposite extreme direction
2 moving fastly to left or to right from initial position you get some various movements of stars
 
sierraKen starfield.jpg


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