Skip to content

Commit ead2777

Browse files
authored
Merge pull request #606 from nekomatata/physics-tests-contacts-update
Updated 2d/3d physics contact performance tests
2 parents 4911866 + 0a0d44d commit ead2777

File tree

17 files changed

+203
-112
lines changed

17 files changed

+203
-112
lines changed

2d/physics_tests/test.gd

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ func clear_drawn_nodes():
6868
_drawn_nodes.clear()
6969

7070

71-
func create_rigidbody_box(size, pickable = false, use_icon = false):
72-
var shape = RectangleShape2D.new()
73-
shape.extents = 0.5 * size
74-
71+
func create_rigidbody(shape, pickable = false, transform = Transform.IDENTITY):
7572
var collision = CollisionShape2D.new()
7673
collision.shape = shape
74+
collision.transform = transform
7775

7876
var body = RigidBody2D.new()
7977
body.add_child(collision)
@@ -82,6 +80,32 @@ func create_rigidbody_box(size, pickable = false, use_icon = false):
8280
var script = load("res://utils/rigidbody_pick.gd")
8381
body.set_script(script)
8482

83+
return body
84+
85+
86+
func create_rigidbody_collision(collision, pickable = false, transform = Transform.IDENTITY):
87+
var collision_copy = collision.duplicate()
88+
collision_copy.transform = transform
89+
90+
if collision is CollisionShape2D:
91+
collision_copy.shape = collision.shape.duplicate()
92+
93+
var body = RigidBody2D.new()
94+
body.add_child(collision_copy)
95+
96+
if pickable:
97+
var script = load("res://utils/rigidbody_pick.gd")
98+
body.set_script(script)
99+
100+
return body
101+
102+
103+
func create_rigidbody_box(size, pickable = false, use_icon = false, transform = Transform.IDENTITY):
104+
var shape = RectangleShape2D.new()
105+
shape.extents = 0.5 * size
106+
107+
var body = create_rigidbody(shape, pickable, transform)
108+
85109
if use_icon:
86110
var texture = load("res://icon.png")
87111
var icon = Sprite.new()

2d/physics_tests/tests/functional/test_collision_pairs.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func _on_option_selected(option):
167167

168168

169169
func _find_type_index(type_name):
170-
for type_index in _collision_shapes.size():
170+
for type_index in range(_collision_shapes.size()):
171171
var type_shape = _collision_shapes[type_index]
172172
if type_shape.resource_name.find(type_name) > -1:
173173
return type_index

2d/physics_tests/tests/functional/test_joints.gd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const OPTION_TEST_CASE_CHANGE_POSITIONS = "Test case/Set body positions after ad
1111

1212
const BOX_SIZE = Vector2(64, 64)
1313

14-
onready var options = $Options
15-
1614
var _update_joint = false
1715
var _selected_joint = null
1816

@@ -27,8 +25,10 @@ var _joint_types = {}
2725

2826

2927
func _ready():
28+
var options = $Options
29+
3030
var joints = $Joints
31-
for joint_index in joints.get_child_count():
31+
for joint_index in range(joints.get_child_count()):
3232
var joint_node = joints.get_child(joint_index)
3333
joint_node.visible = false
3434
var joint_name = joint_node.name

2d/physics_tests/tests/functional/test_pyramid.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func _create_pyramid():
2828

2929
var pos_x = -0.5 * (num_boxes - 1) * (box_size.x + box_spacing.x)
3030

31-
for box_index in num_boxes:
31+
for box_index in range(num_boxes):
3232
var box = template_body.duplicate()
3333
box.position = Vector2(pos_x, 0.0)
3434
box.name = "Box%02d" % (box_index + 1)

2d/physics_tests/tests/functional/test_stack.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func _create_stack():
2626

2727
var pos_x = -0.5 * (width - 1) * (box_size.x + box_spacing.x)
2828

29-
for box_index in width:
29+
for box_index in range(width):
3030
var box = template_body.duplicate()
3131
box.position = Vector2(pos_x, 0.0)
3232
box.name = "Box%02d" % (box_index + 1)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func _remove_objects():
148148

