Test collision with specific tile.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

I have a tile in my set named ‘ice’. Is it possible to test collision between that particular tile and the player? If so, how?

Can you be more specific? Is it topdown game - so Player is on “ice” tile or platformer - pleyer is on top/above a “ice” tile? Is Player KinematicBody2D?

pospathos | 2018-12-05 23:32

Platformer using KinematicBody2D. Apologies.

9BitStrider | 2018-12-05 23:33

:bust_in_silhouette: Reply From: pospathos

I think that you can’t use RayCast2D to get specific tile, you will get specific object but I don’t think that you can determine what tile index is that TileMapxxxxx object (someone correct me if i’m wrong). The only way I can think of is to check if Player is on the floor with is_on_floor() function. And you will need do add Position2D as a child of Player - one or two pixels below Player feet. Then you need to check if Position2D is inside tile of a “ice” name. Convert Position2D position from pixels to TileMap space: Pos2DTileSpace = Vector2( int (Position2D.x/CellSizeOfTileMap), int( Position2D.y/CellSizeOfTileMap) ), or use TileMap function world_to_map. Then check if Pos2DTileSpace have same index as your “ice” tile with get_cellv function of TileMap (you should preload TileMap in Player script with onready var myMap = get_node("MyTileMap") ). If Player is on the floor and tile index is equal “ice” tile then Player is on ice.

is_on_floor is a Godot 3 function. I’m on 2.1.5.

9BitStrider | 2018-12-06 00:00

Use RayCast2D from Player feet down some pixels instead of is on the floor, or better check for collision normal - if collision normal is pointig up you are on the floor. Then check with Position2D as I suggest it (or just use some Vector2 variable in Player script and set its value below Players feet 2-4 pixels and covert it in tilespace and check for index of tile that position is in).

pospathos | 2018-12-06 00:06

I got it to work just using the world_to_map function. No raycast needed.

var tpos = tiles.world_to_map(plyr_obj.get_global_pos())
var itile = tiles.get_cellv(Vector2(tpos.x, tpos.y + 1))
print(itile)

This returns the tile ID. I can use this to toggle the ice flag.

9BitStrider | 2018-12-06 02:52