# Destructable 2D Terrain and a RigidBody2d character that follows it?

Attention Topic was automatically imported from the old Question2Answer platform.

So much fun
Godot 3.0 Stable

I’m making a lemmings type concept. I will be using a RigidBody2d as my “lemming” and I want the ground to be destructible.

Unless I can be swayed otherwise I’m planning on using a bitmap somehow to destroy the landscape. So for example, if I drop a bomb, it will hit the landscape, create a 2D circle that is used to eat away at the bitmap and therefore a hole/dent is created.

I’ll work toward that, but I’m thinking. If I do use that method, how can I get the RigidBody2d to follow the bitmaps outline? I can check for transparency I’m guessing and I found an example where someone manually moved a sprite about based on transparency allowing it, but that’s not using a RigidBody2D.

Any advice / direction would be much appreciated.

Thank you…

Matt_UV | 2018-02-09 15:09

Hi! I would like to change my previous answer, as it was not thought enough

To make a destructible terrain, I would definitely use quadtrees. You have to divide your terrain into squares (which is convenient if you use tilemaps). Then, when there is a “hit”, divide the square into smaller squares, and remove the bigger one. Keep doing that every time a square is hit. At the end, you will have a well sculpted terrain

In the example here I used a large final size, but you can descend down to 1x1px² to be more precise (although it may be more resourceful!)

Now you have to deal with collisions and texture.
Make the main square a StaticBody2D, with a collisionShape2D and a Sprite. When you divide it, copy it into 4 new StaticBody2Ds, and adapt the collisionShapes. For the texture, use Sprite regions, and adapt it to the size of the newer blocks.

General principle: Programming: Destructible Environment - Quad Tree
A Godot 2.1 implementation with Area2Ds instead of staticBody2Ds: GitHub - henriquelalves/destructible_terrain_demo: A simple demo on how to create destructible terrain on Godot.
Another 2.1 implementations (mine ) with RigidBody2Ds(instead of staticBody2Ds) where I handle custom textures: GitHub - MattUV/DestructibleTerrainPhysics: Godot destructible objects that react to physics.
I cheated a little bit with the collisionShapes, I loaded them first at all the sizes possible. And if you don’t want the terrain to collapse when destructed, you just have to set the blocks as static

Also, you may want to optimize the algorithm a little bit to reduce the number of subdivisions. In my example, you can see that many subdivisions “inside” the green could have been avoided.

I hope you achieve what you’d like!

Hi and first of all thank you for the amazing answer and also sorry for the delay in reply. I saw this a few days ago but work has had me pinned down and it’s the weekend so I’m trying to sneak an hour in to absorb what you’ve said.

I’ll drop back here with a proper response after having a look through all the content you just provided.

I really again want to say thanks. It’s an incredible answer.

Robster | 2018-02-16 22:40