Эх сурвалжийг харах

Additional 3D physics tests - RigidBody impact and ground check (#619)

Co-authored-by: Camille Mohr-Daurat <[email protected]>
Agnis "NeZvērs" Aldiņš 4 жил өмнө
parent
commit
08be1b7f03

+ 8 - 0
3d/physics_tests/tests.gd

@@ -34,6 +34,14 @@ var _tests = [
 		"id": "Functional Tests/Raycasting",
 		"path": "res://tests/functional/test_raycasting.tscn",
 	},
+	{
+		"id": "Functional Tests/RigidBody impact",
+		"path": "res://tests/functional/test_rigidbody_impact.tscn",
+	},
+	{
+		"id": "Functional Tests/RigidBody ground check",
+		"path": "res://tests/functional/test_rigidbody_ground_check.tscn",
+	},
 	{
 		"id": "Performance Tests/Broadphase",
 		"path": "res://tests/performance/test_perf_broadphase.tscn",

+ 159 - 0
3d/physics_tests/tests/functional/test_rigidbody_ground_check.tscn

@@ -0,0 +1,159 @@
+[gd_scene load_steps=25 format=2]
+
+[ext_resource path="res://utils/rigidbody_ground_check.gd" type="Script" id=1]
+[ext_resource path="res://test.gd" type="Script" id=2]
+[ext_resource path="res://tests/static_scene_plane.tscn" type="PackedScene" id=3]
+[ext_resource path="res://utils/camera_orbit.gd" type="Script" id=4]
+
+[sub_resource type="PhysicsMaterial" id=1]
+friction = 0.0
+
+[sub_resource type="BoxShape" id=2]
+
+[sub_resource type="CubeMesh" id=3]
+
+[sub_resource type="SpatialMaterial" id=4]
+
+[sub_resource type="PhysicsMaterial" id=5]
+friction = 0.0
+
+[sub_resource type="CapsuleShape" id=6]
+
+[sub_resource type="CapsuleMesh" id=7]
+
+[sub_resource type="SpatialMaterial" id=8]
+
+[sub_resource type="PhysicsMaterial" id=9]
+friction = 0.0
+
+[sub_resource type="CylinderShape" id=10]
+
+[sub_resource type="CylinderMesh" id=11]
+
+[sub_resource type="SpatialMaterial" id=12]
+
+[sub_resource type="PhysicsMaterial" id=13]
+friction = 0.0
+
+[sub_resource type="ConvexPolygonShape" id=14]
+points = PoolVector3Array( -0.7, 0, -0.7, -0.3, 0, 0.8, 0.8, 0, -0.3, 0, -1, 0 )
+
+[sub_resource type="ArrayMesh" id=15]
+surfaces/0 = {
+"aabb": AABB( -0.7, -1, -0.7, 1.5, 1.00001, 1.5 ),
+"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 ),
+"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 ),
+"blend_shape_data": [  ],
+"format": 97539,
+"index_count": 12,
+"primitive": 4,
+"skeleton_aabb": [  ],
+"vertex_count": 4
+}
+
+[sub_resource type="SpatialMaterial" id=16]
+
+[sub_resource type="PhysicsMaterial" id=17]
+friction = 0.0
+
+[sub_resource type="SphereShape" id=18]
+
+[sub_resource type="SphereMesh" id=19]
+
+[sub_resource type="SpatialMaterial" id=20]
+
+[node name="Test" type="Spatial"]
+script = ExtResource( 2 )
+
+[node name="DynamicShapes" type="Spatial" parent="."]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 9.35591, 0 )
+
+[node name="RigidBodyBox" type="RigidBody" parent="DynamicShapes"]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -6, 0, 0 )
+physics_material_override = SubResource( 1 )
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyBox"]
+transform = Transform( 0.6, 0, 0, 0, 1, 0, 0, 0, 0.6, 0, 0, 0 )
+shape = SubResource( 2 )
+
+[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodyBox/CollisionShape"]
+mesh = SubResource( 3 )
+material/0 = SubResource( 4 )
+
+[node name="RigidBodyCapsule" type="RigidBody" parent="DynamicShapes"]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -3, 0, 0 )
+physics_material_override = SubResource( 5 )
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyCapsule"]
+transform = Transform( 0.8, 0, 0, 0, -3.49691e-08, -0.8, 0, 0.8, -3.49691e-08, 0, 0, 0 )
+shape = SubResource( 6 )
+
+[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodyCapsule/CollisionShape"]
+mesh = SubResource( 7 )
+material/0 = SubResource( 8 )
+
+[node name="RigidBodyCylinder" type="RigidBody" parent="DynamicShapes"]
+physics_material_override = SubResource( 9 )
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyCylinder"]
+transform = Transform( 0.8, 0, 0, 0, 1, 0, 0, 0, 0.8, 0, 0, 0 )
+shape = SubResource( 10 )
+
+[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodyCylinder/CollisionShape"]
+mesh = SubResource( 11 )
+material/0 = SubResource( 12 )
+
+[node name="RigidBodyConvex" type="RigidBody" parent="DynamicShapes"]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 3, 0.974548, 0 )
+physics_material_override = SubResource( 13 )
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyConvex"]
+transform = Transform( 1.5, 0, 0, 0, 2, 0, 0, 0, 1.5, 0, 0, 0 )
+shape = SubResource( 14 )
+
+[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodyConvex/CollisionShape"]
+mesh = SubResource( 15 )
+material/0 = SubResource( 16 )
+
+[node name="RigidBodySphere" type="RigidBody" parent="DynamicShapes"]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 6, 0, 0 )
+physics_material_override = SubResource( 17 )
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodySphere"]
+transform = Transform( 0.8, 0, 0, 0, 0.8, 0, 0, 0, 0.8, 0, 0, 0 )
+shape = SubResource( 18 )
+
+[node name="MeshInstance" type="MeshInstance" parent="DynamicShapes/RigidBodySphere/CollisionShape"]
+mesh = SubResource( 19 )
+material/0 = SubResource( 20 )
+
+[node name="StaticBodyPlane" parent="." instance=ExtResource( 3 )]
+collision_layer = 2
+collision_mask = 3
+
+[node name="Camera" type="Camera" parent="."]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.604, 22.124 )
+script = ExtResource( 4 )
+
+[node name="OmniLight" type="OmniLight" parent="Camera"]
+omni_range = 50.0

