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

58 lines
1.6 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-05-06 19:22:12 +03:00
var tip: Node3D
var tip_body: RigidBody3D
var hand: Node3D
var hand_mesh: MeshInstance3D
2024-03-21 17:19:09 +02:00
2024-05-06 19:22:12 +03:00
func _init(hand: Node3D, hand_mesh: MeshInstance3D, tip: Node3D):
self.tip = tip
self.hand = hand
self.hand_mesh = hand_mesh
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-05-06 19:22:12 +03:00
tip_body = TipCollider.instantiate()
tip_body.global_position = tip.global_position
body_container.add_child(tip_body)
2024-02-27 17:39:47 +02:00
2024-03-21 17:19:09 +02:00
func _physics_process(_delta):
2024-05-06 19:22:12 +03:00
_move_tip_rigidbody_to_bone(tip_body, tip)
2024-02-27 17:39:47 +02:00
2024-05-06 21:35:31 +03:00
var last_run_active = false
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-05-06 21:35:31 +03:00
if TouchManager.is_touching() == false:
hand_mesh.position = Vector3.ZERO
last_run_active = false
return
if last_run_active == false:
tip_rigidbody.global_position = tip_bone.global_position
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-05-06 19:22:12 +03:00
hand_mesh.global_position = hand.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)
2024-05-06 19:22:12 +03:00
tip_rigidbody.global_transform.basis = hand.global_transform.basis
2024-05-06 21:35:31 +03:00
last_run_active = true