How to tell kinematic body that he is touching tile with with Area2D attached to sprite

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

I have a tileset with tiles that have sprite and Area2D as a child(with shape) .
And a player’s tank that can move. He is a kinematic body with collision shape. The tiles are grass and road.
My question is how i can check if a player’s tank is colliding with those tiles that have Area2D because i want to modify tank’s max speed when he is on road

:bust_in_silhouette: Reply From: kidscancode

TileSet does not support using Area2D, only StaticBody2D for collisions.

You would have to either add the areas separately to your map, or have your tank check what tile type it’s on using get_cell() when it moves.

Ok, so i have another idea.

I have searched and find that i can get “shape” property from tileset if i know tile ID
My new idea is to create new scene with root as “Area2D” node and child as shape, let’s call this scene “road”

and then at game start i will iterate through all tilemap cells and get tile IDs. With that i want check if current tile has a “shape” if so, then i want to instantiate “road” scene and set collision shape to the same as tile’s shape

I write code for that:

extends TileMap

onready var road = preload("res://Terrain/RoadScript.tscn")

func _ready():
	print("Get Cell IDs")
	var tileset = tile_set
	for i in get_used_cells():
		var cellID = get_cellv(i)
		var shapeCount = tileset.tile_get_shape_count(cellID)
		print("shapeCount: "+str(shapeCount))
		if shapeCount > 0:
			var shape = tileset.tile_get_shape(cellID, 0)
			var roadScene = road.instance()
			add_child(roadScene)
			roadScene.get_node("CollisionShape2D").shape = shape

But my new problem is, how i can add a shape to the tile in tileset:
enter image description here
I tried to add collisionshape2d child to the tile sprite but my script shows shapeCount = 0 for every tile

Huder | 2018-04-18 21:08

:bust_in_silhouette: Reply From: sangress

I have a player with Area2D and tilemap that has several tiles with collision set on them.
And I use signal → _on_Player_body_entered for the Player.
And it works fine when the player hits the tile.