Collision for Terrain Generated via Vertex Displacement / Informal Tutorial, Godot 4 Alpha 10 — Transcript
Full transcript
- 0:00okay let's jump right into it I'm making
- 0:02this video because I said I would make
- 0:04it and a few people messaged me about it
- 0:07so let's just get this over with and
- 0:09move on to more uh fascinating things
- 0:12how do you create a collision for a
- 0:16Shader generated terrain Inca doll well
- 0:19let me show you how to do it again as
- 0:21with nearly everything else in Godot
- 0:23there are multiple ways to bake a
- 0:26collider for height mop terrain you can
- 0:29also use the mesh tools and so on but
- 0:31this one seems the easiest and indeed
- 0:34the fastest and probably the intended
- 0:36way to considering the name of the
- 0:38resource that we're going to be using
- 0:39Okay firstly in the Collision shape node
- 0:43you have this property here called shape
- 0:45which I'm sure you're well aware of what
- 0:47you may have missed about it though is
- 0:49that on the list of shapes here you have
- 0:52this thing called height map shape 3D
- 0:55and so if we go ahead and select it and
- 0:58set the width and depth to let's say a
- 1:01thousand by a thousand you'll see it
- 1:04creates a collision mesh which is
- 1:06nothing else but an array of height
- 1:08values so if you click on this array
- 1:10you'll see that we have all of our
- 1:12height values here on this list there's
- 1:15one for each vertex on this Collision
- 1:17mesh that we've just created so if we
- 1:20put a value in the first one let's say
- 1:2350 you'll see that in the corner over
- 1:25there there is a spike well that is
- 1:28exactly the first vertex reaching the
- 1:30height of 50. that's what's going on so
- 1:33if I put a value into the second one and
- 1:36the third one and so on you'll see how
- 1:38it's starting to build a Terrain shape
- 1:40now this means that if we can somehow
- 1:43populate this map data array with height
- 1:46map values from our height map image we
- 1:49will be able to build the Collision
- 1:51shape that will exactly match the
- 1:53terrain shape which is what we're here
- 1:55to do and we can do it with just a few
- 1:57lines of code so let me show you this is
- 2:01the script and before we go into it let
- 2:03me just explain the setup real quick but
- 2:06you can have a much simpler setup if you
- 2:08want but I've decided to keep my
- 2:10Collision nodes under this parent node
- 2:13called terrain collisions because I'm
- 2:16spawning and despawning collision chunks
- 2:18as the player moves around the map and
- 2:20the reason I have it set up like this is
- 2:22well if you recall in from my previous
- 2:24videos my terrain is infinite so I need
- 2:27a way to dynamically create collisions
- 2:30for the terrain chunk that I am
- 2:32currently on and discard all the other
- 2:34ones so my Collision script which
- 2:37creates and manages Collision chunks is
- 2:39attached to this parent node right here
- 2:42so let's look at what we have in the
- 2:43script if we want to generate a
- 2:45collision shape from the height map we
- 2:48must first deliver the height map into
- 2:50it right so I'm getting mine from the
- 2:53Shader param see my terrain node which I
- 2:56creatively named Plane 2 has the vertex
- 2:59Shader which stores the link to the
- 3:01height map image so I'm just going to
- 3:03grab it off of there it's just more
- 3:05convenient for me to do it in this way
- 3:07once we know where the height map image
- 3:09is we can now go about creating the
- 3:13Collision shape from it I built this
- 3:16create Collision function for it and in
- 3:19it we first set the scale of the
- 3:22Collision node to match the scale of my
- 3:25terrain Collision decimation is totally
- 3:28optional it it's a means to generate
- 3:31less detailed colliders basically for
- 3:33example when your height map image is 4K
- 3:37but it's not very rough you may want to
- 3:41have a collision mesh that is 2K or even
- 3:441K that should be good enough you may
- 3:47not need all this detail in the
- 3:48Collision mesh in which case you would
- 3:50decimate the Collision mesh while
- 3:53generating it I'll explain how it works
- 3:55in just a moment okay next we need to
- 3:58get the actual height map image uh for
- 4:01this to work you need to make sure that
- 4:03you import the height map image as an
- 4:05image not as a texture we also need to
- 4:08know what the height of the terrain is
- 4:11this is the exact same value that you
- 4:14used in your vertex Shader to create the
- 4:17terrain displacement in the first place
- 4:19I could just put the number here but
- 4:22again I want to automate it so I'll just
- 4:24grab the height of value from the
- 4:27terrain Shader parameter and then this
- 4:30is the decimation trick here okay I take
- 4:33the height map image and resize it to
- 4:35the height and width divided by the
- 4:38decimation value so if the decimation
- 4:40value is 2 which it is in my case we're
- 4:44going to resize the image by half and
- 4:46and that is going to produce a collision
- 4:48shape that is uh you know half as
- 4:52detailed as the terrain this is good
- 4:55enough in my case the reason there's a A
- 4:57plus one on the width and height is is
- 5:01that if you don't add one more pixel
- 5:02there will be a gap between two
- 5:05neighboring Collision chunks the reason
- 5:07for that is that in order to build one
- 5:10quad of the Collision shape you need
- 5:13four vertices so let's say your height
- 5:15map image is three by three pixels so it
- 5:17looks something like this you would
- 5:19expect your Collision shape to also be
- 5:22three by three right but in here if you
- 5:25put map width and depth to three so
- 5:27three by three you'll get this it's two
- 5:30quads by two quads right it's one short
- 5:32is this wrong no because vertex wise it
- 5:36is three by three isn't it but since
- 5:38this Collision shape is generated on the
- 5:40basis of an image we want each pixel of
- 5:43the image to build a quad otherwise we
- 5:45can't really control the shape and
- 5:47that's why you need to add this extra
- 5:48row of pixels on each axis because that
- 5:51will give you uh the extra row of
- 5:53vertices and will patch this Gap okay
- 5:55long story but hopefully it makes sense
- 5:58next we need to create a a new height
- 6:01map shape 3D resource and we're going to
- 6:05assign it to the shape property of our
- 6:07Collision shape node set the width and
- 6:09depth using the resized image and then
- 6:12what's left is to generate the float
- 6:13array of height values to populate this
- 6:17map data property and to do this we'll
- 6:19Loop through the X and Y of the height
- 6:22map image and get all the pixels of the
- 6:26image and each pixels color Channel
- 6:28we're going to multiply by the terrain
- 6:31height value before pushing it into the
- 6:33array I'm using the red channel here as
- 6:35you can see the r but it doesn't really
- 6:37matter which one you use it's a
- 6:40grayscale image so all three channels RB
- 6:43and G will have the same value anyway
- 6:45and and that's it then we just feed the
- 6:49float array into the map data parameter
- 6:52on the shape property and we have our
- 6:56Collision the rest of the script is
- 6:57totally out of scope for this episode
- 6:59but it's basically a Rough and Ready way
- 7:01to move the Collision shape around the
- 7:03world so that it's always under my feet
- 7:06so just quickly the way I set this up is
- 7:09I have this function here that creates
- 7:11anchor points around the starting
- 7:14position of the Collision node of the
- 7:17first Collision node it has a radius
- 7:19setting I have it hard set to 10 you can
- 7:22set it to whatever you want meaning that
- 7:24it will spawn Five 3D nodes in both
- 7:27directions under x axis and then for
- 7:31each Anchor Point it will create more
- 7:33anchor points in each direction from
- 7:36that Anchor Point on the y-axis so
- 7:38effectively this function creates a grid
- 7:41of anchor points around my starting
- 7:44position and these anchor points are
- 7:47used by the Collision chunk to attach
- 7:49itself to so when I get close to an
- 7:51anchor point the script will get a copy
- 7:54of the active Collision chunk and attach
- 7:57that copy to the Anchor Point that I'm
- 7:59approaching and after I step onto this
- 8:02new trunk and move away from the
- 8:04previous chunk the previous chunk gets
- 8:06discarded that's just how I did it you
- 8:09can do it in your own way whatever works
- 8:10for you well I hope this was helpful
- 8:12stay tuned for the next episode in which
- 8:15I'll explain how I got this infinite
- 8:18grass thing going on using particle
- 8:20shaders in God of four and it's going to
- 8:22be a much nicer video leave a like if
- 8:25you enjoyed this one consider
- 8:27subscribing for more content if you want
- 8:29more frequent updates follow me on
- 8:31Twitter and now I guess I'll leave you
- 8:33with a few of the recent Clips I
- 8:37captured of my project when I was
- 8:38testing the grass and other things
- 8:39thanks for watching uh till next time
- 8:42peace
- 8:47foreign
- 9:19[Music]
- 9:22foreign
- 9:28[Music]
- 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.