+ 87 - 0
3d/physics_tests/tests/functional/test_rigidbody_impact.tscn

@@ -0,0 +1,87 @@
+[gd_scene load_steps=10 format=2]
+
+[ext_resource path="res://utils/rigidbody_pick.gd" type="Script" id=1]
+[ext_resource path="res://test.gd" type="Script" id=2]
+[ext_resource path="res://tests/static_scene_plane.tscn" type="PackedScene" id=3]
+[ext_resource path="res://utils/camera_orbit.gd" type="Script" id=4]
+
+[sub_resource type="BoxShape" id=1]
+
+[sub_resource type="CapsuleShape" id=2]
+
+[sub_resource type="CylinderShape" id=3]
+
+[sub_resource type="ConvexPolygonShape" id=4]
+points = PoolVector3Array( -0.7, 0, -0.7, -0.3, 0, 0.8, 0.8, 0, -0.3, 0, -1, 0 )
+
+[sub_resource type="SphereShape" id=5]
+
+[node name="Test" type="Spatial"]
+script = ExtResource( 2 )
+
+[node name="DynamicShapes" type="Spatial" parent="."]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 9.35591, 0 )
+
+[node name="RigidBodyBox" type="RigidBody" parent="DynamicShapes"]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -6, 0, 0 )
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyBox"]
+transform = Transform( 0.6, 0, 0, 0, 1, 0, 0, 0, 0.6, 0, 0, 0 )
+shape = SubResource( 1 )
+
+[node name="RigidBodyCapsule" type="RigidBody" parent="DynamicShapes"]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -3, 0, 0 )
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyCapsule"]
+transform = Transform( 0.8, 0, 0, 0, -3.49691e-08, -0.8, 0, 0.8, -3.49691e-08, 0, 0, 0 )
+shape = SubResource( 2 )
+
+[node name="RigidBodyCylinder" type="RigidBody" parent="DynamicShapes"]
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyCylinder"]
+transform = Transform( 0.8, 0, 0, 0, 1, 0, 0, 0, 0.8, 0, 0, 0 )
+shape = SubResource( 3 )
+
+[node name="RigidBodyConvex" type="RigidBody" parent="DynamicShapes"]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 3, 0.974548, 0 )
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodyConvex"]
+transform = Transform( 1.5, 0, 0, 0, 2, 0, 0, 0, 1.5, 0, 0, 0 )
+shape = SubResource( 4 )
+
+[node name="RigidBodySphere" type="RigidBody" parent="DynamicShapes"]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 6, 0, 0 )
+axis_lock_angular_x = true
+axis_lock_angular_y = true
+axis_lock_angular_z = true
+script = ExtResource( 1 )
+
+[node name="CollisionShape" type="CollisionShape" parent="DynamicShapes/RigidBodySphere"]
+transform = Transform( 0.8, 0, 0, 0, 0.8, 0, 0, 0, 0.8, 0, 0, 0 )
+shape = SubResource( 5 )
+
+[node name="StaticBodyPlane" parent="." instance=ExtResource( 3 )]
+
+[node name="Camera" type="Camera" parent="."]
+transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.604, 22.124 )
+fov = 35.0
+script = ExtResource( 4 )
+
+[node name="OmniLight" type="OmniLight" parent="Camera"]
+omni_range = 50.0

