Skip to content

Commit e5d829e

Browse files
authored
Merge pull request #613 from nekomatata/physics-tests-contacts-update
Update contact tests in physics tests for 2D & 3D
2 parents 5132941 + 7f025e0 commit e5d829e

File tree

9 files changed

+172
-48
lines changed

9 files changed

+172
-48
lines changed

2d/physics_tests/test.gd

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,6 @@ func create_rigidbody(shape, pickable = false, transform = Transform.IDENTITY):
8383
return body
8484

8585

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-
10386
func create_rigidbody_box(size, pickable = false, use_icon = false, transform = Transform.IDENTITY):
10487
var shape = RectangleShape2D.new()
10588
shape.extents = 0.5 * size

2d/physics_tests/tests.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ var _tests = [
5050
"id": "Performance Tests/Contacts",
5151
"path": "res://tests/performance/test_perf_contacts.tscn",
5252
},
53+
{
54+
"id" : "Performance Tests/Contact Islands",
55+
"path" : "res://tests/performance/test_perf_contact_islands.tscn",
56+
},
5357
]
5458

5559

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
[gd_scene load_steps=8 format=2]
2+
3+
[ext_resource path="res://tests/static_scene.tscn" type="PackedScene" id=1]
4+
[ext_resource path="res://tests/performance/test_perf_contacts.gd" type="Script" id=2]
5+
[ext_resource path="res://assets/texture/godot-head.png" type="Texture" id=3]
6+
[ext_resource path="res://tests/test_options.tscn" type="PackedScene" id=4]
7+
8+
[sub_resource type="RectangleShape2D" id=1]
9+
extents = Vector2( 5, 7 )
10+
11+
[sub_resource type="CircleShape2D" id=2]
12+
radius = 5.0
13+
14+
[sub_resource type="CapsuleShape2D" id=3]
15+
radius = 5.0
16+
height = 7.0
17+
18+
[node name="Test" type="Node2D"]
19+
script = ExtResource( 2 )
20+
_enable_debug_collision = false
21+
spawns = [ NodePath("SpawnTarget1"), NodePath("SpawnTarget2"), NodePath("SpawnTarget3"), NodePath("SpawnTarget4"), NodePath("SpawnTarget5"), NodePath("SpawnTarget6"), NodePath("SpawnTarget7"), NodePath("SpawnTarget8"), NodePath("SpawnTarget9") ]
22+
spawn_count = 300
23+
spawn_randomize = Vector2( 10, 10 )
24+
25+
[node name="Options" parent="." instance=ExtResource( 4 )]
26+
27+
[node name="SpawnTarget1" type="Node2D" parent="."]
28+
position = Vector2( 145.646, 109.462 )
29+
30+
[node name="SpawnTarget2" type="Node2D" parent="."]
31+
position = Vector2( 508.14, 109.113 )
32+
33+
[node name="SpawnTarget3" type="Node2D" parent="."]
34+
position = Vector2( 873.995, 110.042 )
35+
36+
[node name="SpawnTarget4" type="Node2D" parent="."]
37+
position = Vector2( 149.646, 301.462 )
38+
39+
[node name="SpawnTarget5" type="Node2D" parent="."]
40+
position = Vector2( 512.14, 301.113 )
41+
42+
[node name="SpawnTarget6" type="Node2D" parent="."]
43+
position = Vector2( 877.995, 302.042 )
44+
45+
[node name="SpawnTarget7" type="Node2D" parent="."]
46+
position = Vector2( 165.646, 507.462 )
47+
48+
[node name="SpawnTarget8" type="Node2D" parent="."]
49+
position = Vector2( 528.14, 507.113 )
50+
51+
[node name="SpawnTarget9" type="Node2D" parent="."]
52+
position = Vector2( 893.995, 508.042 )
53+
54+
[node name="StaticScene" parent="." instance=ExtResource( 1 )]
55+
visible = false
56+
position = Vector2( 0, 125.017 )
57+
58+
[node name="DynamicShapes" type="Node2D" parent="."]
59+
60+
[node name="RigidBodyRectangle" type="RigidBody2D" parent="DynamicShapes"]
61+
position = Vector2( 0, 1024 )
62+
gravity_scale = 0.0
63+
64+
[node name="CollisionShape2D" type="CollisionShape2D" parent="DynamicShapes/RigidBodyRectangle"]
65+
shape = SubResource( 1 )
66+
67+
[node name="RigidBodySphere" type="RigidBody2D" parent="DynamicShapes"]
68+
position = Vector2( 100, 1024 )
69+
gravity_scale = 0.0
70+
71+
[node name="CollisionShape2D" type="CollisionShape2D" parent="DynamicShapes/RigidBodySphere"]
72+
shape = SubResource( 2 )
73+
74+
[node name="RigidBodyCapsule" type="RigidBody2D" parent="DynamicShapes"]
75+
position = Vector2( 200, 1024 )
76+
gravity_scale = 0.0
77+
78+
[node name="CollisionShape2D" type="CollisionShape2D" parent="DynamicShapes/RigidBodyCapsule"]
79+
shape = SubResource( 3 )
80+
81+
[node name="RigidBodyConvexPolygon" type="RigidBody2D" parent="DynamicShapes"]
82+
position = Vector2( 300, 1024 )
83+
gravity_scale = 0.0
84+
85+
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="DynamicShapes/RigidBodyConvexPolygon"]
86+
scale = Vector2( 0.1, 0.1 )
87+
polygon = PoolVector2Array( 10.7, -54.5, 28.3596, -49.4067, 47.6282, -34.3806, 57.9717, -20.9447, 50.9869, 35.2694, 38.8, 47.5, 15.9852, 54.3613, -14.9507, 54.1845, -36.5, 48.1, -50.4828, 36.33, -58.0115, -20.515, -46.9473, -34.7342, -26.0876, -50.1138, -11.4152, -54.5332 )
88+
89+
[node name="GodotIcon" type="Sprite" parent="DynamicShapes/RigidBodyConvexPolygon"]
90+
self_modulate = Color( 1, 1, 1, 0.392157 )
91+
scale = Vector2( 0.1, 0.1 )
92+
texture = ExtResource( 3 )
93+
94+
[node name="RigidBodyConcavePolygon" type="RigidBody2D" parent="DynamicShapes"]
95+
position = Vector2( 400, 1024 )
96+
gravity_scale = 0.0
97+
98+
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="DynamicShapes/RigidBodyConcavePolygon"]
99+
scale = Vector2( 0.1, 0.1 )
100+
polygon = PoolVector2Array( -5.93512, -43.2195, 6.44476, -42.9695, 11.127, -54.3941, 26.9528, -49.4309, 26.2037, -36.508, 37.5346, -28.1737, 47.6282, -34.3806, 58.0427, -20.9631, 51.113, -10.2876, 50.9869, 35.2694, 38.8, 47.5, 15.9852, 54.3613, -14.9507, 54.1845, -36.5, 48.1, -50.4828, 36.33, -51.3668, -9.98545, -57.8889, -20.5885, -46.9473, -34.7342, -37.4014, -28.547, -26.0876, -37.0323, -26.9862, -49.15, -11.4152, -54.5332 )
101+
102+
[node name="GodotIcon" type="Sprite" parent="DynamicShapes/RigidBodyConcavePolygon"]
103+
self_modulate = Color( 1, 1, 1, 0.392157 )
104+
scale = Vector2( 0.1, 0.1 )
105+
texture = ExtResource( 3 )

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const OPTION_TYPE_CAPSULE = "Shape type/Capsule"
88
const OPTION_TYPE_CONVEX_POLYGON = "Shape type/Convex Polygon"
99
const OPTION_TYPE_CONCAVE_POLYGON = "Shape type/Concave Polygon"
1010

