Bouncing from tilemap walls

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

Hi. I have in my top down game walls TileMap. And all of the walls tiles has a collision shape. And i have a area2d node, that moving with [position += n] method. And they can be destroyed when touch the wall. and i need to bounce they from the walls. I know about bounce or reflect Vector2 methods, but i can’t advance they’s for that methods. Thanks for help!

(sorry for bad eng)

:bust_in_silhouette: Reply From: IagoAlmeida

Hey there!

If you are using some kind of speed variable in your projectile, all you got to do when you want to reflect it is invert its speed, horizontally if you hit a vertical wall and vertically if you hit a horizontal wall.

You can determinate if the projectile hits the wall vertically or horizontally in many ways, one of the easies is basically check the difference between the position of the colliding and collider instances:

if body.position.x < position.x:
    # Body is hitting the Area2D on the left

By combining those verifications you can determinate if you are going to invert the X axis speed or the Y axis speed.