0 votes

Hey I did wite the following code for the ball in a breakout game:

extends KinematicBody2D

export var speed = 200
var movement = Vector2(0, speed)
var can_move = true

func _ready():
    position.x = 250
    position.y = 470

func _physics_process(delta):
    if can_move == true:
       var collision_info = move_and_collide(movement * delta)

       if collision_info:
           if collision_info.collider.name == "Player":
               speed = -speed
               var diff = collision_info.collider.position.x - position.x
               var new_movement = Vector2(-diff * 5, speed)
               movement = new_movement
           else:
               movement = movement.bounce(collision_info.normal)

Now I would like to add a bricks with a collision with the ball, wich get destroyed when they collide with the ball.
So my questions are:
Wich Node should I use for the bricks? and
How can I program the brick to be destroyed when it collides with the ball?

Thank you

asked Dec 28, 2019 in Engine by KeksKanone (15 points)
retagged Dec 28, 2019 by KeksKanone

1 Answer

0 votes
Best answer

If you don't need the bricks to move, use StaticBody2D. If you do, use KinematicBody2D.

When you move the ball, you're already detecting that you hit the paddle. You can also check if the thing you hit was a brick, and if so, call queue_free() on it to remove it.

answered Dec 28, 2019 by kidscancode (13,248 points)
selected Dec 30, 2019 by KeksKanone

Ok. Thank you

But how can I write my code, for detecting many bricks. For exemple I have 18 bricks how can I code the collission without writing 18 lines of code for each brick?

I'm not sure what you're saying - your brick doesn't need code on it at all.

Put the brick in a group called "bricks", and put the following on the ball:

var collision_info = move_and_collide(movement * delta)
if collision_info:
    if collision_info.collider.is_in_group("bricks"):
        collision_info.collider.queue_free()

Nice. Thank you

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.