0 votes

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

in Engine by (75 points)

1 Answer

0 votes

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

by (840 points)

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 worldtomap() 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":
                if dir.x <= 0:
                    pos.x = pos.x + 16
                    pos.x = pos.x - 16

                if dir.y <= 0:
                    pos.y = pos.y + 8
                    pos.y = pos.y - 8
                var tile = child.world_to_map(pos)
                child.set_cellv(tile, -1)
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.