Skip to content

Commit da9e24d

Browse files
authored
Merge pull request #576 from nekomatata/physics-tests-broadphase-update
Update broadphase performance test in physics tests
2 parents 5618c2b + 1c1ad17 commit da9e24d

File tree

8 files changed

+66
-34
lines changed

8 files changed

+66
-34
lines changed

2d/physics_tests/project.godot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ toggle_pause={
7373
]
7474
}
7575

76+
[memory]
77+
78+
limits/message_queue/max_size_kb=10240
79+
7680
[rendering]
7781

7882
quality/driver/driver_name="GLES2"

2d/physics_tests/test.gd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ extends Node2D
44

55
signal wait_done()
66

7+
export var _enable_debug_collision = true
8+
79
var _timer
810
var _timer_started = false
911

@@ -21,6 +23,11 @@ class Circle2D:
2123
var _drawn_nodes = []
2224

2325

26+
func _enter_tree():
27+
if not _enable_debug_collision:
28+
get_tree().debug_collisions_hint = false
29+
30+
2431
func _physics_process(_delta):
2532
if _wait_physics_ticks_counter > 0:
2633
_wait_physics_ticks_counter -= 1

2d/physics_tests/tests/performance/test_perf_broadphase.gd

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,47 @@ var _log_physics_time_start = 0
1515

1616

1717
func _ready():
18-
_create_objects()
18+
yield(start_timer(1.0), "timeout")
19+
if is_timer_canceled():
20+
return
1921

2022
_log_physics_start()
23+
24+
_create_objects()
25+
2126
yield(wait_for_physics_ticks(5), "wait_done")
2227
_log_physics_stop()
2328

2429
yield(start_timer(1.0), "timeout")
2530
if is_timer_canceled():
2631
return
2732

33+
_log_physics_start()
34+
2835
_add_objects()
2936

30-
_log_physics_start()
3137
yield(wait_for_physics_ticks(5), "wait_done")
3238
_log_physics_stop()
3339

3440
yield(start_timer(1.0), "timeout")
3541
if is_timer_canceled():
3642
return
3743

44+
_log_physics_start()
45+
3846
_move_objects()
3947

40-
_log_physics_start()
4148
yield(wait_for_physics_ticks(5), "wait_done")
4249
_log_physics_stop()
4350

4451
yield(start_timer(1.0), "timeout")
4552
if is_timer_canceled():
4653
return
4754

55+
_log_physics_start()
56+
4857
_remove_objects()
4958

50-
_log_physics_start()
5159
yield(wait_for_physics_ticks(5), "wait_done")
5260
_log_physics_stop()
5361

@@ -85,9 +93,6 @@ func _log_physics_stop():
8593
func _create_objects():
8694
_objects.clear()
8795

88-
var template_body = create_rigidbody_box(BOX_SIZE)
89-
template_body.gravity_scale = 0.0
90-
9196
Log.print_log("* Creating objects...")
9297
var timer = OS.get_ticks_usec()
9398

@@ -97,9 +102,10 @@ func _create_objects():
97102
var pos_y = -0.5 * (column_size - 1) * BOX_SPACE.y
98103

99104
for column in column_size:
100-
var box = template_body.duplicate()
105+
# Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape.
106+
var box = create_rigidbody_box(BOX_SIZE)
107+
box.gravity_scale = 0.0
101108
box.position = Vector2(pos_x, pos_y)
102-
box.name = "Box%03d" % (row * column + 1)
103109
_objects.push_back(box)
104110

105111
pos_y += BOX_SPACE.y
@@ -109,8 +115,6 @@ func _create_objects():
109115
timer = OS.get_ticks_usec() - timer
110116
Log.print_log(" Create Time: %.3f ms" % (0.001 * timer))
111117

112-
template_body.queue_free()
113-
114118

115119
func _add_objects():
116120
var root_node = $Objects
@@ -142,8 +146,10 @@ func _remove_objects():
142146
Log.print_log("* Removing objects...")
143147
var timer = OS.get_ticks_usec()
144148

145-
for object in _objects:
146-
root_node.remove_child(object)
149+
# Remove objects in reversed order to avoid the overhead of changing children index in parent.
150+
var object_count = _objects.size()
151+
for object_index in object_count:
152+
root_node.remove_child(_objects[object_count - object_index - 1])
147153

148154
timer = OS.get_ticks_usec() - timer
149155
Log.print_log(" Remove Time: %.3f ms" % (0.001 * timer))

2d/physics_tests/tests/performance/test_perf_broadphase.tscn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
[node name="Test" type="Node2D"]
66
script = ExtResource( 1 )
7+
_enable_debug_collision = false
8+
row_size = 300
9+
column_size = 300
710

811
[node name="Objects" type="Node2D" parent="."]
912
position = Vector2( 512, 300 )

3d/physics_tests/main.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ margin_left = 157.0
4141
margin_top = 13.0
4242
margin_right = 375.0
4343
margin_bottom = 27.0
44-
text = "R - RESTART / D - TOGGLE COLLISION / F - TOGGLE FULL SCREEN / ESC - QUIT"
44+
text = "P - TOGGLE PAUSE / R - RESTART / D - TOGGLE COLLISION / F - TOGGLE FULL SCREEN / ESC - QUIT"
4545
__meta__ = {
4646
"_edit_use_anchors_": false
4747
}

