Topic was automatically imported from the old Question2Answer platform.
Asked By
BxStudent
I have a “kinematic body 2d” and a “Tilemap”.
I want to get the position of the “Tile Set”
Which the “kinematic body 2d” collided with.
Can anybody give me a hint on how to do that,
Because there is no built in function for that as i checked.
And Thanks.
If you’re moving your KinematicBody2D with move_and_collide(), it returns a KinematicCollision2D object on collision. If you’re using move_and_slide(), you can get the collision object as well, but you must use get_slide_collision().
Either way, KinematicCollision2D.collider will be your TileMap object, and KinematicCollision2D.position will be the collision position. Also, you can use TileMap.world_to_map() to help find the tile position if you need that.
Thanks for your answer,
Im already using get_slide_collision() and KinematicCollision2D.collider to get the collided object but i cant find how to get that object (TileSet) position, could you be more specific on how to use TileMap.world_to_map() to find the tile position ? thanks again, i love your tutorials by the way.
BxStudent | 2018-09-24 02:16
world_to_map() converts from global coordinates to tile coordinates. Say your tiles are 64x64 and KinematicCollision2D.position gives you (322, 520). world_to_map(KinematicCollision2D.position would return (5, 8) - you’re standing in tile (5, 8). If you need to know the tile below you, for example, would be (5, 9).
As I read your question, that’s what you were looking for. If you need to know what tile you hit, TileMap.get_cellv(Vector2(5, 9)) is going to give you the ID of the tile in that position.