Author Topic: Help with Starfield  (Read 11911 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #30 on: August 10, 2019, 11:05:45 pm »
A-HA! I found the reason B+! Those 2 lines just detect to see if any star goes past 600, either vertically or horizontally. It's just a trick to see how far the farthest stars have gone in that 1 direction. And since all of the directions move the same speed and length, only 1 direction is needed. If any of them passed up 600, it would make another new star in make: Knowing this, I changed the 600 to 800 because I had changed the screen size to 800 x 800 when I made the program. And now the stars even look better!!! Here is a better version:
...

True SMcNeill, but it's still only moving right or moving down because if a star went to the left or up, that equation wouldn't work.

Hi Ken,

Ah mathematics! When you square a number negative or positive the square is always positive and gigantic!

800 ^ 2 = 640,000 gigantic AND positive, you were right to increase the number.

The distance between two things is a positive what is a negative distance? there are negative directions though.
« Last Edit: August 10, 2019, 11:07:06 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Help with Starfield
« Reply #31 on: August 10, 2019, 11:09:40 pm »
A-HA! I found the reason B+! Those 2 lines just detect to see if any star goes past 600, either vertically or horizontally. It's just a trick to see how far the farthest stars have gone in that 1 direction. And since all of the directions move the same speed and length, only 1 direction is needed. If any of them passed up 600, it would make another new star in make: Knowing this, I changed the 600 to 800 because I had changed the screen size to 800 x 800 when I made the program. And now the stars even look better!!! Here is a better version:

You're not quite right in your thinking there.

Here's a quick thought process for you to try:  Take a square piece of page and lay it flat on any surface.  Take another piece of paper the same size and hold it vertical at the edge of the first one.  Now, will another sheet, the same size, fit from the far edges of both sides?

If you have your paper like so (pardon the rough ASCII art):

|
|
|______


Would the same size paper fit to make that triangle there?

Easy answer:  It wouldn't.

To fit, that sheet of paper would have to be SQR(left side ^ 2 + bottom side ^ 2)...

Or, simplified SQR(2) in this case, times as long as the other sides. 

SQR (1 page size ^ 2 + 1 page size ^ 2)=
SQR (1 page size + 1 page size)=    *After all, 1 ^ 2 is still 1...
SQR (2 page size)



And how does this apply to your math in particular?

The center of your screen is at 400,400, so it's 400 pixels from the top and 400 pixels from the left.  On the perfect diagonal, the distance from (400,400) to (0,0) makes one of those triangles once again, just as I was pointing out with the paper example above, so it's SQR(2) * 400 pixels in length...

566 pixels from the center, in any direction, will place you off the edge of the screen.



If it's only 566 pixels to the edge, why did Stx use 600 as his limit??

Same reason most programmers do such things; he knew it was off the screen with a little mental math, and didn't need to be bothered to figure out the exact distance for things to work properly for his code.  He just plugged in a simple value "close enough", and then moved on and didn't worry about the fine details...

       IF (r > 600 ^ 2) THEN GOSUB make: 

The above line would work just as well if the value was IF (r > 566 ^ 2) THEN GOSUB make: 

Making it 800 doesn't make the program any better.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Help with Starfield
« Reply #32 on: August 10, 2019, 11:17:05 pm »
800 ^ 2 = 640,000 gigantic AND positive, you were right to increase the number.

Except your not dealing with 800 pixels in every direction -- that's the total _WIDTH and _HEIGHT of the screen.  The centerpoint is half that, at 400,400, so you only need the distance of the diagonal from (400,400) to (0,0), which is 566 pixels. 

The distance between two things is a positive what is a negative distance? there are negative directions though.

I agree with this, 100%.  Distance is a set value zero or greater.  Positive or Negative simply tells you which direction that distance is in relation to your origin.

A.....B
The distance between A and B above is 5 dots.  Since A is the origin, it's positive 5 dots.

B.....A
And now, the distance between A and B is still 5 dots.  But, since A is the origin, it's negative 5 dots.

Positive/Negative just tells you the direction to move in from the origin.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #33 on: August 10, 2019, 11:55:59 pm »
Except your not dealing with 800 pixels in every direction -- that's the total _WIDTH and _HEIGHT of the screen.  The centerpoint is half that, at 400,400, so you only need the distance of the diagonal from (400,400) to (0,0), which is 566 pixels. 

I agree with this, 100%.  Distance is a set value zero or greater.  Positive or Negative simply tells you which direction that distance is in relation to your origin.

A.....B
The distance between A and B above is 5 dots.  Since A is the origin, it's positive 5 dots.

B.....A
And now, the distance between A and B is still 5 dots.  But, since A is the origin, it's negative 5 dots.

Positive/Negative just tells you the direction to move in from the origin.  ;)