3d/physics_tests/test.gd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ extends Node
44

55
signal wait_done()
66

7+
export var _enable_debug_collision = true
8+
79
var _timer
810
var _timer_started = false
911

@@ -12,6 +14,11 @@ var _wait_physics_ticks_counter = 0
1214
var _drawn_nodes = []
1315

1416

17+
func _enter_tree():
18+
if not _enable_debug_collision:
19+
get_tree().debug_collisions_hint = false
20+
21+
1522
func _physics_process(_delta):
1623
if _wait_physics_ticks_counter > 0:
1724
_wait_physics_ticks_counter -= 1
@@ -60,7 +67,7 @@ func clear_drawn_nodes():
6067
_drawn_nodes.clear()
6168

6269

63-
func create_rigidbody_box(size, pickable):
70+
func create_rigidbody_box(size, pickable = false):
6471
var shape = BoxShape.new()
6572
shape.extents = 0.5 * size
6673

3d/physics_tests/tests/performance/test_perf_broadphase.gd

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,47 @@ var _log_physics_time_start = 0
1616

1717

1818
func _ready():
19-
_create_objects()
19+
yield(start_timer(1.0), "timeout")
20+
if is_timer_canceled():
21+
return
2022

2123
_log_physics_start()
24+
25+
_create_objects()
26+
2227
yield(wait_for_physics_ticks(5), "wait_done")
2328
_log_physics_stop()
2429

2530
yield(start_timer(1.0), "timeout")
2631
if is_timer_canceled():
2732
return
2833

34+
_log_physics_start()
35+
2936
_add_objects()
3037

31-
_log_physics_start()
3238
yield(wait_for_physics_ticks(5), "wait_done")
3339
_log_physics_stop()
3440

3541
yield(start_timer(1.0), "timeout")
3642
if is_timer_canceled():
3743
return
3844

45+
_log_physics_start()
46+
3947
_move_objects()
4048

41-
_log_physics_start()
4249
yield(wait_for_physics_ticks(5), "wait_done")
4350
_log_physics_stop()
4451

4552
yield(start_timer(1.0), "timeout")
4653
if is_timer_canceled():
4754
return
4855

56+
_log_physics_start()
57+
4958
_remove_objects()
5059

51-
_log_physics_start()
5260
yield(wait_for_physics_ticks(5), "wait_done")
5361
_log_physics_stop()
5462

@@ -86,9 +94,6 @@ func _log_physics_stop():
8694
func _create_objects():
8795
_objects.clear()
8896

89-
var template_body = create_rigidbody_box(BOX_SIZE)
90-
template_body.gravity_scale = 0.0
91-
9297
Log.print_log("* Creating objects...")
9398
var timer = OS.get_ticks_usec()
9499

@@ -101,9 +106,10 @@ func _create_objects():
101106
var pos_z = -0.5 * (depth_size - 1) * BOX_SPACE.z
102107

103108
for depth in depth_size:
104-
var box = template_body.duplicate()
109+
# Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape.
110+
var box = create_rigidbody_box(BOX_SIZE)
111+
box.gravity_scale = 0.0
105112
box.transform.origin = Vector3(pos_x, pos_y, pos_z)
106-
box.name = "Box%03d" % (row * column + 1)
107113
_objects.push_back(box)
108114

109115
pos_z += BOX_SPACE.z
@@ -115,8 +121,6 @@ func _create_objects():
115121
timer = OS.get_ticks_usec() - timer
116122
Log.print_log(" Create Time: %.3f ms" % (0.001 * timer))
117123

118-
template_body.queue_free()
119-
120124

121125
func _add_objects():
122126
var root_node = $Objects
@@ -148,8 +152,10 @@ func _remove_objects():
148152
Log.print_log("* Removing objects...")
149153
var timer = OS.get_ticks_usec()
150154

151-
for object in _objects:
152-
root_node.remove_child(object)
155+
# Remove objects in reversed order to avoid the overhead of changing children index in parent.
156+
var object_count = _objects.size()
157+
for object_index in object_count:
158+
root_node.remove_child(_objects[object_count - object_index - 1])
153159

154160
timer = OS.get_ticks_usec() - timer
155161
Log.print_log(" Remove Time: %.3f ms" % (0.001 * timer))
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
[gd_scene load_steps=3 format=2]
1+
[gd_scene load_steps=2 format=2]
22

33
[ext_resource path="res://tests/performance/test_perf_broadphase.gd" type="Script" id=1]
4-
[ext_resource path="res://utils/camera_orbit.gd" type="Script" id=5]
54

65
[node name="Test" type="Spatial"]
76
script = ExtResource( 1 )
7+
_enable_debug_collision = false
8+
row_size = 50
9+
column_size = 50
10+
depth_size = 50
811

912
[node name="Objects" type="Spatial" parent="."]
10-
11-
[node name="Camera" type="Camera" parent="."]
12-
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 29.8407 )
13-
script = ExtResource( 5 )

0 commit comments

Comments
 (0)