tilemaps and collisions

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

I seem to have a bit of problem. Creating a breakout game.
I create my bricks with a tilemap. I detect when the ball hits a brick. However the ball hits always just one tile wrong. This depending the impact of my ball on the tile.

Imagine my ball hits tile (8,5) then the debug print say I hit will be (8,6)
If I hit the brick from the top the printout says (8,4)
if I hit from the left side it will be (7,4) and right (9,4)

I have be thinking to get the direction of my ball and simple do calculate calculate the number. But that seems overkill. Is there a way not to give the tile with that the ball hits instead of the position of where the ball hits

:bust_in_silhouette: Reply From: Footurist

Can you provide your project file? I need to see the problem…

pos == the position of the ball that hits the brick
dir == the direction of the ball normalized.

So we hit the tileset get via a signal the position of the ball. I convert to world_to_map() to get the tile position. However the ball position is never the collided tile.

Value that is passed when ball collides with brick.

func _physics_process(delta):
	var bodies = get_colliding_bodies()
	for body in bodies:
		if body.get_name() == 'bricks':
			emit_signal("hit", position, linear_velocity.normalized())

World map tileset script

  func _collided(pos, dir):
    	for child in get_children():
    		if child.get_name() == "bricks":
    			print(pos)
    			if dir.x <= 0:
    				pos.x = pos.x + 16
    			else:
    				pos.x = pos.x - 16
    			
    			if dir.y <= 0:
    				pos.y = pos.y + 8
    			else:
    				pos.y = pos.y - 8
    			print(pos)
    			var tile = child.world_to_map(pos)
    			print(child.get_used_cells())
    			print(tile)
    			child.set_cellv(tile, -1)

Enquest | 2018-02-28 16:41