OK, I was maybe thrown by STxAxTIC's method of locating stars... 400^2 + 400^2 is probably right for 800x800 screen, I was thinking 800 x 800 was bigger than screen 12 in area.

For the record, here is my conditions for making a new star which avoids squaring (and SQRing for proper distance)
Code: QB64: [Select]
  1.         IF y(p) < -2 OR y(p) > _HEIGHT + 2 THEN newStar p 'replace stars going off screen with new ones
  2.         IF x(p) < -2 OR x(p) > _WIDTH + 2 THEN newStar p
  3.  
But I am not locating things like STxAxTIC

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #34 on: August 11, 2019, 12:03:30 am »
SmMcNeill, I still don't understand this. Why would 400 ^ 2 + 400 ^2 = 320,000 matter to anything I'm doing? I did take Geometry in High School back in the 80's, but this does not sound right to me. _WIDTH of the ENTIRE screen is only 800, so this circle could never fit anywhere near the size of my screen. Unless that's what the programmer wanted? A circle that's 800 times the width of the screen? 

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Help with Starfield
« Reply #35 on: August 11, 2019, 12:04:34 am »
Warp Drive in reverse:

Code: QB64: [Select]
  1. TYPE Dot_type
  2.     x AS _FLOAT
  3.     y AS _FLOAT
  4.     ox AS _FLOAT
  5.     oy AS _FLOAT
  6.     speed AS _FLOAT
  7. DIM dots(10000) AS Dot_type
  8.  
  9.  
  10. SCREEN _NEWIMAGE(501, 501, 32)
  11. WINDOW (-250, -250)-(250, 250)
  12.  
  13. dotsinplay = 0
  14.  
  15. FOR i = -250 TO 250
  16.     dotsinplay = dotsinplay + 1
  17.     dots(dotsinplay).x = i
  18.     dots(dotsinplay).y = 250
  19.     dots(dotsinplay).ox = dots(dotsinplay).x
  20.     dots(dotsinplay).oy = dots(dotsinplay).y
  21.     dots(dotsinplay).color = &HFF000001 + INT(RND * &HFFFFFF)
  22.     dots(dotsinplay).speed = RND * 200
  23.     dotsinplay = dotsinplay + 1
  24.     dots(dotsinplay).x = i
  25.     dots(dotsinplay).y = -250
  26.     dots(dotsinplay).ox = dots(dotsinplay).x
  27.     dots(dotsinplay).oy = dots(dotsinplay).y
  28.     dots(dotsinplay).color = &HFF000001 + INT(RND * &HFFFFFF)
  29.     dots(dotsinplay).speed = RND * 200
  30.     dotsinplay = dotsinplay + 1
  31.     dots(dotsinplay).x = 250
  32.     dots(dotsinplay).y = i
  33.     dots(dotsinplay).ox = dots(dotsinplay).x
  34.     dots(dotsinplay).oy = dots(dotsinplay).y
  35.     dots(dotsinplay).color = &HFF000001 + INT(RND * &HFFFFFF)
  36.     dots(dotsinplay).speed = RND * 200
  37.     dotsinplay = dotsinplay + 1
  38.     dots(dotsinplay).x = -250
  39.     dots(dotsinplay).y = i
  40.     dots(dotsinplay).ox = dots(dotsinplay).x
  41.     dots(dotsinplay).oy = dots(dotsinplay).y
  42.     dots(dotsinplay).color = &HFF000001 + INT(RND * &HFFFFFF)
  43.     dots(dotsinplay).speed = RND * 200
  44.  
  45.  
  46.     _LIMIT 100
  47.     LINE (-250, -250)-(250, 250), &H77000000, BF
  48.     'CLS
  49.     FOR i = 1 TO dotsinplay
  50.         PSET (dots(i).x, dots(i).y), dots(i).color
  51.         dots(i).x = dots(i).x - dots(i).ox / dots(i).speed
  52.         dots(i).y = dots(i).y - dots(i).oy / dots(i).speed
  53.         IF SGN(dots(i).ox) * dots(i).x < 0 OR SGN(dots(i).oy) * dots(i).y < 0 THEN
  54.             dots(i).x = dots(i).ox
  55.             dots(i).y = dots(i).oy
  56.         END IF
  57.     NEXT
  58.     _DISPLAY
  59.  

Or maybe, if I was to add a small rotation to the screen as it goes, I could call it a Swirl Into Blackhole.   

Maybe it's just me, but I actually like this effect quite a bit.  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #36 on: August 11, 2019, 12:23:20 am »
SmMcNeill, I still don't understand this. Why would 400 ^ 2 + 400 ^2 = 320,000 matter to anything I'm doing? I did take Geometry in High School back in the 80's, but this does not sound right to me. _WIDTH of the ENTIRE screen is only 800, so this circle could never fit anywhere near the size of my screen. Unless that's what the programmer wanted? A circle that's 800 times the width of the screen? 

