Interaction between 2 rigidbodies2D

Godot Version

4.3 on Linux

Question

Hello, I’m trying to develop a side scroller where the player will controll a 2 wheel veichle (as a bike or scooter) and have to avoid obstacles etc. I want the player to avoid / interact with a boucing ball. I have successfully set up an area2d for instantiating and spawing the ball, but I can’t find any way to detect the collisions with the player.
If using _integrate_forces in the ball script I have this error when calling contact_counts :
palla.gd:43 @ _integrate_forces(): Index p_contact_idx = 0 is out of bounds (body->contact_count = 0).
<Sorgente C++> servers/physics_2d/godot_body_direct_state_2d.cpp:204 @ get_contact_collider_id()
palla.gd:43 @ _integrate_forces()

I also tried to add to the ball (RigidBody2D) an area 2d to try to detect collisions using _on_body_entered or _on_area_2d_entred but doesen’t work. It looks like that interaction withing rigidbodies is something different from a collision.
I attach a screenshot with the Palla ( ball) scene scheme and the non-functioning lines of code.
Any help appreciated.
PS : already googled a lot and asked GPT; with no success.

Hi!
Are you sure you saved your script correctly? It shows the asterisk on the name and the error states your calling get_contact_collider_id wich would throw this error when no collision has occured.

Hi, yes the script is saved . Contact counts still keeps giving ALWAYS 0 as value.
No longer that error but I have commented the part where I tried to manage collisions.
It seems that bouncing is not detected as collision.
I’ll try to record a video of the running scene later.

Have you set max_contacts_reported > 0 and contact_monitor to true?

int max_contacts_reported = 0

void set_max_contacts_reported(value: int)

int get_max_contacts_reported()

The maximum number of contacts that will be recorded. Requires a value greater than 0 and contact_monitor to be set to true to start to register contacts. Use get_contact_count to retrieve the count or get_colliding_bodies to retrieve bodies that have been collided with.

Note: The number of contacts is different from the number of collisions. Collisions between parallel edges will result in two contacts (one at each end), and collisions between parallel faces will result in four contacts (one at each corner).

1 Like

I have set contact montor to true but I didn’t know about the max_contact_reported function yet. Will try later :wink: . Thanks .

EDIT: doesn’t work . Contacts aren’t increasing . Will post (have to upload elsewhere) a short video of the game running to explain what I want to do.

video link can’t embedd as new user

Short video of what I am trying to do (sorry still haven’t a nice sprite for the bike)
The only function reporting something is _integrate_forces()
I attach the scheme I am using for the involved scenes : player, ball and the level itself.
Any further help appreciated. Thanks in advaced. (beginner here !)

What is the purpose of the Area2D in the ball(palla)?

It was an experiment suggested by chatgpt to bypass the non-functioning of the _integrate_forces() : according to the chatbot, using an area2d with onbodyentered should have solved the problem. But doesn’t work and I will delete it.

Maybe something is mixed up by now. So this is the simplest approach i came up with to track collisions …

Does this setup suit your needs?

I already connected body_entered using the GUI. But doesn’t work.
I don’t know if using “connect” by code is doing something different, but can’t figure out how to remove the signal connected via GUI.

E 0:00:18:0758 palla.gd:11 @ _ready(): Signal ‘body_entered’ is already connected to given callable ‘RigidBody2D(palla.gd)::_on_body_entered’ in that object.
<Errore C++> Method/function failed. Returning: ERR_INVALID_PARAMETER
<Sorgente C++> core/object/object.cpp:1402 @ connect()
palla.gd:11 @ _ready()
ball_spawner.gd:25 @ spawn_ball()

Select the node that is connected. On the right side, where the inspector is select the node tab. Right-click the connected signal and click disconnect.

Thanks. Still not working with this strategy. i gave up and inserted 4 raycasts on the ball (longer than the ball collider) and detecting raycast collision it works. Maybe not the best solution, and I would like to understand why the collision/body_entered approach doesn’t work , I think that the ball’s round bouncing collider interaction is considered something different than a collisions by the physics engine … but this is just my impression, I haven’t enough experience to be sure.

You need to activate contact monitors for rigidbody2d, this is a very common mistake that takes a lot of people by surprise.
It is disabled by default for performance reasons.

1 Like

Yes !! This is working or at least partially working. Still have to figure out why it detects just some contacts and not all, but looks like working. Thanks !