Understanding 3D physics frame and physics phases on Godot

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

I’m diving into the tests world in godot and everything is so far so good. But I have a question.

When I need to test somethings that uses physics I usually do the following steps:

  1. Setup tests
  2. Wait for simulation
  3. Check results

An example:

func test_get_interaction_area_bodies():
  # SETUP
  # This is just a method to create a cube and add collisions based on the masks and layers
  var wall = TestUtils.create_static_body_cube(Vector3(0, 0, -1.5), [], [0])    
  # This is just a method to add child and free it on the end of the test
  self.add_child_autofree(wall) 

  # WAIT
  # This is what is confusing me
  yield(get_tree(), "physics_frame")

  # CHECK
  var bodies := main_player.get_interaction_area_bodies() # calls area.get_overlapping_bodies()
  assert_that(bodies).is_not_null().is_not_empty()

My questions is just on the “wait for simulation” step, because I didn’t understood well how godot handle physics simulation. I know it have a PhysicsServer and every X ms it runs the physics step, before the broad phase and narrow phase and so on. But, if that was the case, just waiting for the next “physics_frame” should be enough to handle all collision detection, since I’m not moving anything, just placing them and checking of overlaps.

The problem is, some times I need to chain the yield 2, 3 or even 5 times in order to the simulation actually happens. The above example won’t work, I need to change the waiting step to:

  yield(get_tree(), "physics_frame")
  yield(get_tree(), "physics_frame")
  yield(get_tree(), "physics_frame")

Anyone knows why I have to wait for 3 or more physics frames in order to a single overlapping bodies simulation happens?