YouTube2Text

Collision for Terrain Generated via Vertex Displacement / Informal Tutorial, Godot 4 Alpha 10 — Transcript

by devmar · 1,564 words · 212 segments · language en · Watch on YouTube

Full transcript

  1. 0:00okay let's jump right into it I'm making
  2. 0:02this video because I said I would make
  3. 0:04it and a few people messaged me about it
  4. 0:07so let's just get this over with and
  5. 0:09move on to more uh fascinating things
  6. 0:12how do you create a collision for a
  7. 0:16Shader generated terrain Inca doll well
  8. 0:19let me show you how to do it again as
  9. 0:21with nearly everything else in Godot
  10. 0:23there are multiple ways to bake a
  11. 0:26collider for height mop terrain you can
  12. 0:29also use the mesh tools and so on but
  13. 0:31this one seems the easiest and indeed
  14. 0:34the fastest and probably the intended
  15. 0:36way to considering the name of the
  16. 0:38resource that we're going to be using
  17. 0:39Okay firstly in the Collision shape node
  18. 0:43you have this property here called shape
  19. 0:45which I'm sure you're well aware of what
  20. 0:47you may have missed about it though is
  21. 0:49that on the list of shapes here you have
  22. 0:52this thing called height map shape 3D
  23. 0:55and so if we go ahead and select it and
  24. 0:58set the width and depth to let's say a
  25. 1:01thousand by a thousand you'll see it
  26. 1:04creates a collision mesh which is
  27. 1:06nothing else but an array of height
  28. 1:08values so if you click on this array
  29. 1:10you'll see that we have all of our
  30. 1:12height values here on this list there's
  31. 1:15one for each vertex on this Collision
  32. 1:17mesh that we've just created so if we
  33. 1:20put a value in the first one let's say
  34. 1:2350 you'll see that in the corner over
  35. 1:25there there is a spike well that is
  36. 1:28exactly the first vertex reaching the
  37. 1:30height of 50. that's what's going on so
  38. 1:33if I put a value into the second one and
  39. 1:36the third one and so on you'll see how
  40. 1:38it's starting to build a Terrain shape
  41. 1:40now this means that if we can somehow
  42. 1:43populate this map data array with height
  43. 1:46map values from our height map image we
  44. 1:49will be able to build the Collision
  45. 1:51shape that will exactly match the
  46. 1:53terrain shape which is what we're here
  47. 1:55to do and we can do it with just a few
  48. 1:57lines of code so let me show you this is
  49. 2:01the script and before we go into it let
  50. 2:03me just explain the setup real quick but
  51. 2:06you can have a much simpler setup if you
  52. 2:08want but I've decided to keep my
  53. 2:10Collision nodes under this parent node
  54. 2:13called terrain collisions because I'm
  55. 2:16spawning and despawning collision chunks
  56. 2:18as the player moves around the map and
  57. 2:20the reason I have it set up like this is
  58. 2:22well if you recall in from my previous
  59. 2:24videos my terrain is infinite so I need
  60. 2:27a way to dynamically create collisions
  61. 2:30for the terrain chunk that I am
  62. 2:32currently on and discard all the other
  63. 2:34ones so my Collision script which
  64. 2:37creates and manages Collision chunks is
  65. 2:39attached to this parent node right here
  66. 2:42so let's look at what we have in the
  67. 2:43script if we want to generate a
  68. 2:45collision shape from the height map we
  69. 2:48must first deliver the height map into
  70. 2:50it right so I'm getting mine from the
  71. 2:53Shader param see my terrain node which I
  72. 2:56creatively named Plane 2 has the vertex
  73. 2:59Shader which stores the link to the
  74. 3:01height map image so I'm just going to
  75. 3:03grab it off of there it's just more
  76. 3:05convenient for me to do it in this way
  77. 3:07once we know where the height map image
  78. 3:09is we can now go about creating the
  79. 3:13Collision shape from it I built this
  80. 3:16create Collision function for it and in
  81. 3:19it we first set the scale of the
  82. 3:22Collision node to match the scale of my
  83. 3:25terrain Collision decimation is totally
  84. 3:28optional it it's a means to generate
  85. 3:31less detailed colliders basically for
  86. 3:33example when your height map image is 4K
  87. 3:37but it's not very rough you may want to
  88. 3:41have a collision mesh that is 2K or even
  89. 3:441K that should be good enough you may
  90. 3:47not need all this detail in the
  91. 3:48Collision mesh in which case you would
  92. 3:50decimate the Collision mesh while
  93. 3:53generating it I'll explain how it works
  94. 3:55in just a moment okay next we need to
  95. 3:58get the actual height map image uh for
  96. 4:01this to work you need to make sure that
  97. 4:03you import the height map image as an
  98. 4:05image not as a texture we also need to
  99. 4:08know what the height of the terrain is
  100. 4:11this is the exact same value that you
  101. 4:14used in your vertex Shader to create the
  102. 4:17terrain displacement in the first place
  103. 4:19I could just put the number here but
  104. 4:22again I want to automate it so I'll just
  105. 4:24grab the height of value from the
  106. 4:27terrain Shader parameter and then this
  107. 4:30is the decimation trick here okay I take
  108. 4:33the height map image and resize it to
  109. 4:35the height and width divided by the
  110. 4:38decimation value so if the decimation
  111. 4:40value is 2 which it is in my case we're
  112. 4:44going to resize the image by half and
  113. 4:46and that is going to produce a collision
  114. 4:48shape that is uh you know half as
  115. 4:52detailed as the terrain this is good
  116. 4:55enough in my case the reason there's a A
  117. 4:57plus one on the width and height is is
  118. 5:01that if you don't add one more pixel
  119. 5:02there will be a gap between two
  120. 5:05neighboring Collision chunks the reason
  121. 5:07for that is that in order to build one
  122. 5:10quad of the Collision shape you need
  123. 5:13four vertices so let's say your height
  124. 5:15map image is three by three pixels so it
  125. 5:17looks something like this you would
  126. 5:19expect your Collision shape to also be
  127. 5:22three by three right but in here if you
  128. 5:25put map width and depth to three so
  129. 5:27three by three you'll get this it's two
  130. 5:30quads by two quads right it's one short
  131. 5:32is this wrong no because vertex wise it
  132. 5:36is three by three isn't it but since
  133. 5:38this Collision shape is generated on the
  134. 5:40basis of an image we want each pixel of
  135. 5:43the image to build a quad otherwise we
  136. 5:45can't really control the shape and
  137. 5:47that's why you need to add this extra
  138. 5:48row of pixels on each axis because that
  139. 5:51will give you uh the extra row of
  140. 5:53vertices and will patch this Gap okay
  141. 5:55long story but hopefully it makes sense
  142. 5:58next we need to create a a new height
  143. 6:01map shape 3D resource and we're going to
  144. 6:05assign it to the shape property of our
  145. 6:07Collision shape node set the width and
  146. 6:09depth using the resized image and then
  147. 6:12what's left is to generate the float
  148. 6:13array of height values to populate this
  149. 6:17map data property and to do this we'll
  150. 6:19Loop through the X and Y of the height
  151. 6:22map image and get all the pixels of the
  152. 6:26image and each pixels color Channel
  153. 6:28we're going to multiply by the terrain
  154. 6:31height value before pushing it into the
  155. 6:33array I'm using the red channel here as
  156. 6:35you can see the r but it doesn't really
  157. 6:37matter which one you use it's a
  158. 6:40grayscale image so all three channels RB
  159. 6:43and G will have the same value anyway
  160. 6:45and and that's it then we just feed the
  161. 6:49float array into the map data parameter
  162. 6:52on the shape property and we have our
  163. 6:56Collision the rest of the script is
  164. 6:57totally out of scope for this episode
  165. 6:59but it's basically a Rough and Ready way
  166. 7:01to move the Collision shape around the
  167. 7:03world so that it's always under my feet
  168. 7:06so just quickly the way I set this up is
  169. 7:09I have this function here that creates
  170. 7:11anchor points around the starting
  171. 7:14position of the Collision node of the
  172. 7:17first Collision node it has a radius
  173. 7:19setting I have it hard set to 10 you can
  174. 7:22set it to whatever you want meaning that
  175. 7:24it will spawn Five 3D nodes in both
  176. 7:27directions under x axis and then for
  177. 7:31each Anchor Point it will create more
  178. 7:33anchor points in each direction from
  179. 7:36that Anchor Point on the y-axis so
  180. 7:38effectively this function creates a grid
  181. 7:41of anchor points around my starting
  182. 7:44position and these anchor points are
  183. 7:47used by the Collision chunk to attach
  184. 7:49itself to so when I get close to an
  185. 7:51anchor point the script will get a copy
  186. 7:54of the active Collision chunk and attach
  187. 7:57that copy to the Anchor Point that I'm
  188. 7:59approaching and after I step onto this
  189. 8:02new trunk and move away from the
  190. 8:04previous chunk the previous chunk gets
  191. 8:06discarded that's just how I did it you
  192. 8:09can do it in your own way whatever works
  193. 8:10for you well I hope this was helpful
  194. 8:12stay tuned for the next episode in which
  195. 8:15I'll explain how I got this infinite
  196. 8:18grass thing going on using particle
  197. 8:20shaders in God of four and it's going to
  198. 8:22be a much nicer video leave a like if
  199. 8:25you enjoyed this one consider
  200. 8:27subscribing for more content if you want
  201. 8:29more frequent updates follow me on
  202. 8:31Twitter and now I guess I'll leave you
  203. 8:33with a few of the recent Clips I
  204. 8:37captured of my project when I was
  205. 8:38testing the grass and other things
  206. 8:39thanks for watching uh till next time
  207. 8:42peace
  208. 8:47foreign
  209. 9:19[Music]
  210. 9:22foreign
  211. 9:28[Music]
  212. 9:50[Music]

About this transcript

This page contains the full transcript of Collision for Terrain Generated via Vertex Displacement / Informal Tutorial, Godot 4 Alpha 10 by devmar, generated from the public captions YouTube serves with the video. The transcript has 1,564 words across 212 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.