Skip to content

Commit 57f8628

Browse files
authored
Update impact, rigid groundcheck, moving platform (#634)
1 parent a4e8231 commit 57f8628

File tree

5 files changed

+455
-45
lines changed

5 files changed

+455
-45
lines changed

3d/physics_tests/tests/functional/test_moving_platform.gd

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ const OPTION_FRICTION = "Physics options/Friction (Rigid only)"
1010
const OPTION_ROUGH = "Physics options/Rough (Rigid only)"
1111
const OPTION_PROCESS_PHYSICS = "Physics options/AnimationPlayer physics process mode"
1212

13+
const SHAPE_CAPSULE = "Collision shapes/Capsule"
14+
const SHAPE_BOX = "Collision shapes/Box"
15+
const SHAPE_CYLINDER = "Collision shapes/Cylinder"
16+
const SHAPE_SPHERE = "Collision shapes/Sphere"
17+
const SHAPE_CONVEX = "Collision shapes/Convex"
18+
const SHAPE_RAY = "Collision shapes/Ray"
19+
1320
var _gravity = false
1421
var _slope = false
1522
var _snap = false
@@ -24,6 +31,9 @@ var _current_body_key = ""
2431
var _current_body = null
2532
var _body_type = ["KinematicBody", "RigidBody"]
2633

34+
var _shapes = {}
35+
var _current_shape = ""
36+
2737

2838
func _ready():
2939
var options = $Options
@@ -36,6 +46,13 @@ func _ready():
3646
_body_scene[option_name] = get_packed_scene(body)
3747
body.queue_free()
3848

49+
options.add_menu_item(SHAPE_CAPSULE)
50+
options.add_menu_item(SHAPE_BOX)
51+
options.add_menu_item(SHAPE_CYLINDER)
52+
options.add_menu_item(SHAPE_SPHERE)
53+
options.add_menu_item(SHAPE_CONVEX)
54+
options.add_menu_item(SHAPE_RAY)
55+
3956
options.add_menu_item(OPTION_GRAVITY, true, false)
4057
options.add_menu_item(OPTION_SLOPE, true, false)
4158
options.add_menu_item(OPTION_SNAP, true, false)
@@ -46,6 +63,14 @@ func _ready():
4663
options.connect("option_selected", self, "_on_option_selected")
4764
options.connect("option_changed", self, "_on_option_changed")
4865

66+
_shapes[SHAPE_CAPSULE] = "Capsule"
67+
_shapes[SHAPE_BOX] = "Box"
68+
_shapes[SHAPE_CYLINDER] = "Cylinder"
69+
_shapes[SHAPE_SPHERE] = "Sphere"
70+
_shapes[SHAPE_CONVEX] = "Convex"
71+
_shapes[SHAPE_RAY] = "Ray"
72+
_current_shape = _shapes[SHAPE_CAPSULE]
73+
4974
spawn_body_index(_current_body_index)
5075

5176

@@ -60,6 +85,26 @@ func _input(event):
6085
func _on_option_selected(option):
6186
if _body_scene.has(option):
6287
spawn_body_key(option)
88+
else:
89+
match option:
90+
SHAPE_CAPSULE:
91+
_current_shape = _shapes[SHAPE_CAPSULE]
92+
spawn_body_index(_current_body_index)
93+
SHAPE_BOX:
94+
_current_shape = _shapes[SHAPE_BOX]
95+
spawn_body_index(_current_body_index)
96+
SHAPE_CYLINDER:
97+
_current_shape = _shapes[SHAPE_CYLINDER]
98+
spawn_body_index(_current_body_index)
99+
SHAPE_SPHERE:
100+
_current_shape = _shapes[SHAPE_SPHERE]
101+
spawn_body_index(_current_body_index)
102+
SHAPE_CONVEX:
103+
_current_shape = _shapes[SHAPE_CONVEX]
104+
spawn_body_index(_current_body_index)
105+
SHAPE_RAY:
106+
_current_shape = _shapes[SHAPE_RAY]
107+
spawn_body_index(_current_body_index)
63108

64109

65110
func _on_option_changed(option, checked):
@@ -91,9 +136,10 @@ func spawn_body_index(body_index):
91136
_current_body_key = _key_list[body_index]
92137
var body_parent = $Bodies
93138
var body = _body_scene[_key_list[body_index]].instance()
94-
body_parent.add_child(body)
95139
_current_body = body
96140
init_body()
141+
body_parent.add_child(body)
142+
start_test()
97143

98144

99145
func spawn_body_key(body_key):
@@ -103,9 +149,10 @@ func spawn_body_key(body_key):
103149
_current_body_index = _key_list.find(body_key)
104150
var body_parent = $Bodies
105151
var body = _body_scene[body_key].instance()
106-
body_parent.add_child(body)
107152
_current_body = body
108153
init_body()
154+
body_parent.add_child(body)
155+
start_test()
109156

110157

111158
func init_body():
@@ -116,7 +163,13 @@ func init_body():
116163
elif _current_body is RigidBody:
117164
_current_body.physics_material_override.rough = _rough
118165
_current_body.physics_material_override.friction = 1.0 if _friction else 0.0
166+
for shape in _current_body.get_children():
167+
if shape is CollisionShape:
168+
if shape.name != _current_shape:
169+
shape.queue_free()
170+
119171

172+
func start_test():
120173
var animation_player = $Platforms/KinematicPlatform/AnimationPlayer
121174
animation_player.stop()
122175
if _animation_physics:
@@ -125,11 +178,10 @@ func init_body():
125178
animation_player.playback_process_mode = AnimationPlayer.ANIMATION_PROCESS_IDLE
126179
animation_player.play("Move")
127180

128-
$LabelBodyType.text = "Body Type: " + _body_type[_current_body_index]
181+
$LabelBodyType.text = "Body Type: " + _body_type[_current_body_index] + " \nCollision Shape: " + _current_shape
129182

130183

131184
func get_packed_scene(node):
132-
node.owner = self
133185
for child in node.get_children():
134186
child.owner = node
135187
var packed_scene = PackedScene.new()

3d/physics_tests/tests/functional/test_moving_platform.tscn

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[gd_scene load_steps=9 format=2]
1+
[gd_scene load_steps=14 format=2]
22

33
[ext_resource path="res://utils/camera_orbit.gd" type="Script" id=1]
44
[ext_resource path="res://tests/functional/test_moving_platform.gd" type="Script" id=2]
@@ -8,22 +8,48 @@
88
[sub_resource type="CapsuleShape" id=1]
99
radius = 0.3
1010

11-
[sub_resource type="PhysicsMaterial" id=2]
11+
[sub_resource type="BoxShape" id=2]
12+
extents = Vector3( 0.3, 0.8, 0.3 )
1213

13-
[sub_resource type="BoxShape" id=3]
14+
[sub_resource type="CylinderShape" id=3]
15+
radius = 0.3
16+
height = 1.60005
17+
18+
[sub_resource type="SphereShape" id=4]
19+
radius = 0.79945
20+
21+
[sub_resource type="ConvexPolygonShape" id=5]
22+
points = PoolVector3Array( -0.7, 0, -0.7, -0.3, 0, 0.8, 0.8, 0, -0.3, 0, -0.8, 0 )
23+
24+
[sub_resource type="RayShape" id=6]
25+
length = 0.8
26+
27+
[sub_resource type="PhysicsMaterial" id=7]
28+
29+
[sub_resource type="BoxShape" id=8]
1430
extents = Vector3( 2, 0.2, 1 )
1531

16-
[sub_resource type="Animation" id=4]
17-
length = 4.0
32+
[sub_resource type="Animation" id=9]
33+
length = 9.0
1834
tracks/0/type = "bezier"
1935
tracks/0/path = NodePath(".:translation:x")
2036
tracks/0/interp = 1
2137
tracks/0/loop_wrap = true
2238
tracks/0/imported = false
2339
tracks/0/enabled = true
2440
tracks/0/keys = {
25-
"points": PoolRealArray( -7, -0.25, 0, 0.25, 0, -7, -0.25, 0, 0.245766, 0.531658, 6, -0.132614, -0.374802, 0.25, 0 ),
26-
"times": PoolRealArray( 0, 0.5, 4 )
41+
"points": PoolRealArray( -7, -0.25, 0, 0.25, 0, -7, -0.25, 0, 0.25, 0, 6, -0.25, 0, 0.25, 0 ),
42+
"times": PoolRealArray( 0, 3, 6.5 )
43+
}
44+
tracks/1/type = "bezier"
45+
tracks/1/path = NodePath(".:translation:y")
46+
tracks/1/interp = 1
47+
tracks/1/loop_wrap = true
48+
tracks/1/imported = false
49+
tracks/1/enabled = true
50+
tracks/1/keys = {
51+
"points": PoolRealArray( -4.23538, -0.25, 0, 0.25, 0, -4.23538, -0.25, 0, 0.25, 0, 3, -0.25, 0, 0.25, 0, 3, -0.25, 0, 0.25, 0, -4.23538, -0.25, 0, 0.25, 0 ),
52+
"times": PoolRealArray( 0, 0.5, 3, 6.5, 9 )
2753
}
2854

2955
[node name="Test" type="Spatial"]
@@ -40,43 +66,85 @@ __meta__ = {
4066
}
4167

4268
[node name="Options" parent="." instance=ExtResource( 3 )]
69+
margin_top = 120.0
70+
margin_bottom = 140.0
4371

4472
[node name="Bodies" type="Spatial" parent="."]
4573

4674
[node name="KinematicBody" type="KinematicBody" parent="Bodies"]
47-
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -7, -1.95, 0 )
75+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -7, -4.18538, 0 )
4876
collision_layer = 2
4977
script = ExtResource( 4 )
5078
_stop_on_slopes = true
5179
_use_snap = true
5280

