How to implement collision detection for a Rigid 2D Body?

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

I’m in the beginning stages of making a brickbreaker type game. I’ve got the ball launcher set up, but I’m having trouble getting the brick to disappear after it gets hit. I’m trying to use the collision monitoring to have the brick recognize when it has been hit by the ball and then eventually disappear with queue free, but I can’t get it to recognize that it’s been hit. I have tried on Brick body entered as well, but it didn’t work either.

Code for Collision

Collision monitoring is available and greater than 1

Using body_entered is the correct answer. However, you didn’t mention anything about the ball - what type of body is it and how are you moving it?

kidscancode | 2022-07-21 16:19

Ah, sorry! So the ball is a Kinematic 2D Object. It bounces off the brick when it hits the brick.

theworldisaplace | 2022-07-21 16:43

How are you moving it? A kinematic body hitting the rigid body will trigger body_entered (or many of my projects wouldn’t work :P)

kidscancode | 2022-07-21 17:04

Hm here’s the code for the ball. I think how it moves is mostly answered in the two process functions.

Would it change things if I am instantiating the brick via a different node?

theworldisaplace | 2022-07-21 17:09

There’s nothing in there that looks wrong. Another possibility is your collision shapes aren’t correct. Enable “Visible Collision Shapes” in the Debug menu and see if things are where they’re supposed to be.

kidscancode | 2022-07-21 17:51

Hm, yeah looks like things are indeed where they’re supposed to be. I even moved the collision box a little bit off the paddle just so I could see it better, but the ball is definitely fully covered in its collision shape (the ball is really orange–the brown color is the tint from the collission shape). Video below:

Ball Game (DEBUG) 2022-07-21 14-24-14.mp4 - VEED

theworldisaplace | 2022-07-21 18:27

Ah I figured it out! For some reason when the RigidBody2D is set to “Static” in Mode (you can see that in one of the screenshots I posted above) it doesn’t track the collision. But if I set it to Rigid it tracks it successfully. The problem is that I want it not to be affected by gravity etc. so I’ll have to figure out a way to do that without making it Static.

theworldisaplace | 2022-07-22 13:23