I am developing a simple platformer, I have three types of tiles right now:

  • Floor: does nothing on collision.
  • Bad: makes the player lose the game.
  • Good: makes the player beat the game.

I am trying to detect the collision with this code I found:

extends RigidBody2D

func _process(delta):
    var tile = get_parent().get_node("TileMap").get_cellv(get_parent().get_node("TileMap").world_to_map(position))

The tile variable is supposed to contain the identifier of the type of tile the player is touching, but it doesn't, it always returns -1 (No exception or error, just -1).

I believe returning -1 means it isn't touching any tile, but it is.

This is using Godot 3.0 stable.

What am I missing?

Thank you for your patience.


I've found a way to make it work, I am not sure if this is the best possible solution but it works:

extends RigidBody2D

func _integrate_forces(state):
    var tilemap= get_parent().get_node("TileMap");  
    for i in range(get_colliding_bodies().size()):
        var tile_pos = tilemap.world_to_map(state.get_contact_local_position(i))
        var tile = tilemap.get_cellv(tile_pos)

This function is inside my player script. var tile will contain the type id of each colliding tile.


I don't know if this helps, but in my case i was raycasting and found a neat set of key value pairs to my aid:

var result = space_state.intersect_ray(origin, get_global_mouse_position(), [self, owner]);

If the colliding object is a tilemap, there is a entry called 'metadata' which contains the coordinates (in terms of tilemap coordinates) of that tile which collided with the ray.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.