53-
[node name="CollisionShape" type="CollisionShape" parent="Bodies/KinematicBody"]
81+
[node name="Capsule" type="CollisionShape" parent="Bodies/KinematicBody"]
5482
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.8, 0 )
5583
shape = SubResource( 1 )
5684

85+
[node name="Box" type="CollisionShape" parent="Bodies/KinematicBody"]
86+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0 )
87+
shape = SubResource( 2 )
88+
89+
[node name="Cylinder" type="CollisionShape" parent="Bodies/KinematicBody"]
90+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0 )
91+
shape = SubResource( 3 )
92+
93+
[node name="Sphere" type="CollisionShape" parent="Bodies/KinematicBody"]
94+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0 )
95+
shape = SubResource( 4 )
96+
97+
[node name="Convex" type="CollisionShape" parent="Bodies/KinematicBody"]
98+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0 )
99+
shape = SubResource( 5 )
100+
101+
[node name="Ray" type="CollisionShape" parent="Bodies/KinematicBody"]
102+
transform = Transform( 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0.8, 0 )
103+
shape = SubResource( 6 )
104+
57105
[node name="RigidBody" type="RigidBody" parent="Bodies"]
58-
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -7, -1.95, 0 )
106+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -7, -4.18538, 0 )
59107
collision_layer = 4
60-
physics_material_override = SubResource( 2 )
108+
physics_material_override = SubResource( 7 )
61109
axis_lock_angular_x = true
62110
axis_lock_angular_y = true
63111
axis_lock_angular_z = true
64112

