immersive-home/app/lib/utils/touch/collide.gd

59 lines
2.0 KiB
GDScript3
Raw Normal View History

2024-02-27 17:39:47 +02:00
extends Node3D
2024-03-17 01:14:31 +02:00
## Calculates collision for fingers and FingerAreas
2024-02-27 17:39:47 +02:00
const Finger = preload ("res://lib/utils/touch/finger.gd")
2024-03-21 17:19:09 +02:00
const TipCollider = preload ("res://content/system/hands/tip_collider.tscn")
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
var tip_right: Node3D
var tip_left: Node3D
var tip_left_body: RigidBody3D
var tip_right_body: RigidBody3D
2024-02-27 17:39:47 +02:00
var hand_left: Node3D
var hand_right: Node3D
2024-03-21 17:19:09 +02:00
var hand_left_mesh: MeshInstance3D
var hand_right_mesh: MeshInstance3D
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
func _init(hand_left: OpenXRHand, hand_right: OpenXRHand, tip_left: Node3D, tip_right: Node3D):
self.tip_right = tip_right
self.tip_left = tip_left
self.hand_left = hand_left
self.hand_right = hand_right
self.hand_left_mesh = hand_left.get_node("left_hand/Armature_001/Skeleton3D/vr_glove_left_slim")
self.hand_right_mesh = hand_right.get_node("right_hand/Armature/Skeleton3D/vr_glove_right_slim")
2024-02-27 17:39:47 +02:00
func _ready():
2024-03-21 17:19:09 +02:00
var body_container = Node3D.new()
body_container.name = "HandBodyContainer"
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
get_node("/root/Main/").add_child.call_deferred(body_container)
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
tip_right_body = TipCollider.instantiate()
tip_right_body.global_position = tip_right.global_position
body_container.add_child(tip_right_body)
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
tip_left_body = TipCollider.instantiate()
tip_left_body.global_position = tip_left.global_position
body_container.add_child(tip_left_body)
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
func _physics_process(_delta):
_move_tip_rigidbody_to_bone(tip_left_body, tip_left)
_move_tip_rigidbody_to_bone(tip_right_body, tip_right)
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
func _move_tip_rigidbody_to_bone(tip_rigidbody: RigidBody3D, tip_bone: Node3D):
if tip_rigidbody.is_inside_tree() == false:
return
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
var move_delta: Vector3 = tip_bone.global_position - tip_rigidbody.global_position
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
hand_right_mesh.global_position = hand_right.global_position - move_delta
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
# Snap back the rigidbody if it's too far away.
if move_delta.length() > 0.1:
tip_rigidbody.global_position = tip_bone.global_position
return
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
var coef_force = 30.0
tip_rigidbody.apply_central_force(move_delta * coef_force)
tip_rigidbody.global_transform.basis = hand_right.global_transform.basis