11-
export(Array) var spawns = Array()
11+
export(Array, NodePath) var spawns = Array()
1212
export(int) var spawn_count = 100
13+
export(Vector2) var spawn_randomize
1314

1415
onready var options = $Options
1516

@@ -152,40 +153,51 @@ func _start_all_types():
152153

153154
func _spawn_objects(type_index):
154155
var template_node = _object_templates[type_index]
156+
157+
Log.print_log("* Spawning: " + template_node.name)
158+
155159
for spawn in spawns:
156160
var spawn_parent = get_node(spawn)
157161

158-
Log.print_log("* Spawning: " + template_node.name)
159-
160162
for _node_index in range(spawn_count):
161163
# 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+
var collision = template_node.get_child(0).duplicate()
165+
if collision is CollisionShape2D:
166+
collision.shape = collision.shape.duplicate()
167+
var body = template_node.duplicate()
168+
body.transform = Transform.IDENTITY
169+
if spawn_randomize != Vector2.ZERO:
170+
body.position.x = randf() * spawn_randomize.x
171+
body.position.y = randf() * spawn_randomize.y
172+
var prev_collision = body.get_child(0)
173+
body.remove_child(prev_collision)
174+
prev_collision.queue_free()
175+
body.add_child(collision)
164176
body.set_sleeping(true)
165177
spawn_parent.add_child(body)
166178

167179

168180
func _activate_objects():
181+
Log.print_log("* Activating")
182+
169183
for spawn in spawns:
170184
var spawn_parent = get_node(spawn)
171185

172-
Log.print_log("* Activating")
173-
174186
for node_index in range(spawn_parent.get_child_count()):
175187
var node = spawn_parent.get_child(node_index) as RigidBody2D
176188
node.set_sleeping(false)
177189

178190

