Detecting Collisions between a RigidBody2D and a StaticBody2D

Godot Version

4.2.1

Question

I have been working on a simple game where you drop balls from the top and hit other balls that stay in one place below. I am try trying to use a script to detect when the two objects touch each other by using the _on_body_entered function but it does not work. The structure of the static balls which are stored in a scene goes: StaticBody2D > Collision Shape2D + Sprite2D. The bouncing balls are the same except I use RigidBody2D instead of static body. The script is attached to the Collision Shape. Here is my code:

extends StaticBody2D

func _on_body_entered(body):
	print(body)

I have tried moving this script to the StaticBody2D node and I’ve also attempted to add a script to the bouncing ball, but nothing works, and nothing is ever printed to the console. The objects are also have the same collision layering and the sizes are the same. Thanks!

Is the signal body_entered connected to this function?

No, it’s not. I’m not sure how to set that up. I’m looking in the node panel but I can’t add any functions. Do I need to add another script in the Rigidbody2D scene?

I found out you can add on_body_entered to the RigidBody2D but it still can’t tell when it collides with one of the static balls. I connected the signal to the RigidBody btw.

I found the solution. I needed to enable contact monitor after adding the signal in the RigidBody2D scene and set the max contacts to 50.

1 Like

glad you figured it out. here is some code in case it helps

extends RigidBody2D

func _ready():
	max_contacts_reported = 1
	contact_monitor = true

func _physics_process(_a_delta):
	if(0 != get_contact_count()):
		var v_list : Array[Node2D] = get_colliding_bodies()
		for j_node : Node2D in v_list:
			print (j_node.name)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.