One way collision tile with Tiled and Godot


This is DeadBox . With it we want to explore the design and the development of a platform game in GodotEngine.

Today we are going to talk about one-way collision boxes. Here is an example of our little hero jumping on platforms with a one-way collision:


Example of DeadBox jumping on and off one-way collision tiles

I see lots of tutorial that explain how to manually add a box and a CollisionShape2D directly from Godot. But I haven’t seen anything on Tiled.

The maps in our games are made in Tiled. This allows us to build them in a easy way, and use all the fantastic features that Tiled has to offer. In another article we posted something about our tileset setup, check it out here.

Below there is an image that shows our rules.tmx file. You can see the top left image showing the mapping layers:

  • harmful: layer for the elements of the map that hurt the player
  • solid: layer for the collision elements, like wall, floor, ceiling, platform
  • bouncy: layer that allows the player to bounce when stepping on it (to be implemented yet)
  • water: layer object for the water tiles (more about it in a future post)
  • jumpthrough: layer that will identify the one-way platform
  • regions: layer that allows the automapping for Tiled
  • art: layer that identifies our own tileset

In order to better understand the automapping, I suggest to read the docs.


Mapping layers


Properties of a one-way collision tile. As you can see the ID is 122.


Tile that will map our main layer ("art")

In the first picure we can see the properties of the abovementioned tile. We need to know its ID, in order to let Godot know what is the tile to which apply the undocumented function tile_set.tile_set_shape_one_way. The function allows us to set, via code,  a CollisionShape2D node to one-way collision mode (the same as checking the box “One Way Collision” in Godot UI).

After having the tilemap imported we need to find the proper layer and put the right CollisionShape to it. Unfortunately for us, the script doesn’t allow,  yet, handling types of tiles in a better way than we did it. A big shout out to the developer of this incredible plugin. Hopefully the 3.1 version will be released soon enough, as stated here in this issue.

We adopted this workaround: on the gdscript of the root node World we add the following script:

# World.gd
# enable one-ways collision in jumpthrough layer
var jtl = map.get_node('jumpthrough')
if jtl:  # workaround - waiting for a better implementation in godot-tiled-importer
# reference: <a href="https://github.com/vnen/godot-tiled-importer/issues/37
">https://github.com/vnen/godot-tiled-importer/issues/37
</a>  jtl.tile_set.tile_set_shape_one_way(123, 0, true)  

#123 here is the ID of the current tile into the tileset. For some reason the imported tileset starts counting the IDs from 1 instead of 0.

#0 is the CollisionShape associated to that tile in the tileset.

This workaround is not stable because there is no way to find out the index of the CollisionShape that the plugin gives to the tile when importing the tilemap. That might change if you reimport it with a new map.

We might be wrong about the automapping and setting the right properties, let us know with a comment down below. We would like to know if there is anoter or a better way.

Fail and learn. 🙂

See you next time!

Leave a comment

Log in with itch.io to leave a comment.