179191
func _despawn_objects():
192+
Log.print_log("* Despawning")
193+
180194
for spawn in spawns:
181195
var spawn_parent = get_node(spawn)
182196

183197
var object_count = spawn_parent.get_child_count()
184198
if object_count == 0:
185199
continue
186200

187-
Log.print_log("* Despawning")
188-
189201
# Remove objects in reversed order to avoid the overhead of changing children index in parent.
190202
for object_index in range(object_count):
191203
var node = spawn_parent.get_child(object_count - object_index - 1)

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
[ext_resource path="res://tests/test_options.tscn" type="PackedScene" id=4]
77

88
[sub_resource type="RectangleShape2D" id=1]
9-
extents = Vector2( 20, 30 )
9+
extents = Vector2( 10, 15 )
1010

1111
[sub_resource type="CircleShape2D" id=2]
12-
radius = 30.0
12+
radius = 15.0
1313

1414
[sub_resource type="CapsuleShape2D" id=3]
15-
radius = 20.0
16-
height = 30.0
15+
height = 15.0
1716

1817
[node name="Test" type="Node2D"]
1918
script = ExtResource( 2 )
2019
_enable_debug_collision = false
2120
spawns = [ NodePath("SpawnTarget1") ]
22-
spawn_count = 200
21+
spawn_count = 500
22+
spawn_randomize = Vector2( 10, 10 )
2323

2424
[node name="Options" parent="." instance=ExtResource( 4 )]
2525

@@ -53,22 +53,22 @@ shape = SubResource( 3 )
5353
position = Vector2( 300, 1024 )
5454

5555
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="DynamicShapes/RigidBodyConvexPolygon"]
56-
scale = Vector2( 0.5, 0.5 )
56+
scale = Vector2( 0.25, 0.25 )
5757
polygon = PoolVector2Array( 10.7, -54.5, 28.3596, -49.4067, 47.6282, -34.3806, 57.9717, -20.9447, 50.9869, 35.2694, 38.8, 47.5, 15.9852, 54.3613, -14.9507, 54.1845, -36.5, 48.1, -50.4828, 36.33, -58.0115, -20.515, -46.9473, -34.7342, -26.0876, -50.1138, -11.4152, -54.5332 )
5858

5959
[node name="GodotIcon" type="Sprite" parent="DynamicShapes/RigidBodyConvexPolygon"]
6060
self_modulate = Color( 1, 1, 1, 0.392157 )
61-
scale = Vector2( 0.5, 0.5 )
61+
scale = Vector2( 0.25, 0.25 )
6262
texture = ExtResource( 3 )
6363

6464
[node name="RigidBodyConcavePolygon" type="RigidBody2D" parent="DynamicShapes"]
6565
position = Vector2( 400, 1024 )
6666

6767
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="DynamicShapes/RigidBodyConcavePolygon"]
68-
scale = Vector2( 0.5, 0.5 )
68+
scale = Vector2( 0.25, 0.25 )
6969
polygon = PoolVector2Array( -5.93512, -43.2195, 6.44476, -42.9695, 11.127, -54.3941, 26.9528, -49.4309, 26.2037, -36.508, 37.5346, -28.1737, 47.6282, -34.3806, 58.0427, -20.9631, 51.113, -10.2876, 50.9869, 35.2694, 38.8, 47.5, 15.9852, 54.3613, -14.9507, 54.1845, -36.5, 48.1, -50.4828, 36.33, -51.3668, -9.98545, -57.8889, -20.5885, -46.9473, -34.7342, -37.4014, -28.547, -26.0876, -37.0323, -26.9862, -49.15, -11.4152, -54.5332 )
7070

7171
[node name="GodotIcon" type="Sprite" parent="DynamicShapes/RigidBodyConcavePolygon"]
7272
self_modulate = Color( 1, 1, 1, 0.392157 )
73-
scale = Vector2( 0.5, 0.5 )
73+
scale = Vector2( 0.25, 0.25 )
7474
texture = ExtResource( 3 )

3d/physics_tests/tests.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ var _tests = [
4343
"path": "res://tests/performance/test_perf_contacts.tscn",
4444
},
4545
{
46-
"id" : "Performance Tests/Contacts Extended",
47-
"path" : "res://tests/performance/test_perf_contacts_extended.tscn",
46+
"id" : "Performance Tests/Contact Islands",
47+
"path" : "res://tests/performance/test_perf_contact_islands.tscn",
4848
},
4949
]
5050

3d/physics_tests/tests/performance/test_perf_contacts_extended.tscn renamed to 3d/physics_tests/tests/performance/test_perf_contact_islands.tscn

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,30 @@
66
[ext_resource path="res://tests/static_scene.tscn" type="PackedScene" id=5]
77

88
[sub_resource type="BoxShape" id=1]
9+
extents = Vector3( 0.5, 0.5, 0.5 )
910