65-
[node name="CollisionShape" type="CollisionShape" parent="Bodies/RigidBody"]
113+
[node name="Capsule" type="CollisionShape" parent="Bodies/RigidBody"]
66114
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.8, 0 )
67115
shape = SubResource( 1 )
68116

117+
[node name="Box" type="CollisionShape" parent="Bodies/RigidBody"]
118+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0 )
119+
shape = SubResource( 2 )
120+
121+
[node name="Cylinder" type="CollisionShape" parent="Bodies/RigidBody"]
122+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0 )
123+
shape = SubResource( 3 )
124+
125+
[node name="Sphere" type="CollisionShape" parent="Bodies/RigidBody"]
126+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0 )
127+
shape = SubResource( 4 )
128+
129+
[node name="Convex" type="CollisionShape" parent="Bodies/RigidBody"]
130+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.8, 0 )
131+
shape = SubResource( 5 )
132+
133+
[node name="Ray" type="CollisionShape" parent="Bodies/RigidBody"]
134+
transform = Transform( 1, 0, 0, 0, 0, -1, 0, 1, 0, 0, 0.8, 0 )
135+
shape = SubResource( 6 )
136+
69137
[node name="Platforms" type="Spatial" parent="."]
70138

71139
[node name="KinematicPlatform" type="KinematicBody" parent="Platforms"]
72-
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -7, -2, 0 )
140+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -7, -4.25, 0 )
73141

