+1 vote

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...

in Engine by (835 points)
edited by

EDIT: see my other answer

1 Answer

+4 votes
Best answer

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 :)
enter image description here
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.

Here are some links about quadtrees:
General principle: http://dylowen.blogspot.fr/2012/01/destructible-environment-quad-tree.html
A Godot 2.1 implementation with Area2Ds instead of staticBody2Ds: https://github.com/henriquelalves/destructible_terrain_demo
Another 2.1 implementations (mine :-) ) with RigidBody2Ds(instead of staticBody2Ds) where I handle custom textures: https://github.com/MattUV/DestructibleTerrainPhysics
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!

by (288 points)
selected by

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.