How to implement falling tiles like the sand tiles in Terraria.

Godot Version

4

Question

I’m making a 2d mining game with tilemaps and I want to implement a mechanic where if you destroy a “sturdy” tile on the ceiling of a cave for example and above that tile is a “falling” tile like a sand tile, that tile would fall to the ground below, along with its colliding tiles above, and deal damage to the player if standing below. How would you go about coding a mechanic like this? I just started learning programming and I’m really lost here/ struggling to find any resources online. I would really appreciate it if someone could point me to the right direction! Thanks! :pray::blush:

I think Terraria and Minecraft both make them entities (or the equivalent) so they don’t have to abide by the grid. If you wanted to do this, create a sand scene that extends RigidBody2D or similar (characterbody2d could work pretty well) and alllow gravity to do it’s thing. You would want to set some properties so your sand doesn’t bounce.

If you wanted to stay in the grid, I would check every frame (or every other frame) if the tile below the sand was empty. If so, set that tile to sand and remove sand from above. You could check all the way up (check the whole column) and move the whole column down if you wanted that. Then, I would spawn a RayCast2D or ShapeCast2D to check for player collisions. If there is a player there, damage them (send using signals).

I’m assuming you’re using a tilemap, and those are fixed to a grid (unless you want to get into some serious shenanigans).

What you probably want to do is, if sand tiles get destabilized, calculate which of those tiles are going to fall. When you know what tiles are going to move/fall, remove them from the tilemap, and replace them immediately with Sprite2Ds with the same images.

You can then animate the sprites until they’ve settled, then remove them and bake equivalent tiles back into their new positions in the tilemap.

Interesting… I like the second approach. In the first one where you’re talking about how terraria makes them entities, wouldn’t that be really bad for optimization? Having hundreds of blocks, each with their own colliders, rigidbodies, or am I missing something/ misunderstanding what youre saying?

Because they only fall vertically, and the floor is in a grid, it’s relatively simple to optimize. Also, how many falling blocks are you planning? If it’s anything close to Noita, maybe a different engine is better (Noita used a custom engine called the Falling Everything engine). Sure, it might be a little costly, but if you optimize well, it’s not too bad. Maybe you don’t use rigidbodies, but charaterbodies to do collision better, or a custom node that rebuilds collision from scratch.

1 Like

Okay that makes sense! Thanks so much, this helps a lot! :slightly_smiling_face: