Get GridMap cell from collision

Godot Version

4.1.2

Question

Hello, I am casting a ray from the camera that collides with a tile in a GridMap. I can see the collision result which is the GridMap itself, I then want to map the coordinates to grid space and select the specific tile that was collided. My goal is to then get the group of the tile. It seems no matter what I do i keep getting INVALID_CELL_ITEM.

Here is the snippet:

Any ideas what I am doing wrong? Thank you in advance!

The problem is that there is no single collision “tile” in a GridMap. Like everything the GridMap combines things to quadrants for performance. So in this case there is only a static body per quadrant, but not per tile. Since a quadrant is an internal update unit with no node or resource representation the GridMap returns itself as the collider object.

So what you need to do is remap the collision position to the cell in question yourself. The simplest way to do this with a global collision position (while your GridMap is at the world origin) is to div it with your cell size and floor it to a Vector3i integer coord.

That is what GridMap.map_to_local() does. I think the problem in your script is that you try to remap a global position to_local() while the functions wants a global position, not a local. If INVALID_CELL_ITEM is returned with get_cell_item() using this coord then the cell has no MeshLbirary item.

1 Like

Thank you for the reply, I removed the to_local and I am receiving invalid so I assume it is the mesh issue you brought up. Here is a screen shot of the only tile from the GridMap I am using. It has a mesh instance, is that what you are referring to? Could it be a cell size issue?

image

That is your export scene, that scene does not exist like that after the export.

The “real” scene that matters is what is inside the MeshLibrary item, so look at the collision shape there.

Okay I think I’m getting what you are saying, I found this view when I clicked edit on the mesh library.

A few questions come to mind:
In the future should I just keep the scene simple when I export and add a shape to whatever tiles in the mesh library? Or let the export handle it?

Is the concave shape correct or should I manually change it to something else?

Thank you again for all the help.

The concave shape is ok. The exporter does not really know that you have a shape that could be replaced by a slightly more efficient box shape. You could change that for some small speed up yourself later or create the item directly in the MeshLibrary like that but it is nothing big.

What would matter more for performance and stability is that you save all those resources like meshes, shapes and materials to external resources. You could use a script for that when your MeshLibrary gets big. Because while all those subresourcs are part of the MeshLibrary and scene everytime you save they are also all saved again which gets slower and slower the bigger your lib gets.

Thanks that is definitely above my capabilities at the moment but something to keep in mind. Any ideas on why i cant get the cell index from the collision coords?

Hey so by complete luck I found the issue, it appears that my GridMap position was not set to 0,0,0, and changing it to 0,0,0 fixed the issue. Thanks for the help.