You address Steve, but I'd like to take a shot at answering because I am a bit confused myself.

The farthest points on the screen from the center are in the corners, what is the distance from center to corner?
x distance = 400, y distance = 400 so it is the SQR(400 ^ 2 + 400 ^ 2) ~ 566

but we are leaving the x and y squared, so we leave the distance squared to compare apples to apples and avoid using SQR function.




Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #37 on: August 11, 2019, 12:25:11 am »
LOL Steve, your warp drive in reverse looks like a vacuum cleaner, quick sand?

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #38 on: August 11, 2019, 12:29:20 am »
Hmm OK then yes, maybe the programmer did want a GIANT circle 800 times larger than the computer screen. I think because I don't understand this that much, I'm going to change it to use B+'s _WIDTH and _HEIGHT limitation example a couple posts before this instead. :) Thanks!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Help with Starfield
« Reply #39 on: August 11, 2019, 12:30:20 am »
SmMcNeill, I still don't understand this. Why would 400 ^ 2 + 400 ^2 = 320,000 matter to anything I'm doing? I did take Geometry in High School back in the 80's, but this does not sound right to me. _WIDTH of the ENTIRE screen is only 800, so this circle could never fit anywhere near the size of my screen. Unless that's what the programmer wanted? A circle that's 800 times the width of the screen?

Give this demo a run and see if it explains the concept better than I ever could without much better pictures than ASCII art allows in a text forum:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 800, 32)
  2.  
  3.     count = count + 1
  4.     CLS
  5.     CIRCLE (400, 400), count, -1
  6.     PAINT (400, 400), -1
  7.     _DISPLAY
  8.     _TITLE STR$(count)
  9.     _LIMIT 40
  10.     IF count = 400 THEN
  11.         BEEP
  12.         PRINT "Notice that with a size of 400, we've now touched the midpoints of the edges if the screen?"
  13.         _DISPLAY
  14.         SLEEP
  15.         _KEYCLEAR
  16.  
  17.     END IF
  18.     IF count = 566 THEN
  19.         COLOR &HFF000000, 0
  20.         BEEP
  21.         PRINT "We're now at 566...  Notice how we've now covered the whole screen, completely?"
  22.         _DISPLAY
  23.         SLEEP
  24.         _KEYCLEAR
  25.         EXIT DO
  26.     END IF
  27.  
  28. PRINT "Now, at this point, let's do a little math."
  29. PRINT "Pythogoren's Theorum says:   A ^ 2 + B ^ 2 = C ^ 2"
  30. PRINT "In this case, it's 400 ^ 2 + 400 ^ 2 = C ^ 2"
  31. PRINT "Or 160000 + 160000 = C ^ 2"
  32. PRINT "Which turns into 320000 = C ^ 2"
  33. PRINT "And simplified, becomes SQR(320000) = C"
  34. PRINT "Which, oddly enough, gives us:"; SQR(320000)
  35. PRINT "So it rounds up to 566, since we can't process a fraction of a loop..."
  36. PRINT "And that's where the 400 ^ 2 + 400 ^2 being less than 600 ^2 comes from..."
  37. PRINT "That 600 ^ 2 is just lazy math and a fast guesstimate, which is definitely off the screen."
  38. PRINT "Because  if 566 covers the whole screen, 600 certainly isn't going to have any issues covering it as well."
  39.  
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #40 on: August 11, 2019, 12:43:23 am »
Hmm OK then yes, maybe the programmer did want a GIANT circle 800 times larger than the computer screen. I think because I don't understand this that much, I'm going to change it to use B+'s _WIDTH and _HEIGHT limitation example a couple posts before this instead. :) Thanks!

Ken be careful, my limits don't work the same for this setup:
Code: QB64: [Select]
  1. x(stars) = (RND - .5) * _WIDTH
  2.  
  3. 'Get a random Y number using 2 decimal points - RND and .5 and times it by the height of the screen.
  4.  
  5. y(stars) = (RND - .5) * _HEIGHT
  6.  
  7.  

These gives x and y a range of -400 to 400.

So check if  x < - 400 or x > 400 then make newStar or use _width/2 *-1 and _width/2
likewise for y  if y < -400 or y > 400 then make newStar
that should be something we all can understand.

BTW, I don't understand your comment about 2 decimal points.
RND -.5 takes a number from 0 to 1 and makes it a number from -.5 to .5
multiply that by width or height of 800 and you get a number between -400 and 400.

