Tag Archives: tilemap collision

Godot Tilemap Collision Detection & Finding the Corresponding Colliding Cell

So I’ve been working on a gamejam called the godot wild jam 5, and I will release source when I’m finish (hopefully I pull through, only have a few days left to get this done), and I ran into an issue with godot’s tilemaps. Basically the way godot’s tilemaps work from my understanding is in order for you to get collision detection you have to create your own tileset from nodes using staticbody2d’s with collisionshape2d’s as a child. That way you can in fact tell when you are colliding into other objects.

There were two issues I ran into, the first one being I couldn’t figure out how to deal with tilemaps that were scaled in a parent node up/down. The second issue was related to that and figuring out how to get the cell that my player touched. I have a solution to both.

First and foremost for this example to work you need to have your player node be a kinematicbody2d. The reason for this is because KinematicBody2D allows you to get some collision data that other nodes won’t give you without using a raycaster which I have no experience with at this time. That collision data comes from a method called move_and_collide or (MoveAndCollide for c#). This returns a KinematicCollision2D that gives you basic information on the object you collided with and the position of the object that you just collided with.

From here it made sense to subtract my current players position from that colliders position to figure out the direction of where the collision was coming from and in our case we wanted to know if it was TR, TL, BR, BL, T, R, B, L (top right, top left, etc…) that way we could determine the exact cell location that tile is located from my current position.

You wouldn’t think it would be that simple, but it was and here is the code on how I did that and I’ll add an example project of me reproducing it using gdscript for the sake of having an example available online somewhere.

So to explain what’s happening here in this example. First we check every physics frame if we’re moving and attempt to get the collision data. Then if we have collided with something we call a handle collision function to figure out if we collided with a tilemap (you can get more specific when working with more tilemaps, like the name, etc…) then I create a temporary vector2 to store our collision cell location data in. We need to determine what side the collision was handled on from our current player cell’s position. This is when we say look, we have a 3×3 tile section that we need to figure out which of the 8 other tiles from the center position that we are touching and to do that we simply figure out if the touching tile is greater than or less than the x and y position and we either add or subtract from 0 to determine that. Or we leave it at zero and do nothing meaning we’re on the same cell row or column so it’ll either be directly T, L, B, or R.

Now after we’ve figured out the cell’s location from our player location (this works even when the data is scaled, it’s pretty nifty) – we need to say hey where the hell exactly is our player’s cell location on the tilemap’s grid. We use world_to_map to determine this information. Now we want to take our player’s position and convert it to the local coordinates of the tilemap. This is useful if our tilemap has been scaled down to fit inside of something. So we need to first convert the players position to local coordinates to get the proper map location that our player is located at inside of that tilemap. To do that we call to_local from the tilemap node. Only nodes that derive from a Node2D have these methods.

So we have the exact player cell location within that tilemap and we also know the location of our collision tilemap cell, all we have to do now is add it to the value we get from world_to_map and then use that data and pass it to get_cellv to get the exact cell index in the tilemap and you can do what ever you want with that data. Edit the tilemap by removing the cell block, it doesn’t matter.

Hope this helped you understand how to get the tilemap’s tile cell point when colliding with a tilemap using a kinematicbody2d 🙂

Download (godot project 3.1beta)