Skip to content

Commit 68fcf8f

Browse files
committed
Add hinge math wip
Still fails to account for hinge transforms or the custom 'axis' property. Rotates relative to handle's starting position. No longer increases in angle with non-axis relevant motion.
1 parent 6ca6c18 commit 68fcf8f

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

addons/godot-xr-tools/interactables/interactable_hinge.gd

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,38 @@ func _ready():
7474
func _process(_delta: float) -> void:
7575
if grabbed_handles.is_empty():
7676
return
77-
77+
7878
# Get the total handle angular offsets
7979
var offset_sum := 0.0
8080
for item in grabbed_handles:
8181
var handle := item as XRToolsInteractableHandle
82-
# global handle + handle_origin position
8382
var axis := get_final_axis()
84-
var to_handle : Vector3 = handle.global_transform.origin * global_transform
85-
var to_handle_origin : Vector3 = handle.handle_origin.origin * global_transform
8683

87-
# project 'to_handle' and 'to_handle_origin' on 'axis'
88-
# then measure the angle
89-
offset_sum += atan2(to_handle_origin.cross(to_handle).dot(axis), to_handle.dot(to_handle_origin))
84+
# Move to local space
85+
var to_handle := to_local(handle.global_position)
86+
var to_origin := to_local(handle.handle_origin.origin)
87+
var start := handle.handle_origin.origin
88+
89+
# Find angle
90+
var axis_plane = Plane(axis)
91+
var h_flat = axis_plane.project(to_handle)
92+
var o_flat = axis_plane.project(to_origin)
93+
94+
# Angle from origin to handle on the plane of "axis"
95+
var angle1 = o_flat.signed_angle_to(-h_flat, axis)
96+
97+
# Angle from "UP" to starting position of the handle
98+
var start_angle = Vector3.UP.signed_angle_to(start, axis)
99+
100+
var offset = angle1-start_angle
101+
102+
offset_sum += offset
90103

91104
# Average the angular offsets
92105
var offset := offset_sum / grabbed_handles.size()
93106

94107
# Move the hinge by the requested offset
95-
move_hinge(_hinge_position_rad + offset)
108+
move_hinge(offset)
96109

97110

98111
## Return a unit vector of the final rotation axis

0 commit comments

Comments
 (0)