Author Topic: Terrain Grid  (Read 5039 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Terrain Grid
« on: March 16, 2022, 09:29:06 pm »
Using Petr's Sound Visualization code, I converted it to this small simulation of flying over a green terrain grid and a blue sky. I might look into this more sometime to see if there is ways to turn, etc. But here is the general code for it. I started this new thread so it wouldn't take over Petr's and in case anyone wants to make mods of it. 
Petr's Music Visualization code is here: https://qb64forum.alephc.xyz/index.php?topic=4719.0

Code: QB64: [Select]
  1. 'Almost all of this code comes from Petr's Music Visualizer.
  2. Screen _NewImage(800, 600, 32)
  3.     ShiftIt = 89
  4.     $NoPrefix
  5.     Dest Texture
  6.     Cls
  7.     Paint (1, 1), _RGB32(127, 255, 127)
  8.     For diY = 0 To 100
  9.         Line (diY + 20, diY + 20)-(235 - diY, 235 - diY), RGB32(0, diY, 0), B
  10.     Next
  11.     Dest 0
  12.     Dim M3D(90, 90) As Single
  13.     Dim M2D(90, 90) As Single
  14.     DW = 800
  15.     DH = 600
  16.     HT = CopyImage(Texture, 33)
  17.     If Texture <> 0 Then FreeImage Texture
  18.     Screen NewImage(DW, DH, 32)
  19.     Do Until ShiftIt < 0
  20.         For rows = 0 To 90
  21.             Swap M3D(rows, ShiftIt), M3D(rows, ShiftIt + 1)
  22.             Swap M2D(rows, ShiftIt), M2D(rows, ShiftIt + 1)
  23.         Next
  24.         ShiftIt = ShiftIt - 1
  25.     Loop
  26.     Depth = 0
  27.     Do Until Depth = 90000
  28.         M3D(ra, 0) = Rnd * 2.5
  29.         M2D(ra, 0) = Rnd * 2.5
  30.         Depth = Depth + 1000
  31.         ra = ra + 1
  32.     Loop
  33.  
  34.     ra = 0
  35.  
  36.     For Z = 0 To 89
  37.         For X = 0 To 89
  38.             HX = -45 + X
  39.             HX2 = HX + 1
  40.             HZ = -89 + Z
  41.             HZ2 = HZ + 1
  42.             HY1 = -3 + M3D(X, Z): HY2 = -3 + M3D(X + 1, Z): HY3 = -3 + M3D(X, Z + 1): HY4 = -3 + M3D(X + 1, Z + 1)
  43.             HY11 = 3 - M2D(X, Z): HY12 = 3 - M2D(X + 1, Z): HY13 = 3 - M2D(X, Z + 1): HY14 = 3 - M2D(X + 1, Z + 1)
  44.             'Ground
  45.             MapTriangle (0, 0)-(255, 0)-(0, 255), HT To(HX, HY1, HZ)-(HX2, HY2, HZ)-(HX, HY3, HZ2), 0, Smooth
  46.             MapTriangle (255, 0)-(0, 255)-(255, 255), HT To(HX2, HY2, HZ)-(HX, HY3, HZ2)-(HX2, HY4, HZ2), 0, Smooth
  47.             'Ceiling
  48.             'MapTriangle (0, 0)-(255, 0)-(0, 255), HT To(HX, HY11, HZ)-(HX2, HY12, HZ)-(HX, HY13, HZ2), 0, Smooth
  49.             'MapTriangle (255, 0)-(0, 255)-(255, 255), HT To(HX2, HY12, HZ)-(HX, HY13, HZ2)-(HX2, HY14, HZ2), 0, Smooth
  50.     Next X, Z
  51.     'Sky
  52.     Paint (1, 1), _RGB32(128, 255, 255)
  53.     Display
  54.     Limit 20
  55.  
  56.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Terrain Grid
« Reply #1 on: March 16, 2022, 10:36:15 pm »
Well that does look pretty cool! Nice Mod!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Terrain Grid
« Reply #2 on: March 16, 2022, 10:40:46 pm »
Thanks B+ :). Thank you also Petr for the code!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Terrain Grid
« Reply #3 on: March 17, 2022, 01:10:10 am »
Here's an improved version. I made the terrain look brighter and thicker and found out how to turn left and right and speed up and slow down, using the arrow keys. I still may improve on it later sometime but here is what I have. Notice that there's no central line going straight down. I was able to remove that when you turn left or right by using xx=xx+.33 and xx=xx-.33 so there's no pattern to the grid on turning which makes it more real looking. 

