Author Topic: Spawn 2 variation 1  (Read 2855 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Spawn 2 variation 1
« on: September 10, 2020, 09:47:13 pm »
Meteor shower with more 3D effect:
Code: QB64: [Select]
  1. _TITLE "Spawn 2 variation 1" 'b+ mod 2020-09-10
  2. CONST angle = _PI(3 / 4)
  3. TYPE Meteor
  4.     X AS INTEGER
  5.     Y AS INTEGER
  6.     dx AS SINGLE
  7.     dy AS SINGLE
  8.     mag AS SINGLE ' ah ha sizes
  9.  
  10. SCREEN _NEWIMAGE(1200, 720, 32)
  11. _DELAY .25 'allow screen to load
  12. Stars& = _LOADIMAGE("stars.png")
  13. M& = _LOADIMAGE("meteor.png")
  14.  
  15. ' filter out dark space
  16. newM& = _NEWIMAGE(_WIDTH(M&), _HEIGHT(M&), 32)
  17. _DEST newM&
  18. FOR y = 0 TO _HEIGHT(M&)
  19.     FOR x = 0 TO _WIDTH(M&)
  20.         p = POINT(x, y)
  21.         pr = _RED32(p)
  22.         pg = _GREEN32(p)
  23.         pb = _BLUE32(p)
  24.         IF pr > 25 AND pg > 25 AND pb > 30 THEN PSET (x, y), p ELSE PSET (x, y), &H00000000
  25.     NEXT
  26.  
  27. REDIM SHARED spawn(0) AS Meteor, rock AS Meteor
  28. FOR i = 1 TO 100 'make a rock and then load it into spawn array with smallest listed first and biggest last
  29.     rock.X = .5 * _WIDTH + 1000 * RND
  30.     rock.Y = -1000 * RND - 100
  31.     rock.mag = RND * 2 + .2
  32.     r = (RND * 10 + 2)
  33.     rock.dx = rock.mag * r * COS(angle)
  34.     rock.dy = rock.mag * r * SIN(angle)
  35.  
  36.     'load rock from smallest to largest so when display the bigger ones will be drawn last and be "up front"
  37.     loadSort rock, spawn()
  38.  
  39.     _PUTIMAGE , Stars&, 0
  40.     FOR i = 1 TO 100
  41.         spawn(i).X = spawn(i).X + spawn(i).dx
  42.         spawn(i).Y = spawn(i).Y + spawn(i).dy
  43.         IF spawn(i).X < -200 OR spawn(i).Y > _HEIGHT + 150 THEN reincarnateSpawn i
  44.         _PUTIMAGE (spawn(i).X, spawn(i).Y)-STEP(spawn(i).mag * _WIDTH(newM&), spawn(i).mag * _HEIGHT(newM&)), newM&, 0
  45.     NEXT
  46.     _DISPLAY
  47.     _LIMIT 60
  48. LOOP UNTIL _KEYDOWN(27) 'escape
  49.  
  50. SUB reincarnateSpawn (i)
  51.     spawn(i).X = .5 * _WIDTH + 1000 * RND
  52.     spawn(i).Y = -1000 * RND - 100
  53.  
  54. SUB loadSort (insertN AS Meteor, dynArr() AS Meteor) '  mod for meteor
  55.     'note this leaves dynArr(0) empty! so ubound of array is also number of items in list
  56.     DIM ub, j, k
  57.     ub = UBOUND(dynArr) + 1
  58.     REDIM _PRESERVE dynArr(LBOUND(dynArr) TO ub) AS Meteor
  59.     FOR j = 1 TO ub - 1
  60.         IF insertN.mag < dynArr(j).mag THEN '  GT to LT according to descending or ascending sort
  61.             FOR k = ub TO j + 1 STEP -1
  62.                 dynArr(k) = dynArr(k - 1)
  63.             NEXT
  64.             EXIT FOR
  65.         END IF
  66.     NEXT
  67.     dynArr(j) = insertN
  68.  

 
spawn 2.PNG


EDIT: Oh, ha, read my notes on LoadSort and reminded the ubound IS the actual number of Items in the array. The array(0) is empty. So one more meteor now showing, just replace this code with bas file in zip which only showed 99% of the meteors ;-))
« Last Edit: September 10, 2020, 10:02:17 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Spawn 2 variation 1
« Reply #1 on: September 11, 2020, 10:49:22 am »
Oh dang! Forgot the zip, here it is!
* Spawn 2 var 1.zip (Filesize: 760.83 KB, Downloads: 157)
« Last Edit: September 11, 2020, 10:53:21 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Spawn 2 variation 1
« Reply #2 on: September 13, 2020, 12:02:53 am »
Another variation com'in at you!