« Last Edit: August 11, 2019, 12:53:06 am by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Help with Starfield
« Reply #41 on: August 11, 2019, 12:51:28 am »
Wow yes! The negative numbers! That's what SmMcNeil was referring to, making the 0,0 in the middle of the screen. Makes a lttle more sense now to me, although it's too late tonight to do the geometry again lol.  I totally overlooked that. So I changed it to the limits of -400 and 400 on both X and Y now and it works great! I wondered why it was only in 1 corner. LOL
Thanks again, wow what a learning day for me. :)

Here it is again:

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 = 750
  11. FOR stars = 1 TO amount
  12.     GOSUB make:
  13. NEXT stars
  14.  
  15.     _LIMIT 500
  16.     FOR stars = 1 TO amount
  17.         a$ = INKEY$
  18.         IF a$ = CHR$(27) THEN END
  19.  
  20.         'Detect the arrow keys to make it go faster or slower.
  21.         IF a$ = CHR$(0) + CHR$(72) THEN speed = speed + .001: warp = warp + 1
  22.         IF a$ = CHR$(0) + CHR$(80) THEN speed = speed - .001: warp = warp - 1
  23.         IF warp < .0003 THEN warp = 0
  24.         IF warp > 10 THEN warp = 10
  25.         IF warp > 0 THEN LOCATE 1, 1: PRINT "Warp: "; warp
  26.         IF warp = 0 THEN LOCATE 1, 1: PRINT "Cruise Speed"
  27.         IF speed < .001 THEN speed = .001
  28.         IF speed > .01 THEN speed = .01
  29.  
  30.         'Move the stars.
  31.         x(stars) = x(stars) + dx(stars) * speed
  32.         y(stars) = y(stars) + dy(stars) * speed
  33.  
  34.         'Get a new star if one goes off the screen.
  35.         IF x(stars) < -400 OR x(stars) > 400 OR y(stars) < -400 OR y(stars) > 400 THEN GOSUB make:
  36.  
  37.         'r = x(stars) * x(stars) + y(stars) * y(stars)
  38.         'IF (r > 600 ^ 2) THEN GOSUB make:
  39.  
  40.         'Draw the stars, c(stars) is the choice of color.
  41.         IF c(stars) < 75 THEN CIRCLE (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(255, 255, 255)
  42.         IF c(stars) > 74 AND c(stars) < 85 THEN CIRCLE (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(100, 10, 10)
  43.         IF c(stars) > 84 THEN CIRCLE (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), sz(stars), _RGB32(10, 10, 200)
  44.  
  45.         'If the size of the star is less than 1, skip paint.
  46.         IF sz(stars) < 1 THEN GOTO nopaint:
  47.  
  48.         'Paint the stars.
  49.         IF c(stars) < 75 THEN PAINT (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), _RGB32(255, 255, 255)
  50.         IF c(stars) > 74 AND c(stars) < 85 THEN PAINT (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), _RGB32(100, 10, 10)
  51.         IF c(stars) > 84 THEN PAINT (x(stars) + _WIDTH / 2, -y(stars) + _HEIGHT / 2), _RGB32(10, 10, 200)
  52.         nopaint:
  53.     NEXT stars
  54.  
  55.     'This stops flickering.
  56.     _DISPLAY
  57.  
  58.     'Erase the stars for motion but keep a transparent black for the trails.
  59.     LINE (0, 0)-(_WIDTH, _HEIGHT), _RGBA32(0, 0, 0, 30), BF
  60.  
  61.  
  62. make:
  63.  
  64. 'Get a random X number using 2 decimal points - RND and .5 and times it by the width of the screen.
  65.  
  66. x(stars) = (RND - .5) * _WIDTH
  67.  
  68. 'Get a random Y number using 2 decimal points - RND and .5 and times it by the height of the screen.
  69.  
  70. y(stars) = (RND - .5) * _HEIGHT
  71.  
  72. 'Get a random star size between .25 and 2.
  73.  
  74. sz(stars) = (RND * 2) + .25
  75.  
  76. 'Get a random number between 1 and 100 to pick 1 of 3 different star colors in the main loop.
  77.  
  78. c(stars) = INT(RND * 100) + 1
  79.  
  80. 'Get any random number between 5 and 10, used for both X and Y, to use in the star movement in the main loop.
  81.  
  82. v = 5 + RND * 5
  83. dx(stars) = x(stars) * v
  84. dy(stars) = y(stars) * v
  85.  
  86.  
« Last Edit: August 11, 2019, 12:55:24 am by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Help with Starfield
« Reply #42 on: August 11, 2019, 12:59:30 am »
Yep the change looks right to me, good night!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Help with Starfield
« Reply #43 on: August 11, 2019, 04:51:43 am »
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
Logic is the beginning of wisdom.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Help with Starfield
« Reply #44 on: August 11, 2019, 05:40:45 am »
....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....

SCREEN _NEWIMAGE(640,480,32)
CLS, -1
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!