Code: QB64: [Select]
  1. 'Almost all of this code comes from Petr's Music Visualizer.
  2. Screen _NewImage(800, 600, 32)
  3. _Title "Terrain mod by SierraKen - Use arrow keys"
  4. lim = 20
  5. Do Until a$ = Chr$(27)
  6.     Limit lim
  7.     a$ = InKey$
  8.     If a$ = Chr$(0) + Chr$(75) Then xx = xx + .33
  9.     If a$ = Chr$(0) + Chr$(77) Then xx = xx - .33
  10.     If a$ = Chr$(0) + Chr$(72) Then lim = lim + .1
  11.     If a$ = Chr$(0) + Chr$(80) Then lim = lim - .1
  12.     If lim < 10 Then lim = 10
  13.     If lim > 30 Then lim = 30
  14.     ShiftIt = 89
  15.     $NoPrefix
  16.     Dest Texture
  17.     Cls
  18.     Paint (1, 1), _RGB32(0, 177, 0)
  19.     For diY = 0 To 1000
  20.         Line (diY + 20, diY + 20)-(235 - diY, 235 - diY), RGB32(0, diY, 0), B
  21.     Next
  22.     Dest 0
  23.     Dim M3D(90, 90) As Single
  24.     Dim M2D(90, 90) As Single
  25.     DW = 800
  26.     DH = 600
  27.     HT = CopyImage(Texture, 33)
  28.     If Texture <> 0 Then FreeImage Texture
  29.     Screen NewImage(DW, DH, 32)
  30.     Do Until ShiftIt < 0
  31.         For rows = 0 To 90
  32.             Swap M3D(rows, ShiftIt), M3D(rows, ShiftIt + 1)
  33.             Swap M2D(rows, ShiftIt), M2D(rows, ShiftIt + 1)
  34.         Next
  35.         ShiftIt = ShiftIt - 1
  36.     Loop
  37.     Depth = 0
  38.     Do Until Depth = 90000
  39.         M3D(ra, 0) = Rnd * 2.5
  40.         M2D(ra, 0) = Rnd * 2.5
  41.         Depth = Depth + 1000
  42.         ra = ra + 1
  43.     Loop
  44.  
  45.     ra = 0
  46.     For Z = 0 To 89
  47.         For x = 0 To 89
  48.             HX = -45 + x
  49.             HX2 = HX + 1
  50.             HZ = -89 + Z
  51.             HZ2 = HZ + 1
  52.             HY1 = -3 + M3D(x, Z): HY2 = -3 + M3D(x + 1, Z): HY3 = -3 + M3D(x, Z + 1): HY4 = -3 + M3D(x + 1, Z + 1)
  53.             HY11 = 3 - M2D(x, Z): HY12 = 3 - M2D(x + 1, Z): HY13 = 3 - M2D(x, Z + 1): HY14 = 3 - M2D(x + 1, Z + 1)
  54.             'Ground
  55.             MapTriangle (0 + xx, 0)-(255 + xx, 0)-(0 + xx, 255), HT To(HX + xx, HY1, HZ)-(HX2 + xx, HY2, HZ)-(HX + xx, HY3, HZ2), 0, _Smooth
  56.             MapTriangle (255 + xx, 0)-(0 + xx, 255)-(255 + xx, 255), HT To(HX2 + xx, HY2, HZ)-(HX + xx, HY3, HZ2)-(HX2 + xx, HY4, HZ2), 0, _Smooth
  57.             'Ceiling
  58.             'MapTriangle (0, 0)-(255, 0)-(0, 255), HT To(HX, HY11, HZ)-(HX2, HY12, HZ)-(HX, HY13, HZ2), 0, Smooth
  59.             'MapTriangle (255, 0)-(0, 255)-(255, 255), HT To(HX2, HY12, HZ)-(HX, HY13, HZ2)-(HX2, HY14, HZ2), 0, Smooth
  60.     Next x, Z
  61.     'Sky
  62.     Paint (1, 1), _RGB32(128, 255, 255)
  63.     Display
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Terrain Grid
« Reply #4 on: March 17, 2022, 07:12:50 am »
This would be good for Pi Day
 
image_2022-03-17_071240.png

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Terrain Grid
« Reply #5 on: March 17, 2022, 02:31:06 pm »
I fixed the left and right land edges by just making xx start back from its opposite number when it reaches a certain point. Although there is still the edge you see from far away, which I can't seem to fix. I know I have to make a wider land area in order to do this, somehow. I'm still learning. I also changed the up and down arrow keys to go higher or lower like an airplane simulator. I also made it so the terrain is lower the higher you go and the terrain is higher the lower you go. I changed the speed to the + and - keys as well.