* Aster.zip (Filesize: 33.52 KB, Downloads: 160)

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: Spawn 2 variation 1
« Reply #3 on: September 13, 2020, 06:32:21 am »
Hey Bplus
Let me say that your creativity is wonderful!
I'm doping myself with caffeine... so you?  :-)
I find wonderful also the transpareny of images  versus background!
Thanks for these variations of main theme!

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

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: Spawn 2 variation 1
« Reply #4 on: September 13, 2020, 06:36:08 am »
Moreover Bplus
your fantastic demo of traveling in 3D is very enjoyable!
If you revert the direction  CoderVSB12  have the work already done!
Waiting your version of Blaster!  ;-)
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Spawn 2 variation 1
« Reply #5 on: September 13, 2020, 11:09:14 am »
Moreover Bplus
your fantastic demo of traveling in 3D is very enjoyable!
If you revert the direction  CoderVSB12  have the work already done!
Waiting your version of Blaster!  ;-)

Thanks @TempodiBasic

Quote
If you revert the direction

Wow, I thought that was the required direction!   need more coffee :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Spawn 2 variation 1
« Reply #6 on: September 13, 2020, 11:21:26 am »
OK this time I forgot the code to Aster, short for Asteroids  (image in zip package Reply #2)

Code: QB64: [Select]
  1. _TITLE "Astera" 'b+ mod 2020-09-12
  2. CONST nRocks = 100
  3. TYPE rock
  4.     X AS SINGLE
  5.     Y AS SINGLE
  6.     Z AS SINGLE
  7.     dx AS SINGLE
  8.     dy AS SINGLE
  9.     dz AS SINGLE 'mag
  10.  
  11. SCREEN _NEWIMAGE(1200, 720, 32)
  12. _DELAY .25 'allow screen to load
  13. M& = _LOADIMAGE("aster.png")
  14.  
  15. ' filter out dark space
  16. newM& = _NEWIMAGE(_WIDTH(M&), _HEIGHT(M&), 32)
  17. _DEST newM&
  18. FOR y = 0 TO _HEIGHT(M&)
  19.     FOR x = 0 TO _WIDTH(M&)
  20.         p = POINT(x, y)
  21.         pr = _RED32(p)
  22.         pg = _GREEN32(p)
  23.         pb = _BLUE32(p)
  24.         IF _HYPOT(x - _WIDTH(M&) / 2, y - _HEIGHT(M&) / 2) > _WIDTH / 2 * .7 THEN
  25.             IF pr > 35 AND pg > 35 AND pb > 35 THEN PSET (x, y), p ELSE PSET (x, y), &H00000000
  26.         ELSE
  27.             PSET (x, y), p
  28.         END IF
  29.     NEXT
  30.  
  31. DIM SHARED r(nRocks) AS rock
  32. FOR i = 0 TO nRocks
  33.     new i
  34.     CLS
  35.     'FOR y = 0 TO _HEIGHT    ' optional gray static background that really slows thing down
  36.     '    FOR x = 0 TO _WIDTH
  37.     '        r = RND * 55
  38.     '        LINE (x, y)-STEP(0, 0), _RGB32(r, r, r, RND * 50 + 150)
  39.     '    NEXT
  40.     'NEXT
  41.     FOR i = 0 TO nRocks
  42.         r(i).dx = r(i).dx * 1.05
  43.         r(i).dy = r(i).dy * 1.05
  44.         r(i).X = r(i).X + r(i).dx
  45.         r(i).Y = r(i).Y + r(i).dy
  46.         r(i).Z = r(i).Z + .2
  47.         IF r(i).X < -200 OR r(i).X > _WIDTH OR r(i).Y > _HEIGHT OR r(i).Y < -200 THEN new i
  48.     NEXT
  49.     QSort 0, nRocks
  50.     FOR i = 0 TO nRocks
  51.         _PUTIMAGE (r(i).X - .5 * r(i).Z / 10 * _WIDTH(newM&), r(i).Y - .5 * r(i).Z / 10 * _HEIGHT(newM&))-STEP(r(i).Z / 10 * _WIDTH(newM&), r(i).Z / 10 * _HEIGHT(newM&)), newM&, 0
  52.     NEXT
  53.     _DISPLAY
  54.     _LIMIT 40
  55. LOOP UNTIL _KEYDOWN(27) 'escape
  56.  
  57. SUB new (i)
  58.     r(i).X = _WIDTH * RND
  59.     r(i).Y = _HEIGHT * RND
  60.     r(i).Z = 1
  61.     angle = _ATAN2(r(i).Y - _HEIGHT / 2, r(i).X - _WIDTH / 2)
  62.     d = _HYPOT(r(i).Y - _HEIGHT / 2, r(i).X - _WIDTH / 2)
  63.     r(i).dx = d / 100 * COS(angle)
  64.     r(i).dy = d / 100 * SIN(angle)
  65.  
  66. SUB QSort (Start, Finish) 'sa$ needs to be   DIM SHARED !!!!     array
  67.     DIM i AS INTEGER, j AS INTEGER, x$, a$
  68.     i = Start
  69.     j = Finish
  70.     x = r(INT((i + j) / 2)).Z
  71.     WHILE i <= j
  72.         WHILE r(i).Z < x
  73.             i = i + 1
  74.         WEND
  75.         WHILE r(j).Z > x
  76.             j = j - 1
  77.         WEND
  78.         IF i <= j THEN
  79.             SWAP r(i), r(j)
  80.             i = i + 1
  81.             j = j - 1
  82.         END IF
  83.     WEND
  84.     IF j > Start THEN QSort Start, j
  85.     IF i < Finish THEN QSort i, Finish
  86.  

Changes from first code:
1) Well the direction first of all, this is exactly like moving through Starfield.
2) Image when I removed all dark colors I lost the black in craters of asteroid so I restricted transparent color setting to a radius past some percentage of width of image from center of image. Cool asteroid right?
3) Then used Qsort to get the sizes arranged to draw smallest first because they are in background.

