Skip to content

Commit 08be1b7

Browse files
Additional 3D physics tests - RigidBody impact and ground check (#619)
Co-authored-by: Camille Mohr-Daurat <[email protected]>
1 parent 4ef90a8 commit 08be1b7

File tree

5 files changed

+303
-1
lines changed

5 files changed

+303
-1
lines changed

3d/physics_tests/tests.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ var _tests = [
3434
"id": "Functional Tests/Raycasting",
3535
"path": "res://tests/functional/test_raycasting.tscn",
3636
},
37+
{
38+
"id": "Functional Tests/RigidBody impact",
39+
"path": "res://tests/functional/test_rigidbody_impact.tscn",
40+
},
41+
{
42+
"id": "Functional Tests/RigidBody ground check",
43+
"path": "res://tests/functional/test_rigidbody_ground_check.tscn",
44+
},
3745
{
3846
"id": "Performance Tests/Broadphase",
3947
"path": "res://tests/performance/test_perf_broadphase.tscn",
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
[gd_scene load_steps=25 format=2]
2+
3+
[ext_resource path="res://utils/rigidbody_ground_check.gd" type="Script" id=1]
4+
[ext_resource path="res://test.gd" type="Script" id=2]
5+
[ext_resource path="res://tests/static_scene_plane.tscn" type="PackedScene" id=3]
6+
[ext_resource path="res://utils/camera_orbit.gd" type="Script" id=4]
7+
8+
[sub_resource type="PhysicsMaterial" id=1]
9+
friction = 0.0
10+
11+
[sub_resource type="BoxShape" id=2]
12+
13+
[sub_resource type="CubeMesh" id=3]
14+
15+
[sub_resource type="SpatialMaterial" id=4]
16+
17+
[sub_resource type="PhysicsMaterial" id=5]
18+
friction = 0.0
19+
20+
[sub_resource type="CapsuleShape" id=6]
21+
22+
[sub_resource type="CapsuleMesh" id=7]
23+
24+
[sub_resource type="SpatialMaterial" id=8]
25+
26+
[sub_resource type="PhysicsMaterial" id=9]
27+
friction = 0.0
28+
29+
[sub_resource type="CylinderShape" id=10]
30+
31+
[sub_resource type="CylinderMesh" id=11]
32+
33+
[sub_resource type="SpatialMaterial" id=12]
34+
35+
[sub_resource type="PhysicsMaterial" id=13]
36+
friction = 0.0
37+
38+
[sub_resource type="ConvexPolygonShape" id=14]
39+
points = PoolVector3Array( -0.7, 0, -0.7, -0.3, 0, 0.8, 0.8, 0, -0.3, 0, -1, 0 )
40+
41+
[sub_resource type="ArrayMesh" id=15]
42+
surfaces/0 = {
43+
"aabb": AABB( -0.7, -1, -0.7, 1.5, 1.00001, 1.5 ),
44+
"array_data": PoolByteArray( 51, 51, 51, 191, 0, 0, 0, 0, 51, 51, 51, 191, 167, 0, 167, 0, 154, 153, 153, 190, 0, 0, 0, 0, 205, 204, 76, 63, 212, 0, 118, 0, 205, 204, 76, 63, 0, 0, 0, 0, 154, 153, 153, 190, 118, 0, 212, 0, 0, 0, 0, 0, 0, 0, 128, 191, 0, 0, 0, 0, 0, 129, 0, 0 ),
45+
"array_index_data": PoolByteArray( 0, 0, 1, 0, 3, 0, 1, 0, 2, 0, 3, 0, 2, 0, 0, 0, 3, 0, 2, 0, 1, 0, 0, 0 ),
46+
"blend_shape_data": [ ],
47+
"format": 97539,
48+
"index_count": 12,
49+
"primitive": 4,
50+
"skeleton_aabb": [ ],
51+
"vertex_count": 4
52+
}
53+
54+
[sub_resource type="SpatialMaterial" id=16]
55+
56+
[sub_resource type="PhysicsMaterial" id=17]
57+
friction = 0.0
58+
59+
[sub_resource type="SphereShape" id=18]
60+
61+
[sub_resource type="SphereMesh" id=19]
62+
63+
[sub_resource type="SpatialMaterial" id=20]
64+
65+
[node name="Test" type="Spatial"]
66+
script = ExtResource( 2 )
67+
68+
[node name="DynamicShapes" type="Spatial" parent="."]
69+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 9.35591, 0 )
70+
71+
[node name="RigidBodyBox" type="RigidBody" parent="DynamicShapes"]
72+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -6, 0, 0 )
73+
physics_material_override = SubResource( 1 )
74+
axis_lock_angular_x = true
75+
axis_lock_angular_y = true
76+
axis_lock_angular_z = true
77+
script = ExtResource( 1 )
78+
79+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyBox"]
80+
transform = Transform( 0.6, 0, 0, 0, 1, 0, 0, 0, 0.6, 0, 0, 0 )
81+
shape = SubResource( 2 )
82+
83+
[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodyBox/CollisionShape"]
84+
mesh = SubResource( 3 )
85+
material/0 = SubResource( 4 )
86+
87+
[node name="RigidBodyCapsule" type="RigidBody" parent="DynamicShapes"]
88+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -3, 0, 0 )
89+
physics_material_override = SubResource( 5 )
90+
axis_lock_angular_x = true
91+
axis_lock_angular_y = true
92+
axis_lock_angular_z = true
93+
script = ExtResource( 1 )
94+
95+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyCapsule"]
96+
transform = Transform( 0.8, 0, 0, 0, -3.49691e-08, -0.8, 0, 0.8, -3.49691e-08, 0, 0, 0 )
97+
shape = SubResource( 6 )
98+
99+
[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodyCapsule/CollisionShape"]
100+
mesh = SubResource( 7 )
101+
material/0 = SubResource( 8 )
102+
103+
[node name="RigidBodyCylinder" type="RigidBody" parent="DynamicShapes"]
104+
physics_material_override = SubResource( 9 )
105+
axis_lock_angular_x = true
106+
axis_lock_angular_y = true
107+
axis_lock_angular_z = true
108+
script = ExtResource( 1 )
109+
110+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyCylinder"]
111+
transform = Transform( 0.8, 0, 0, 0, 1, 0, 0, 0, 0.8, 0, 0, 0 )
112+
shape = SubResource( 10 )
113+
114+
[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodyCylinder/CollisionShape"]
115+
mesh = SubResource( 11 )
116+
material/0 = SubResource( 12 )
117+
118+
[node name="RigidBodyConvex" type="RigidBody" parent="DynamicShapes"]
119+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 3, 0.974548, 0 )
120+
physics_material_override = SubResource( 13 )
121+
axis_lock_angular_x = true
122+
axis_lock_angular_y = true
123+
axis_lock_angular_z = true
124+
script = ExtResource( 1 )
125+
126+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyConvex"]
127+
transform = Transform( 1.5, 0, 0, 0, 2, 0, 0, 0, 1.5, 0, 0, 0 )
128+
shape = SubResource( 14 )
129+
130+
[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodyConvex/CollisionShape"]
131+
mesh = SubResource( 15 )
132+
material/0 = SubResource( 16 )
133+
134+
[node name="RigidBodySphere" type="RigidBody" parent="DynamicShapes"]
135+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 6, 0, 0 )
136+
physics_material_override = SubResource( 17 )
137+
axis_lock_angular_x = true
138+
axis_lock_angular_y = true
139+
axis_lock_angular_z = true
140+
script = ExtResource( 1 )
141+
142+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodySphere"]
143+
transform = Transform( 0.8, 0, 0, 0, 0.8, 0, 0, 0, 0.8, 0, 0, 0 )
144+
shape = SubResource( 18 )
145+
146+
[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodySphere/CollisionShape"]
147+
mesh = SubResource( 19 )
148+
material/0 = SubResource( 20 )
149+
150+
[node name="StaticBodyPlane" parent="." instance=ExtResource( 3 )]
151+
collision_layer = 2
152+
collision_mask = 3
153+
154+
[node name="Camera" type="Camera" parent="."]
155+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.604, 22.124 )
156+
script = ExtResource( 4 )
157+
158+
[node name="OmniLight" type="OmniLight" parent="Camera"]
159+
omni_range = 50.0
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[gd_scene load_steps=10 format=2]
2+
3+
[ext_resource path="res://utils/rigidbody_pick.gd" type="Script" id=1]
4+
[ext_resource path="res://test.gd" type="Script" id=2]
5+
[ext_resource path="res://tests/static_scene_plane.tscn" type="PackedScene" id=3]
6+
[ext_resource path="res://utils/camera_orbit.gd" type="Script" id=4]
7+
8+
[sub_resource type="BoxShape" id=1]
9+
10+
[sub_resource type="CapsuleShape" id=2]
11+
12+
[sub_resource type="CylinderShape" id=3]
13+
14+
[sub_resource type="ConvexPolygonShape" id=4]
15+
points = PoolVector3Array( -0.7, 0, -0.7, -0.3, 0, 0.8, 0.8, 0, -0.3, 0, -1, 0 )
16+
17+
[sub_resource type="SphereShape" id=5]
18+
19+
[node name="Test" type="Spatial"]
20+
script = ExtResource( 2 )
21+
22+
[node name="DynamicShapes" type="Spatial" parent="."]
23+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 9.35591, 0 )
24+
25+
[node name="RigidBodyBox" type="RigidBody" parent="DynamicShapes"]
26+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -6, 0, 0 )
27+
axis_lock_angular_x = true
28+
axis_lock_angular_y = true
29+
axis_lock_angular_z = true
30+
script = ExtResource( 1 )
31+
32+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyBox"]
33+
transform = Transform( 0.6, 0, 0, 0, 1, 0, 0, 0, 0.6, 0, 0, 0 )
34+
shape = SubResource( 1 )
35+
36+
[node name="RigidBodyCapsule" type="RigidBody" parent="DynamicShapes"]
37+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -3, 0, 0 )
38+
axis_lock_angular_x = true
39+
axis_lock_angular_y = true
40+
axis_lock_angular_z = true
41+
script = ExtResource( 1 )
42+
43+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyCapsule"]
44+
transform = Transform( 0.8, 0, 0, 0, -3.49691e-08, -0.8, 0, 0.8, -3.49691e-08, 0, 0, 0 )
45+
shape = SubResource( 2 )
46+
47+
[node name="RigidBodyCylinder" type="RigidBody" parent="DynamicShapes"]
48+
axis_lock_angular_x = true
49+
axis_lock_angular_y = true
50+
axis_lock_angular_z = true
51+
script = ExtResource( 1 )
52+
53+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyCylinder"]
54+
transform = Transform( 0.8, 0, 0, 0, 1, 0, 0, 0, 0.8, 0, 0, 0 )
55+
shape = SubResource( 3 )
56+
57+
[node name="RigidBodyConvex" type="RigidBody" parent="DynamicShapes"]
58+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 3, 0.974548, 0 )
59+
axis_lock_angular_x = true
60+
axis_lock_angular_y = true
61+
axis_lock_angular_z = true
62+
script = ExtResource( 1 )
63+
64+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyConvex"]
65+
transform = Transform( 1.5, 0, 0, 0, 2, 0, 0, 0, 1.5, 0, 0, 0 )
66+
shape = SubResource( 4 )
67+
68+
[node name="RigidBodySphere" type="RigidBody" parent="DynamicShapes"]
69+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 6, 0, 0 )
70+
axis_lock_angular_x = true
71+
axis_lock_angular_y = true
72+
axis_lock_angular_z = true
73+
script = ExtResource( 1 )
74+
75+
[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodySphere"]
76+
transform = Transform( 0.8, 0, 0, 0, 0.8, 0, 0, 0, 0.8, 0, 0, 0 )
77+
shape = SubResource( 5 )
78+
79+
[node name="StaticBodyPlane" parent="." instance=ExtResource( 3 )]
80+
81+
[node name="Camera" type="Camera" parent="."]
82+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.604, 22.124 )
83+
fov = 35.0
84+
script = ExtResource( 4 )
85+
86+
[node name="OmniLight" type="OmniLight" parent="Camera"]
87+
omni_range = 50.0
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
extends RigidBody
2+
3+
4+
onready var _forward = - transform.basis.z
5+
onready var _collision_shape = $CollisionShape
6+
onready var _material = $CollisionShape/MeshInstance.get_surface_material(0)
7+
8+
var _dir = 1.0
9+
var _distance = 10.0
10+
var _walk_spd = 100.0
11+
var _acceleration = 22.0
12+
var _gravity_impulse = 30.0
13+
var _is_on_floor = false
14+
15+
16+
func _process(_delta):
17+
if _is_on_floor:
18+
_material.albedo_color = Color.white
19+
else:
20+
_material.albedo_color = Color.red
21+
22+
23+
func _integrate_forces(state):
24+
var delta = state.step
25+
var velocity = (_forward * _dir * _walk_spd * delta) + (state.linear_velocity * Vector3.UP)
26+
state.linear_velocity = state.linear_velocity.move_toward(velocity, _acceleration * delta)
27+
28+
if state.transform.origin.z < -_distance:
29+
_dir = -1
30+
if state.transform.origin.z > _distance:
31+
_dir = 1
32+
33+
ground_check()
34+
35+
36+
func ground_check():
37+
var space_state = get_world().direct_space_state
38+
var shape = PhysicsShapeQueryParameters.new()
39+
shape.transform = _collision_shape.global_transform
40+
shape.shape_rid = _collision_shape.shape.get_rid()
41+
shape.collision_mask = 2
42+
var result = space_state.get_rest_info(shape)
43+
if result:
44+
_is_on_floor = true
45+
else:
46+
_is_on_floor = false
47+

3d/physics_tests/utils/rigidbody_pick.gd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ func _physics_process(delta):
4747
world_delta = camera_basis * world_delta
4848

4949
var camera_dist = camera.global_transform.origin.distance_to(global_transform.origin)
50-
world_delta *= CAMERA_DISTANCE_COEFFICIENT * camera_dist
50+
var fov_coefficient = camera.fov / 70.0
51+
world_delta *= CAMERA_DISTANCE_COEFFICIENT * camera_dist * fov_coefficient
5152

5253
if mode == MODE_STATIC:
5354
global_transform.origin += world_delta

0 commit comments

Comments
 (0)