74142
[node name="CollisionShape" type="CollisionShape" parent="Platforms/KinematicPlatform"]
75143
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.2, 0 )
76-
shape = SubResource( 3 )
144+
shape = SubResource( 8 )
77145

78146
[node name="AnimationPlayer" type="AnimationPlayer" parent="Platforms/KinematicPlatform"]
79-
anims/Move = SubResource( 4 )
147+
anims/Move = SubResource( 9 )
80148

81149
[node name="Camera" type="Camera" parent="."]
82150
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 10 )
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
extends Test
2+
3+
4+
const OPTION_BIG = "Floor options/Big"
5+
const OPTION_SMALL = "Floor options/Small"
6+
7+
const SHAPE_CONCAVE = "Collision shapes/Concave"
8+
const SHAPE_CONVEX = "Collision shapes/Convex"
9+
const SHAPE_BOX = "Collision shapes/Box"
10+
11+
var _dynamic_shapes_scene
12+
var _floor_shapes = {}
13+
var _floor_size = "Small"
14+
15+
var _current_floor_name = SHAPE_CONCAVE
16+
var _current_bodies
17+
var _current_floor
18+
19+
20+
func _ready():
21+
var options = $Options
22+
_dynamic_shapes_scene = get_packed_scene($DynamicShapes/Bodies)
23+
_floor_shapes[SHAPE_CONVEX + "Small"] = get_packed_scene($"Floors/ConvexSmall")
24+
_floor_shapes[SHAPE_CONVEX + "Big"] = get_packed_scene($"Floors/ConvexBig")
25+
_floor_shapes[SHAPE_CONCAVE + "Big"] = get_packed_scene($"Floors/ConcaveBig")
26+
_floor_shapes[SHAPE_CONCAVE + "Small"] = get_packed_scene($"Floors/ConcaveSmall")
27+
_floor_shapes[SHAPE_BOX + "Big"] = get_packed_scene($"Floors/BoxBig")
28+
_floor_shapes[SHAPE_BOX + "Small"] = get_packed_scene($"Floors/BoxSmall")
29+
$DynamicShapes/Bodies.queue_free()
30+
for floorNode in $Floors.get_children():
31+
floorNode.queue_free()
32+
33+
options.add_menu_item(OPTION_SMALL)
34+
options.add_menu_item(OPTION_BIG)
35+
options.add_menu_item(SHAPE_CONCAVE)
36+
options.add_menu_item(SHAPE_CONVEX)
37+
options.add_menu_item(SHAPE_BOX)
38+
39+
options.connect("option_selected", self, "_on_option_selected")
40+
restart_scene()
41+
42+
43+
func _on_option_selected(option):
44+
match option:
45+
OPTION_BIG:
46+
_floor_size = "Big"
47+
OPTION_SMALL:
48+
_floor_size = "Small"
49+
_:
50+
_current_floor_name = option
51+
restart_scene()
52+
53+
54+
func restart_scene():
55+
if _current_bodies:
56+
_current_bodies.queue_free()
57+
if _current_floor:
58+
_current_floor.queue_free()
59+
60+
var dynamic_bodies = _dynamic_shapes_scene.instance()
61+
_current_bodies = dynamic_bodies
62+
add_child(dynamic_bodies)
63+
64+
var floor_inst = _floor_shapes[_current_floor_name + _floor_size].instance()
65+
_current_floor = floor_inst
66+
$Floors.add_child(floor_inst)
67+
68+
$LabelBodyType.text = "Floor Type: " + _current_floor_name.rsplit("/", true, 1)[1] + "\nSize: " + _floor_size
69+
70+
71+
func get_packed_scene(node):
72+
for child in node.get_children():
73+
child.owner = node
74+
for child1 in child.get_children():
75+
child1.owner = node
76+
for child2 in child1.get_children():
77+
child2.owner = node
78+
var packed_scene = PackedScene.new()
79+
packed_scene.pack(node)
80+
return packed_scene
81+

0 commit comments

Comments
 (0)