Never did use dz ;-)) could say it was .2 for all because that was added to r(i).Z every loop until it went off screen.

« Last Edit: September 13, 2020, 11:43:20 am by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: Spawn 2 variation 1
« Reply #7 on: September 13, 2020, 06:10:06 pm »
Good! New features... but now in black and white the asteroid image is more similar to a skull!
Yes you don't use dz but you already have not changed the direction of  asteroids.
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: Spawn 2 variation 1
« Reply #8 on: September 13, 2020, 08:08:19 pm »
Ok Bplus I understand that you are too busy... so I make this mod of your opera

Code: QB64: [Select]
  1. _TITLE "Astera" 'b+ mod 2020-09-12 ' TdB mod 2020-09-14
  2. CONST nRocks = 5 '<--- better less meteors
  3. TYPE rock
  4.     X AS SINGLE
  5.     Y AS SINGLE
  6.     Z AS SINGLE
  7.     dx AS SINGLE
  8.     dy AS SINGLE
  9.     dz AS SINGLE 'mag
  10.  
  11. SCREEN _NEWIMAGE(1200, 720, 32)
  12. _DELAY .25 'allow screen to load
  13. M& = _LOADIMAGE("aster.png")
  14.  
  15. ' filter out dark space
  16. newM& = _NEWIMAGE(_WIDTH(M&), _HEIGHT(M&), 32)
  17. _DEST newM&
  18. FOR y = 0 TO _HEIGHT(M&)
  19.     FOR x = 0 TO _WIDTH(M&)
  20.         p = POINT(x, y)
  21.         pr = _RED32(p)
  22.         pg = _GREEN32(p)
  23.         pb = _BLUE32(p)
  24.         IF _HYPOT(x - _WIDTH(M&) / 2, y - _HEIGHT(M&) / 2) > _WIDTH / 2 * .7 THEN
  25.             IF pr > 35 AND pg > 35 AND pb > 35 THEN PSET (x, y), p ELSE PSET (x, y), &H00000000
  26.         ELSE
  27.             PSET (x, y), p
  28.         END IF
  29.     NEXT
  30.  
  31. DIM SHARED r(nRocks) AS rock
  32. FOR i = 0 TO nRocks
  33.     new i
  34.     CLS
  35.     'FOR y = 0 TO _HEIGHT
  36.     '    FOR x = 0 TO _WIDTH
  37.     '        r = RND * 55
  38.     '        LINE (x, y)-STEP(0, 0), _RGB32(r, r, r, RND * 50 + 150)
  39.     '    NEXT
  40.     'NEXT
  41.     FOR i = 0 TO nRocks
  42.         r(i).dx = r(i).dx * .80 '<--- we reduce acceleration X as far is the meteor
  43.         r(i).dy = r(i).dy * .80 '<--- we reduce acceleration Y as far is the meteor
  44.         r(i).X = r(i).X + r(i).dx
  45.         r(i).Y = r(i).Y + r(i).dy
  46.         r(i).Z = r(i).Z - .2 ' <--- we go away from camera/point of view of user
  47.  
  48.         ' |
  49.         ' V and we must regenerate the meteor if it is too far from our point of view
  50.         IF r(i).X < -200 OR r(i).X > _WIDTH OR r(i).Y > _HEIGHT OR r(i).Y < -200 OR r(i).Z < 0.000001 THEN new i
  51.     NEXT
  52.     QSort 0, nRocks
  53.     FOR i = 0 TO nRocks
  54.         _PUTIMAGE (r(i).X - .5 * r(i).Z / 10 * _WIDTH(newM&), r(i).Y - .5 * r(i).Z / 10 * _HEIGHT(newM&))-STEP(r(i).Z / 10 * _WIDTH(newM&), r(i).Z / 10 * _HEIGHT(newM&)), newM&, 0
  55.     NEXT
  56.     _DISPLAY
  57.     _LIMIT 40
  58. LOOP UNTIL _KEYDOWN(27) 'escape
  59.  
  60. SUB new (i)
  61.     r(i).X = _WIDTH * RND
  62.     r(i).Y = _HEIGHT * RND
  63.     r(i).Z = 50 '<--- our meteor is next to our point of view
  64.     angle = _ATAN2(r(i).Y - _HEIGHT / 2, r(i).X - _WIDTH / 2)
  65.     d = _HYPOT(r(i).Y - _HEIGHT / 2, r(i).X - _WIDTH / 2)
  66.     r(i).dx = d / 100 * COS(angle)
  67.     r(i).dy = d / 100 * SIN(angle)
  68.  
  69. SUB QSort (Start, Finish) 'sa$ needs to be   DIM SHARED !!!!     array
  70.     DIM i AS INTEGER, j AS INTEGER, x$, a$
  71.     i = Start
  72.     j = Finish
  73.     x = r(INT((i + j) / 2)).Z
  74.     WHILE i <= j
  75.         WHILE r(i).Z < x
  76.             i = i + 1
  77.         WEND
  78.         WHILE r(j).Z > x
  79.             j = j - 1
  80.         WEND
  81.         IF i <= j THEN
  82.             SWAP r(i), r(j)
  83.             i = i + 1
  84.             j = j - 1
  85.         END IF
  86.     WEND
  87.     IF j > Start THEN QSort Start, j
  88.     IF i < Finish THEN QSort i, Finish
  89.  
it uses the same image of course ... so the skull is always there
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Spawn 2 variation 1
« Reply #9 on: September 13, 2020, 09:51:17 pm »
@TempodiBasic Well you really did reverse the direction. But I swear in video I saw, they were flying into the shooting zone! ??

That image is something isn't it? ! Your close ups make it all the more amazing to me.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Spawn 2 variation 1
« Reply #10 on: September 13, 2020, 10:20:44 pm »
@TempodiBasic  OHHH!!! you are looking at the bullets shooting out from the user location, right?

They start big and shrink down to horizon, I was thinking of enemy or asteroid fields you are flying into where you meet up with objects to shoot at.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: Spawn 2 variation 1
« Reply #11 on: September 14, 2020, 02:52:38 pm »
Your thought is right, also now
the request was this https://www.qb64.org/forum/index.php?topic=3007.msg122699#msg122699
the more adjustment  of the algorythm will be coded by the main interested coder...
Programming isn't difficult, only it's  consuming time and coffee