149149
# Remove objects in reversed order to avoid the overhead of changing children index in parent.
150150
var object_count = _objects.size()
151-
for object_index in object_count:
151+
for object_index in range(object_count):
152152
root_node.remove_child(_objects[object_count - object_index - 1])
153153

154154
timer = OS.get_ticks_usec() - timer

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

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ const OPTION_TYPE_CONCAVE_POLYGON = "Shape type/Concave Polygon"
1010

1111
export(Array) var spawns = Array()
1212
export(int) var spawn_count = 100
13-
export(int, 1, 10) var spawn_multiplier = 5
1413

1514
onready var options = $Options
1615

1716
var _object_templates = []
1817

18+
var _log_physics = false
19+
var _log_physics_time = 0
20+
var _log_physics_time_start = 0
21+
1922

2023
func _ready():
2124
yield(start_timer(0.5), "timeout")
@@ -40,6 +43,25 @@ func _ready():
4043
_start_all_types()
4144

4245

46+
func _physics_process(_delta):
47+
if _log_physics:
48+
var time = OS.get_ticks_usec()
49+
var time_delta = time - _log_physics_time
50+
var time_total = time - _log_physics_time_start
51+
_log_physics_time = time
52+
Log.print_log(" Physics Tick: %.3f ms (total = %.3f ms)" % [0.001 * time_delta, 0.001 * time_total])
53+
54+
55+
func _log_physics_start():
56+
_log_physics = true
57+
_log_physics_time_start = OS.get_ticks_usec()
58+
_log_physics_time = _log_physics_time_start
59+
60+
61+
func _log_physics_stop():
62+
_log_physics = false
63+
64+
4365
func _exit_tree():
4466
for object_template in _object_templates:
4567
object_template.free()
@@ -66,7 +88,7 @@ func _on_option_selected(option):
6688

6789

6890
func _find_type_index(type_name):
69-
for type_index in _object_templates.size():
91+
for type_index in range(_object_templates.size()):
7092
var type_node = _object_templates[type_index]
7193
if type_node.name.find(type_name) > -1:
7294
return type_index
@@ -85,44 +107,47 @@ func _start_type(type_index):
85107
if is_timer_canceled():
86108
return
87109

110+
_log_physics_start()
111+
88112
_spawn_objects(type_index)
89113

114+
yield(wait_for_physics_ticks(5), "wait_done")
115+
_log_physics_stop()
116+
90117
yield(start_timer(1.0), "timeout")
91118
if is_timer_canceled():
92119
return
93120

121+
_log_physics_start()
122+
94123
_activate_objects()
95124

125+
yield(wait_for_physics_ticks(5), "wait_done")
126+
_log_physics_stop()
127+
96128
yield(start_timer(5.0), "timeout")
97129
if is_timer_canceled():
98130
return
99131

100-
_despawn_objects()
101-
102-
Log.print_log("* Done.")
132+
_log_physics_start()
103133

134+
_despawn_objects()
104135

105-
func _start_all_types():
106-
for type_index in _object_templates.size():
107-
yield(start_timer(1.0), "timeout")
108-
if is_timer_canceled():
109-
return
136+
yield(wait_for_physics_ticks(5), "wait_done")
137+
_log_physics_stop()
110138

111-
_spawn_objects(type_index)
139+
yield(start_timer(1.0), "timeout")
112140

113-
yield(start_timer(1.0), "timeout")
114-
if is_timer_canceled():
115-
return
116141

117-
_activate_objects()
142+
func _start_all_types():
143+
Log.print_log("* Start all types.")
118144

119-
yield(start_timer(5.0), "timeout")
145+
for type_index in range(_object_templates.size()):
146+
yield(_start_type(type_index), "completed")
120147
if is_timer_canceled():
121148
return
122149

123-
_despawn_objects()
124-
125-
Log.print_log("* Done.")
150+
Log.print_log("* Done all types.")
126151

127152

128153
func _spawn_objects(type_index):
@@ -132,33 +157,37 @@ func _spawn_objects(type_index):
132157

133158
Log.print_log("* Spawning: " + template_node.name)
134159