Code: QB64: [Select]
  1. 'Almost all of this code comes from Petr's Music Visualizer.
  2. Screen _NewImage(800, 600, 32)
  3. _Title "Terrain mod by SierraKen - Use arrow keys and plus and minus keys for speed."
  4. lim = 20
  5. altitude = 1.9
  6. Do Until a$ = Chr$(27)
  7.     Limit lim
  8.     a$ = InKey$
  9.     If a$ = Chr$(0) + Chr$(75) Then xx = xx + .33
  10.     If a$ = Chr$(0) + Chr$(77) Then xx = xx - .33
  11.     If a$ = Chr$(0) + Chr$(72) Then altitude = altitude + .05: al = al + .1
  12.     If a$ = Chr$(0) + Chr$(80) Then altitude = altitide - .05: al = al - .1
  13.     If altitude > 3 Then altitude = 3
  14.     If altitude < .5 Then altitude = .5
  15.     If al > 0 Then al = 0
  16.     If al < -20 Then al = -20
  17.     If a$ = "+" Then lim = lim + .1
  18.     If a$ = "-" Then lim = lim - .1
  19.     If xx > 25 Then xx = -25
  20.     If xx < -25 Then xx = 25
  21.     If lim < 10 Then lim = 10
  22.     If lim > 30 Then lim = 30
  23.     ShiftIt = 89
  24.     $NoPrefix
  25.     Dest Texture
  26.     Cls
  27.     Paint (1, 1), _RGB32(0, 177, 0)
  28.     For diY = -1000 To 2000
  29.         Line (diY + 20, diY + 20)-(235 - diY, 235 - diY), RGB32(0, diY, 0), B
  30.     Next
  31.     Dest 0
  32.     Dim M3D(300, 300) As Single
  33.     Dim M2D(300, 300) As Single
  34.     DW = 800
  35.     DH = 600
  36.     HT = CopyImage(Texture, 33)
  37.     If Texture <> 0 Then FreeImage Texture
  38.     Screen NewImage(DW, DH, 32)
  39.     Do Until ShiftIt < 0
  40.         For rows = 0 To 90
  41.             Swap M3D(rows, ShiftIt), M3D(rows, ShiftIt + 1)
  42.             Swap M2D(rows, ShiftIt), M2D(rows, ShiftIt + 1)
  43.         Next
  44.         ShiftIt = ShiftIt - 1
  45.     Loop
  46.     Depth = 0
  47.     Do Until Depth = 90000
  48.         M3D(ra, 0) = Rnd * altitude
  49.         M2D(ra, 0) = Rnd * altitude
  50.         Depth = Depth + 1000
  51.         ra = ra + 1
  52.     Loop
  53.     ra = 0
  54.     For Z = 0 To 89
  55.         For x = 0 To 178
  56.             HX = -45 + x
  57.             HX2 = HX + 1
  58.             HZ = -89 + Z
  59.             HZ2 = HZ + 1
  60.             HY1 = -3 + M3D(x, Z): HY2 = -3 + M3D(x + 1, Z): HY3 = -3 + M3D(x, Z + 1): HY4 = -3 + M3D(x + 1, Z + 1)
  61.             HY11 = 3 - M2D(x, Z): HY12 = 3 - M2D(x + 1, Z): HY13 = 3 - M2D(x, Z + 1): HY14 = 3 - M2D(x + 1, Z + 1)
  62.             'Ground
  63.             MapTriangle _Seamless(0 + xx, al)-(255 + xx, al)-(0 + xx, 255 + al), HT To(HX + xx, HY1 + al, HZ)-(HX2 + xx, HY2 + al, HZ)-(HX + xx, HY3 + al, HZ2), 0, _Smooth
  64.             MapTriangle _Seamless(255 + xx, al)-(0 + xx, 255 + al)-(255 + xx, 255 + al), HT To(HX2 + xx, HY2 + al, HZ)-(HX + xx, HY3 + al, HZ2)-(HX2 + xx, HY4 + al, HZ2), 0, _Smooth
  65.  
  66.             'Ceiling
  67.             'MapTriangle (0, 0)-(255, 0)-(0, 255), HT To(HX, HY11, HZ)-(HX2, HY12, HZ)-(HX, HY13, HZ2), 0, Smooth
  68.             'MapTriangle (255, 0)-(0, 255)-(255, 255), HT To(HX2, HY12, HZ)-(HX, HY13, HZ2)-(HX2, HY14, HZ2), 0, Smooth
  69.     Next x, Z
  70.     'Sky
  71.     Paint (1, 1), _RGB32(128, 255, 255)
  72.     Display
  73.  

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Terrain Grid
« Reply #6 on: March 17, 2022, 07:16:54 pm »
Does anyone happen to know an easy way to convert this type of terrain to rounded edges and hills? I've seen it once or twice here on the forum but the code was massive and I didn't want to dig through it. You would think that all it needs is a couple equations or so.