Last active
January 16, 2019 14:20
-
-
Save exts/07bcb5c8c40175a851a006c205b17b4e to your computer and use it in GitHub Desktop.
Godot Tilemap Collision (for detecting the exact colliding cell from the player position)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func _physics_process(delta): | |
var direction = Vector2() | |
# .. figure out the direction | |
var collision = move_and_collide(direction) | |
if collision: | |
handle_collision(collision) | |
func handle_collision(node): | |
var collider = get_collider() | |
if collider is TileMap: | |
var col_cell = Vector2() | |
# from player position do we add/subtract a cell on the x axis | |
if node.position.x > position.x: | |
col_cell.x += 1 | |
elif node.position.x < position.x: | |
col_cell.x -= 1 | |
# or the y axis | |
if node.position.y > position.y: | |
col_cell.y += 1 | |
elif node.position.x < position.y: | |
col_cell.y -= 1 | |
var loc = collider.world_to_map(collider.to_local(position)) | |
loc += col_cell | |
var cell = collider.get_cellv(loc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment