0 votes

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.

in Engine by (12 points)

1 Answer

0 votes

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().

http://docs.godotengine.org/en/latest/classes/class_kinematiccollision2d.html

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.

by (20,897 points)

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.