Skip to content

Commit 2d4d233

Browse files
authored
3D physics tests - Moving platform (#623)
1 parent 4456027 commit 2d4d233

File tree

5 files changed

+270
-2
lines changed

5 files changed

+270
-2
lines changed

3d/physics_tests/project.godot

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ toggle_pause={
8888
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":80,"unicode":0,"echo":false,"script":null)
8989
]
9090
}
91+
character_right={
92+
"deadzone": 0.5,
93+
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null)
94+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null)
95+
]
96+
}
97+
character_left={
98+
"deadzone": 0.5,
99+
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null)
100+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null)
101+
]
102+
}
103+
character_jump={
104+
"deadzone": 0.5,
105+
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null)
106+
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"unicode":0,"echo":false,"script":null)
107+
]
108+
}
91109

92110
[rendering]
93111

3d/physics_tests/tests.gd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ var _tests = [
3535
"path": "res://tests/functional/test_raycasting.tscn",
3636
},
3737
{
38-
"id": "Functional Tests/RigidBody impact",
38+
"id": "Functional Tests/RigidBody Impact",
3939
"path": "res://tests/functional/test_rigidbody_impact.tscn",
4040
},
4141
{
42-
"id": "Functional Tests/RigidBody ground check",
42+
"id": "Functional Tests/RigidBody Ground Check",
4343
"path": "res://tests/functional/test_rigidbody_ground_check.tscn",
4444
},
45+
{
46+
"id": "Functional Tests/Moving Platform",
47+
"path": "res://tests/functional/test_moving_platform.tscn",
48+
},
4549
{
4650
"id": "Performance Tests/Broadphase",
4751
"path": "res://tests/performance/test_perf_broadphase.tscn",
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
extends Test
2+
3+
4+
const OPTION_BODY_TYPE = "Body Type/%s (%d)"
5+
6+
const OPTION_GRAVITY = "Physics options/Gravity on floor (Kinematic only)"
7+
const OPTION_SLOPE = "Physics options/Stop on slope (Kinematic only)"
8+
const OPTION_SNAP = "Physics options/Use snap (Kinematic only)"
9+
const OPTION_FRICTION = "Physics options/Friction (Rigid only)"
10+
const OPTION_ROUGH = "Physics options/Rough (Rigid only)"
11+
const OPTION_PROCESS_PHYSICS = "Physics options/AnimationPlayer physics process mode"
12+
13+
var _gravity = false
14+
var _slope = false
15+
var _snap = false
16+
var _friction = false
17+
var _rough = false
18+
var _animation_physics = false
19+
20+
var _body_scene = {}
21+
var _key_list = []
22+
var _current_body_index = 0
23+
var _current_body_key = ""
24+
var _current_body = null
25+
var _body_type = ["KinematicBody", "RigidBody"]
26+
27+
28+
func _ready():
29+
var options = $Options
30+
var bodies = $Bodies.get_children()
31+
for i in bodies.size():
32+
var body = bodies[i]
33+
var option_name = OPTION_BODY_TYPE % [body.name, i + 1]
34+
options.add_menu_item(option_name)
35+
_key_list.append(option_name)
36+
_body_scene[option_name] = get_packed_scene(body)
37+
body.queue_free()
38+
39+
options.add_menu_item(OPTION_GRAVITY, true, false)
40+
options.add_menu_item(OPTION_SLOPE, true, false)
41+
options.add_menu_item(OPTION_SNAP, true, false)
42+
options.add_menu_item(OPTION_FRICTION, true, false)
43+
options.add_menu_item(OPTION_ROUGH, true, false)
44+
options.add_menu_item(OPTION_PROCESS_PHYSICS, true, false)
45+
46+
options.connect("option_selected", self, "_on_option_selected")
47+
options.connect("option_changed", self, "_on_option_changed")
48+
49+
spawn_body_index(_current_body_index)
50+
51+
52+
func _input(event):
53+
var key_event = event as InputEventKey
54+
if key_event and not key_event.pressed:
55+
var _index = key_event.scancode - KEY_1
56+
if _index >= 0 and _index < _key_list.size():
57+
spawn_body_index(_index)
58+
59+
60+
func _on_option_selected(option):
61+
if _body_scene.has(option):
62+
spawn_body_key(option)
63+
64+
65+
func _on_option_changed(option, checked):
66+
match option:
67+
OPTION_GRAVITY:
68+
_gravity = checked
69+
spawn_body_index(_current_body_index)
70+
OPTION_SLOPE:
71+
_slope = checked
72+
spawn_body_index(_current_body_index)
73+
OPTION_SNAP:
74+
_snap = checked
75+
spawn_body_index(_current_body_index)
76+
OPTION_FRICTION:
77+
_friction = checked
78+
spawn_body_index(_current_body_index)
79+
OPTION_ROUGH:
80+
_rough = checked
81+
spawn_body_index(_current_body_index)
82+
OPTION_PROCESS_PHYSICS:
83+
_animation_physics = checked
84+
spawn_body_index(_current_body_index)
85+
86+
87+
func spawn_body_index(body_index):
88+
if _current_body:
89+
_current_body.queue_free()
90+
_current_body_index = body_index
91+
_current_body_key = _key_list[body_index]
92+
var body_parent = $Bodies
93+
var body = _body_scene[_key_list[body_index]].instance()
94+
body_parent.add_child(body)
95+
_current_body = body
96+
init_body()
97+
98+
99+
func spawn_body_key(body_key):
100+
if _current_body:
101+
_current_body.queue_free()
102+
_current_body_key = body_key
103+
_current_body_index = _key_list.find(body_key)
104+
var body_parent = $Bodies
105+
var body = _body_scene[body_key].instance()
106+
body_parent.add_child(body)
107+
_current_body = body
108+
init_body()
109+
110+
111+
func init_body():
112+
if _current_body is KinematicBody:
113+
_current_body._gravity_on_floor = _gravity
114+
_current_body._stop_on_slopes = _slope
115+
_current_body._use_snap = _snap
116+
elif _current_body is RigidBody:
117+
_current_body.physics_material_override.rough = _rough
118+
_current_body.physics_material_override.friction = 1.0 if _friction else 0.0
119+
120+
var animation_player = $Platforms/KinematicPlatform/AnimationPlayer
121+
animation_player.stop()
122+
if _animation_physics:
123+
animation_player.playback_process_mode = AnimationPlayer.ANIMATION_PROCESS_PHYSICS
124+
else:
125+
animation_player.playback_process_mode = AnimationPlayer.ANIMATION_PROCESS_IDLE
126+
animation_player.play("Move")
127+
128+
$LabelBodyType.text = "Body Type: " + _body_type[_current_body_index]
129+
130+
131+
func get_packed_scene(node):
132+
node.owner = self
133+
for child in node.get_children():
134+
child.owner = node
135+
var packed_scene = PackedScene.new()
136+
packed_scene.pack(node)
137+
return packed_scene
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
[gd_scene load_steps=9 format=2]
2+
3+
[ext_resource path="res://utils/camera_orbit.gd" type="Script" id=1]
4+
[ext_resource path="res://tests/functional/test_moving_platform.gd" type="Script" id=2]
5+
[ext_resource path="res://tests/test_options.tscn" type="PackedScene" id=3]
6+
[ext_resource path="res://utils/kinematicbody_physics.gd" type="Script" id=4]
7+
8+
[sub_resource type="CapsuleShape" id=1]
9+
radius = 0.3
10+
11+
[sub_resource type="PhysicsMaterial" id=2]
12+
13+
[sub_resource type="BoxShape" id=3]
14+
extents = Vector3( 2, 0.2, 1 )
15+
16+
[sub_resource type="Animation" id=4]
17+
length = 4.0
18+
tracks/0/type = "bezier"
19+
tracks/0/path = NodePath(".:translation:x")
20+
tracks/0/interp = 1
21+
tracks/0/loop_wrap = true
22+
tracks/0/imported = false
23+
tracks/0/enabled = true
24+
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 )
27+
}
28+
29+
[node name="Test" type="Spatial"]
30+
script = ExtResource( 2 )
31+
32+
[node name="LabelBodyType" type="Label" parent="."]
33+
margin_left = 14.0
34+
margin_top = 78.0
35+
margin_right = 171.0
36+
margin_bottom = 92.0
37+
text = "Body Type: "
38+
__meta__ = {
39+
"_edit_use_anchors_": false
40+
}
41+
42+
[node name="Options" parent="." instance=ExtResource( 3 )]
43+
44+
[node name="Bodies" type="Spatial" parent="."]
45+
46+
[node name="KinematicBody" type="KinematicBody" parent="Bodies"]
47+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -7, -1.95, 0 )
48+
collision_layer = 2
49+
script = ExtResource( 4 )
50+
_stop_on_slopes = true
51+
_use_snap = true
52+
53+
[node name="CollisionShape" type="CollisionShape" parent="Bodies/KinematicBody"]
54+
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.8, 0 )
55+
shape = SubResource( 1 )
56+
57+
[node name="RigidBody" type="RigidBody" parent="Bodies"]
58+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -7, -1.95, 0 )
59+
collision_layer = 4
60+
physics_material_override = SubResource( 2 )
61+
axis_lock_angular_x = true
62+
axis_lock_angular_y = true
63+
axis_lock_angular_z = true
64+
65+
[node name="CollisionShape" type="CollisionShape" parent="Bodies/RigidBody"]
66+
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.8, 0 )
67+
shape = SubResource( 1 )
68+
69+
[node name="Platforms" type="Spatial" parent="."]
70+
71+
[node name="KinematicPlatform" type="KinematicBody" parent="Platforms"]
72+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -7, -2, 0 )
73+
74+
[node name="CollisionShape" type="CollisionShape" parent="Platforms/KinematicPlatform"]
75+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.2, 0 )
76+
shape = SubResource( 3 )
77+
78+
[node name="AnimationPlayer" type="AnimationPlayer" parent="Platforms/KinematicPlatform"]
79+
anims/Move = SubResource( 4 )
80+
81+
[node name="Camera" type="Camera" parent="."]
82+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 10 )
83+
script = ExtResource( 1 )
84+
85+
[node name="OmniLight" type="OmniLight" parent="Camera"]
86+
omni_range = 50.0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
extends KinematicBody
2+
3+
4+
export(bool) var _gravity_on_floor = true
5+
export(bool) var _stop_on_slopes = false
6+
export(bool) var _use_snap = false
7+
8+
var _gravity = 20.0
9+
var _velocity = Vector3.ZERO
10+
11+
12+
func _physics_process(delta):
13+
var snap = Vector3.DOWN * 0.2
14+
if is_on_floor() and _gravity_on_floor:
15+
_velocity += Vector3.DOWN * _gravity * delta
16+
else:
17+
_velocity += Vector3.DOWN * _gravity * delta
18+
snap = Vector3.ZERO
19+
20+
if _use_snap:
21+
_velocity = move_and_slide_with_snap(_velocity, snap, Vector3.UP, _stop_on_slopes)
22+
else:
23+
_velocity = move_and_slide(_velocity, Vector3.UP, _stop_on_slopes)

0 commit comments

Comments
 (0)