+ 47 - 0
3d/physics_tests/utils/rigidbody_ground_check.gd

@@ -0,0 +1,47 @@
+extends RigidBody
+
+
+onready var _forward = - transform.basis.z
+onready var _collision_shape = $CollisionShape
+onready var _material = $CollisionShape/MeshInstance.get_surface_material(0)
+
+var _dir = 1.0
+var _distance = 10.0
+var _walk_spd = 100.0
+var _acceleration = 22.0
+var _gravity_impulse = 30.0
+var _is_on_floor = false
+
+
+func _process(_delta):
+	if _is_on_floor:
+		_material.albedo_color = Color.white
+	else:
+		_material.albedo_color = Color.red
+
+
+func _integrate_forces(state):
+	var delta = state.step
+	var velocity = (_forward * _dir * _walk_spd * delta) + (state.linear_velocity  * Vector3.UP)
+	state.linear_velocity = state.linear_velocity.move_toward(velocity, _acceleration * delta)
+
+	if state.transform.origin.z < -_distance:
+		_dir = -1
+	if state.transform.origin.z > _distance:
+		_dir = 1
+
+	ground_check()
+
+
+func ground_check():
+	var space_state = get_world().direct_space_state
+	var shape = PhysicsShapeQueryParameters.new()
+	shape.transform = _collision_shape.global_transform
+	shape.shape_rid = _collision_shape.shape.get_rid()
+	shape.collision_mask = 2
+	var result = space_state.get_rest_info(shape)
+	if result:
+		_is_on_floor = true
+	else:
+		_is_on_floor = false
+

+ 2 - 1
3d/physics_tests/utils/rigidbody_pick.gd

@@ -47,7 +47,8 @@ func _physics_process(delta):
 			world_delta = camera_basis * world_delta
 
 			var camera_dist = camera.global_transform.origin.distance_to(global_transform.origin)
-			world_delta *= CAMERA_DISTANCE_COEFFICIENT * camera_dist
+			var fov_coefficient = camera.fov / 70.0
+			world_delta *= CAMERA_DISTANCE_COEFFICIENT * camera_dist * fov_coefficient
 
 		if mode == MODE_STATIC:
 			global_transform.origin += world_delta