{"id":356895,"date":"2024-05-20T23:39:16","date_gmt":"2024-05-20T23:39:16","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=356895"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-20T23:39:16","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=356895","title":{"rendered":"<span>How to make a destructible landscape in Godot 4<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>In my just released game \u201cProtolife: Other Side\u201d I have the destructible landscape. Creatures that we control can make new ways through the walls. Also, some enemies are able to modify the landscape as well.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/755\/557\/009\/755557009aad1649026ee060ff4598c7.gif\" width=\"674\" height=\"496\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/755\/557\/009\/755557009aad1649026ee060ff4598c7.gif\"\/><\/figure>\n<p>That was made in Godot 3.5, but I was interested in how to do the same in Godot 4 (spoiler: no big differences).<\/p>\n<p>The solution is pretty simple. I use subdivided plane mesh and HeightMapShape3D as a collider. In runtime, I modify both of them.<\/p>\n<h3>How to modify mesh in runtime<\/h3>\n<p>There are multiple tools that could be used in Godot to generate or modify meshes (they are described in docs:\u00a0<a href=\"https:\/\/docs.godotengine.org\/en\/stable\/tutorials\/3d\/procedural_geometry\/index.html\" rel=\"noopener noreferrer nofollow\"><u>https:\/\/docs.godotengine.org\/en\/stable\/tutorials\/3d\/procedural_geometry\/index.html<\/u><\/a>). I use two tools here:<\/p>\n<ul>\n<li>\n<p>MeshDataTool to modify vertex position<\/p>\n<\/li>\n<li>\n<p>SurfaceTool to recalculate normals and tangents<\/p>\n<\/li>\n<\/ul>\n<p>BTW, the latter is the slowest part of the algorithm. I hope there is a simple way to recalculate normals manually just for a few modifier vertices.<\/p>\n<pre><code class=\"python\">func modify_height(position: Vector3, radius: float, set_to: float, min = -10.0, max = 10.0):     mesh_data_tool.clear()     mesh_data_tool.create_from_surface(mesh_data, 0)     var vertice_idxs = _get_vertice_indexes(position, radius)     # Modify affected vertices     for vi in vertice_idxs:         var pos = mesh_data_tool.get_vertex(vi)         pos.y = set_to         pos.y = clampf(pos.y, min, max)         mesh_data_tool.set_vertex(vi, pos)     mesh_data.clear_surfaces()     mesh_data_tool.commit_to_surface(mesh_data)     # Generate normals and tangents     var st = SurfaceTool.new()     st.create_from(mesh_data, 0)     st.generate_normals()     st.generate_tangents()     mesh_data.clear_surfaces()     st.commit(mesh_data)  func _get_vertice_indexes(position: Vector3, radius: float)->Array[int]:     var array: Array[int] = []     var radius2 = radius*radius     for i in mesh_data_tool.get_vertex_count():         var pos = mesh_data_tool.get_vertex(i)         if pos.distance_squared_to(position) &lt;= radius2:             array.append(i)     return array <\/code><\/pre>\n<h3>How to modify collision shape in runtime<\/h3>\n<p>This is much easier than modifying of mesh. Just need to calculate a valid offset in the height map data array, and set a new value to it.<\/p>\n<pre><code class=\"python\"># Modify affected vertices for vi in vertice_idxs: var pos = mesh_data_tool.get_vertex(vi) pos.y = set_to pos.y = clampf(pos.y, min, max) mesh_data_tool.set_vertex(vi, pos)     # Calculate index in height map data array     # Array is linear, and has size width*height     # Plane is centered, so left-top corner is (-width\/2, -height\/2)     var hmy = int((pos.z + height\/2.0) * 0.99)     var hmx = int((pos.x + width\/2.0) * 0.99)     height_map_shape.map_data[hmy*height_map_shape.map_width + hmx] = pos.y  <\/code><\/pre>\n<h3>Editor<\/h3>\n<p>I could not resist and made an in-editor landscape map (via\u00a0<code><a class=\"mention\" href=\"\/users\/tool\">@tool<\/a><\/code>\u00a0script, not familiar with editor plugins yet).<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8e2\/a3c\/38b\/8e2a3c38b63a2202735fb309fa209207.gif\" width=\"526\" height=\"407\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8e2\/a3c\/38b\/8e2a3c38b63a2202735fb309fa209207.gif\"\/><\/figure>\n<h3>Demo<\/h3>\n<p>This is how it may look like in the game itself.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e59\/d61\/513\/e59d61513733cf96932e21e78b95bf8d.gif\" width=\"674\" height=\"496\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e59\/d61\/513\/e59d61513733cf96932e21e78b95bf8d.gif\"\/><\/figure>\n<p>I\u2019ve put all this on\u00a0<a href=\"https:\/\/github.com\/AlexeyGrishin\/godot-4-landscape\/tree\/main\" rel=\"noopener noreferrer nofollow\"><u>github<\/u><\/a>. Maybe someday I will make an addon for the asset library.<\/p>\n<p>I hope that was useful.<\/p>\n<p>P.S. Check my\u00a0<a href=\"https:\/\/store.steampowered.com\/app\/2346710\/Protolife_Other_Side\/\" rel=\"noopener noreferrer nofollow\"><u>\u201cProtolife: Other Side\u201d<\/u><\/a>\u00a0game. But please note: this is a simple casual arcade, not a strategy like the original \u201cProtolife\u201d. I\u2019ve made a mistake with game naming \ud83d\ude41<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/765976\/\"> https:\/\/habr.com\/ru\/articles\/765976\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>In my just released game \u201cProtolife: Other Side\u201d I have the destructible landscape. Creatures that we control can make new ways through the walls. Also, some enemies are able to modify the landscape as well.<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>That was made in Godot 3.5, but I was interested in how to do the same in Godot 4 (spoiler: no big differences).<\/p>\n<p>The solution is pretty simple. I use subdivided plane mesh and HeightMapShape3D as a collider. In runtime, I modify both of them.<\/p>\n<h3>How to modify mesh in runtime<\/h3>\n<p>There are multiple tools that could be used in Godot to generate or modify meshes (they are described in docs:\u00a0<a href=\"https:\/\/docs.godotengine.org\/en\/stable\/tutorials\/3d\/procedural_geometry\/index.html\" rel=\"noopener noreferrer nofollow\"><u>https:\/\/docs.godotengine.org\/en\/stable\/tutorials\/3d\/procedural_geometry\/index.html<\/u><\/a>). I use two tools here:<\/p>\n<ul>\n<li>\n<p>MeshDataTool to modify vertex position<\/p>\n<\/li>\n<li>\n<p>SurfaceTool to recalculate normals and tangents<\/p>\n<\/li>\n<\/ul>\n<p>BTW, the latter is the slowest part of the algorithm. I hope there is a simple way to recalculate normals manually just for a few modifier vertices.<\/p>\n<pre><code class=\"python\">func modify_height(position: Vector3, radius: float, set_to: float, min = -10.0, max = 10.0):     mesh_data_tool.clear()     mesh_data_tool.create_from_surface(mesh_data, 0)     var vertice_idxs = _get_vertice_indexes(position, radius)     # Modify affected vertices     for vi in vertice_idxs:         var pos = mesh_data_tool.get_vertex(vi)         pos.y = set_to         pos.y = clampf(pos.y, min, max)         mesh_data_tool.set_vertex(vi, pos)     mesh_data.clear_surfaces()     mesh_data_tool.commit_to_surface(mesh_data)     # Generate normals and tangents     var st = SurfaceTool.new()     st.create_from(mesh_data, 0)     st.generate_normals()     st.generate_tangents()     mesh_data.clear_surfaces()     st.commit(mesh_data)  func _get_vertice_indexes(position: Vector3, radius: float)->Array[int]:     var array: Array[int] = []     var radius2 = radius*radius     for i in mesh_data_tool.get_vertex_count():         var pos = mesh_data_tool.get_vertex(i)         if pos.distance_squared_to(position) &lt;= radius2:             array.append(i)     return array <\/code><\/pre>\n<h3>How to modify collision shape in runtime<\/h3>\n<p>This is much easier than modifying of mesh. Just need to calculate a valid offset in the height map data array, and set a new value to it.<\/p>\n<pre><code class=\"python\"># Modify affected vertices for vi in vertice_idxs: var pos = mesh_data_tool.get_vertex(vi) pos.y = set_to pos.y = clampf(pos.y, min, max) mesh_data_tool.set_vertex(vi, pos)     # Calculate index in height map data array     # Array is linear, and has size width*height     # Plane is centered, so left-top corner is (-width\/2, -height\/2)     var hmy = int((pos.z + height\/2.0) * 0.99)     var hmx = int((pos.x + width\/2.0) * 0.99)     height_map_shape.map_data[hmy*height_map_shape.map_width + hmx] = pos.y  <\/code><\/pre>\n<h3>Editor<\/h3>\n<p>I could not resist and made an in-editor landscape map (via\u00a0<code><a class=\"mention\" href=\"\/users\/tool\">@tool<\/a><\/code>\u00a0script, not familiar with editor plugins yet).<\/p>\n<figure class=\"full-width\"><\/figure>\n<h3>Demo<\/h3>\n<p>This is how it may look like in the game itself.<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>I\u2019ve put all this on\u00a0<a href=\"https:\/\/github.com\/AlexeyGrishin\/godot-4-landscape\/tree\/main\" rel=\"noopener noreferrer nofollow\"><u>github<\/u><\/a>. Maybe someday I will make an addon for the asset library.<\/p>\n<p>I hope that was useful.<\/p>\n<p>P.S. Check my\u00a0<a href=\"https:\/\/store.steampowered.com\/app\/2346710\/Protolife_Other_Side\/\" rel=\"noopener noreferrer nofollow\"><u>\u201cProtolife: Other Side\u201d<\/u><\/a>\u00a0game. But please note: this is a simple casual arcade, not a strategy like the original \u201cProtolife\u201d. I\u2019ve made a mistake with game naming \ud83d\ude41<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/765976\/\"> https:\/\/habr.com\/ru\/articles\/765976\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-356895","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=356895"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/356895\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=356895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=356895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=356895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}