123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- extends Test
- const BOX_SIZE = Vector2(40, 40)
- const BOX_SPACE = Vector2(50, 50)
- @export_range(1, 1000) var row_size = 100
- @export_range(1, 1000) var column_size = 100
- var _objects = []
- var _log_physics = false
- var _log_physics_time = 0
- var _log_physics_time_start = 0
- func _ready():
- await start_timer(1.0).timeout
- if is_timer_canceled():
- return
- _log_physics_start()
- _create_objects()
- await wait_for_physics_ticks(5).wait_done
- _log_physics_stop()
- await start_timer(1.0).timeout
- if is_timer_canceled():
- return
- _log_physics_start()
- _add_objects()
- await wait_for_physics_ticks(5).wait_done
- _log_physics_stop()
- await start_timer(1.0).timeout
- if is_timer_canceled():
- return
- _log_physics_start()
- _move_objects()
- await wait_for_physics_ticks(5).wait_done
- _log_physics_stop()
- await start_timer(1.0).timeout
- if is_timer_canceled():
- return
- _log_physics_start()
- _remove_objects()
- await wait_for_physics_ticks(5).wait_done
- _log_physics_stop()
- await start_timer(1.0).timeout
- if is_timer_canceled():
- return
- Log.print_log("* Done.")
- func _exit_tree():
- for object in _objects:
- object.free()
- func _physics_process(delta):
- super._physics_process(delta)
- if _log_physics:
- var time = Time.get_ticks_usec()
- var time_delta = time - _log_physics_time
- var time_total = time - _log_physics_time_start
- _log_physics_time = time
- Log.print_log(" Physics Tick: %.3f ms (total = %.3f ms)" % [0.001 * time_delta, 0.001 * time_total])
- func _log_physics_start():
- _log_physics = true
- _log_physics_time_start = Time.get_ticks_usec()
- _log_physics_time = _log_physics_time_start
- func _log_physics_stop():
- _log_physics = false
- func _create_objects():
- _objects.clear()
- Log.print_log("* Creating objects...")
- var timer = Time.get_ticks_usec()
- var pos_x = -0.5 * (row_size - 1) * BOX_SPACE.x
- for row in row_size:
- var pos_y = -0.5 * (column_size - 1) * BOX_SPACE.y
- for column in column_size:
- # Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape.
- var box = create_rigidbody_box(BOX_SIZE)
- box.gravity_scale = 0.0
- box.position = Vector2(pos_x, pos_y)
- _objects.push_back(box)
- pos_y += BOX_SPACE.y
- pos_x += BOX_SPACE.x
- timer = Time.get_ticks_usec() - timer
- Log.print_log(" Create Time: %.3f ms" % (0.001 * timer))
- func _add_objects():
- var root_node = $Objects
- Log.print_log("* Adding objects...")
- var timer = Time.get_ticks_usec()
- for object in _objects:
- root_node.add_child(object)
- timer = Time.get_ticks_usec() - timer
- Log.print_log(" Add Time: %.3f ms" % (0.001 * timer))
- func _move_objects():
- Log.print_log("* Moving objects...")
- var timer = Time.get_ticks_usec()
- for object in _objects:
- object.position += BOX_SPACE
- timer = Time.get_ticks_usec() - timer
- Log.print_log(" Move Time: %.3f ms" % (0.001 * timer))
- func _remove_objects():
- var root_node = $Objects
- Log.print_log("* Removing objects...")
- var timer = Time.get_ticks_usec()
- # Remove objects in reversed order to avoid the overhead of changing children index in parent.
- var object_count = _objects.size()
- for object_index in range(object_count):
- root_node.remove_child(_objects[object_count - object_index - 1])
- timer = Time.get_ticks_usec() - timer
- Log.print_log(" Remove Time: %.3f ms" % (0.001 * timer))
|