135-
for _index in range(spawn_multiplier):
136-
for _node_index in spawn_count / spawn_multiplier:
137-
var node = template_node.duplicate() as Node2D
138-
spawn_parent.add_child(node)
160+
for _node_index in range(spawn_count):
161+
# Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape.
162+
var collision = template_node.get_child(0)
163+
var body = create_rigidbody_collision(collision, false, collision.transform)
164+
body.set_sleeping(true)
165+
spawn_parent.add_child(body)
139166

140167

141168
func _activate_objects():
142-
var spawn_parent = $SpawnTarget1
169+
for spawn in spawns:
170+
var spawn_parent = get_node(spawn)
143171

144-
Log.print_log("* Activating")
172+
Log.print_log("* Activating")
145173

146-
for node_index in spawn_parent.get_child_count():
147-
var node = spawn_parent.get_child(node_index) as RigidBody2D
148-
node.set_sleeping(false)
174+
for node_index in range(spawn_parent.get_child_count()):
175+
var node = spawn_parent.get_child(node_index) as RigidBody2D
176+
node.set_sleeping(false)
149177

150178

151179
func _despawn_objects():
152180
for spawn in spawns:
153181
var spawn_parent = get_node(spawn)
154182

155-
if spawn_parent.get_child_count() == 0:
156-
return
183+
var object_count = spawn_parent.get_child_count()
184+
if object_count == 0:
185+
continue
157186

158187
Log.print_log("* Despawning")
159188

160-
while spawn_parent.get_child_count():
161-
var node_index = spawn_parent.get_child_count() - 1
162-
var node = spawn_parent.get_child(node_index)
189+
# Remove objects in reversed order to avoid the overhead of changing children index in parent.
190+
for object_index in range(object_count):
191+
var node = spawn_parent.get_child(object_count - object_index - 1)
163192
spawn_parent.remove_child(node)
164193
node.queue_free()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ height = 30.0
1717

1818
[node name="Test" type="Node2D"]
1919
script = ExtResource( 2 )
20+
_enable_debug_collision = false
2021
spawns = [ NodePath("SpawnTarget1") ]
22+
spawn_count = 200
2123

2224
[node name="Options" parent="." instance=ExtResource( 4 )]
2325

2d/physics_tests/utils/option_menu.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func add_menu_item(item_path, checkbox = false, checked = false, radio = false):
1313

1414
var path = ""
1515
var popup = get_popup()
16-
for element_index in path_element_count - 1:
16+
for element_index in range(path_element_count - 1):
1717
var popup_label = path_elements[element_index]
1818
path += popup_label + "/"
1919
popup = _add_popup(popup, path, popup_label)
@@ -59,7 +59,7 @@ func _on_item_pressed(item_index, popup_menu, path):
5959
var checked = popup_menu.is_item_checked(item_index)
6060
if not checked:
6161
popup_menu.set_item_checked(item_index, true)
62-
for other_index in popup_menu.get_item_count():
62+
for other_index in range(popup_menu.get_item_count()):
6363
if other_index != item_index:
6464
popup_menu.set_item_checked(other_index, false)
6565
emit_signal("option_selected", item_path)

3d/physics_tests/test.gd

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ func clear_drawn_nodes():
6767
_drawn_nodes.clear()
6868

6969

70-
func create_rigidbody_box(size, pickable = false):
71-
var shape = BoxShape.new()
72-
shape.extents = 0.5 * size
73-
70+
func create_rigidbody(shape, pickable = false, transform = Transform.IDENTITY):
7471
var collision = CollisionShape.new()
7572
collision.shape = shape
73+
collision.transform = transform
7674

7775
var body = RigidBody.new()
7876
body.add_child(collision)
@@ -84,6 +82,13 @@ func create_rigidbody_box(size, pickable = false):
8482
return body
8583

8684

85+
func create_rigidbody_box(size, pickable = false, transform = Transform.IDENTITY):
86+
var shape = BoxShape.new()
87+
shape.extents = 0.5 * size
88+
89+
return create_rigidbody(shape, pickable, transform)
90+
91+
8792
func start_timer(timeout):
8893
if _timer == null:
8994
_timer = Timer.new()

0 commit comments

Comments
 (0)