1011
[sub_resource type="CapsuleShape" id=2]
12+
radius = 0.5
13+
height = 0.5
1114

1215
[sub_resource type="CylinderShape" id=3]
16+
radius = 0.5
17+
height = 1.0
1318

1419
[sub_resource type="ConvexPolygonShape" id=4]
1520
points = PoolVector3Array( -0.7, 0, -0.7, -0.3, 0, 0.8, 0.8, 0, -0.3, 0, -1, 0 )
1621

1722
[sub_resource type="SphereShape" id=5]
23+
radius = 0.5
1824

1925
[sub_resource type="PlaneShape" id=6]
2026

2127
[node name="Test" type="Spatial"]
2228
script = ExtResource( 1 )
2329
_enable_debug_collision = false
2430
spawns = [ "SpawnTarget1", "SpawnTarget2", "SpawnTarget3", "SpawnTarget4", "SpawnTarget5", "SpawnTarget6", "SpawnTarget7", "SpawnTarget8", "SpawnTarget9", "SpawnTarget10", "SpawnTarget11", "SpawnTarget12", "SpawnTarget13", "SpawnTarget14", "SpawnTarget15", "SpawnTarget16" ]
25-
spawn_count = 50
31+
spawn_count = 200
32+
spawn_randomize = Vector3( 0.2, 0.2, 0.2 )
2633

2734
[node name="Options" parent="." instance=ExtResource( 4 )]
2835

@@ -98,6 +105,7 @@ shape = SubResource( 3 )
98105
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 6, 0, 0 )
99106

100107
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyConvex"]
108+
transform = Transform( 0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0 )
101109
shape = SubResource( 4 )
102110

103111
[node name="RigidBodySphere" type="RigidBody" parent="DynamicShapes"]

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const OPTION_TYPE_CAPSULE = "Shape type/Capsule"
88
const OPTION_TYPE_CYLINDER = "Shape type/Cylinder"
99
const OPTION_TYPE_CONVEX = "Shape type/Convex"
1010

11-
export(Array) var spawns = Array()
11+
export(Array, NodePath) var spawns = Array()
1212
export(int) var spawn_count = 100
13+
export(Vector3) var spawn_randomize
1314

1415
var _object_templates = []
1516

@@ -148,37 +149,47 @@ func _start_all_types():
148149

149150
func _spawn_objects(type_index):
150151
var template_node = _object_templates[type_index]
152+
153+
Log.print_log("* Spawning: " + template_node.name)
154+
151155
for spawn in spawns:
152156
var spawn_parent = get_node(spawn)
153157

154-
Log.print_log("* Spawning: " + template_node.name)
155-
156158
for _node_index in range(spawn_count):
157159
# Create a new object and shape every time to avoid the overhead of connecting many bodies to the same shape.
158-
var collision = template_node.get_child(0) as CollisionShape
159-
var shape = collision.shape.duplicate()
160-
var body = create_rigidbody(shape, false, collision.transform)
160+
var collision = template_node.get_child(0).duplicate()
161+
collision.shape = collision.shape.duplicate()
162+
var body = template_node.duplicate()
163+
body.transform = Transform.IDENTITY
164+
if spawn_randomize != Vector3.ZERO:
165+
body.transform.origin.x = randf() * spawn_randomize.x
166+
body.transform.origin.y = randf() * spawn_randomize.y
167+
body.transform.origin.z = randf() * spawn_randomize.z
168+
var prev_collision = body.get_child(0)
169+
body.remove_child(prev_collision)
170+
prev_collision.queue_free()
171+
body.add_child(collision)
161172
body.set_sleeping(true)
162173
spawn_parent.add_child(body)
163174

164175

165176
func _activate_objects():
177+
Log.print_log("* Activating")
178+
166179
for spawn in spawns:
167180
var spawn_parent = get_node(spawn)
168181

169-
Log.print_log("* Activating")
170-
171182
for node_index in range(spawn_parent.get_child_count()):
172183
var node = spawn_parent.get_child(node_index) as RigidBody
173184
node.set_sleeping(false)
174185

175186

176187
func _despawn_objects():
188+
Log.print_log("* Despawning")
189+
177190
for spawn in spawns:
178191
var spawn_parent = get_node(spawn)
179192

180-
Log.print_log("* Despawning")
181-
182193
# Remove objects in reversed order to avoid the overhead of changing children index in parent.
183194
var object_count = spawn_parent.get_child_count()
184195
for object_index in range(object_count):

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ script = ExtResource( 2 )
2323
_enable_debug_collision = false
2424
spawns = [ "SpawnTarget1" ]
2525
spawn_count = 500
26+
spawn_randomize = Vector3( 0.2, 0.2, 0.2 )
2627

2728
[node name="Options" parent="." instance=ExtResource( 4 )]
2829

0 commit comments

Comments
 (0)