What are some ways to detect collisions for a non-Area2D body

Godot Version

V 4.6.1

Question

After trying to make a brick breaker clone, I don`t know how to make bricks to detect collision of the ball so they could disappear.

Code of the ball:

extends CharacterBody2D

var direction = Vector2.ZERO
var speed = 350
var collision_times: int = 0

func _ready():
	global_position = Vector2(960, 600)
	#Each variable gets a random number for direction
	direction.x = [1, -1].pick_random()
	direction.y = [1, -1].pick_random()
	velocity = speed * direction

#Controls the movement of the ball
func _physics_process(delta):
	var colisionInfo := move_and_collide(velocity * delta)
	if colisionInfo:
		var normal := colisionInfo.get_normal()
		if normal.dot(velocity.normalized()) <= 0.0:
			velocity = velocity.bounce(normal)

Just to be sure, which node you’re using for your bricks? If they are from a physics class like StaticBody2D, collisionInfo will also have the brick you collided. You can retrieve the object you collided with collisionInfo.get_collider() and call queue_free() on it to delete.

1 Like