diff --git a/src/bone.cpp b/src/bone.cpp index 670bf6a..787ce8e 100644 --- a/src/bone.cpp +++ b/src/bone.cpp @@ -137,29 +137,15 @@ void UltraleapBone::_bind_methods() { BIND_ENUM_CONSTANT(None); } -void UltraleapBone::fill_bone_data(Ref ul_bone, LEAP_BONE* bone, BoneType type, LEAP_BONE* previous_bone, LEAP_PALM* palm) { - ul_bone->type = type; - ul_bone->prev_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&bone->prev_joint); - ul_bone->next_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&bone->next_joint); - ul_bone->width = bone->width / 1000; - ul_bone->orientation = UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&bone->rotation); +void UltraleapBone::fill_bone_data(Ref bone, Ref previous_bone, LEAP_BONE* leap_bone, BoneType type, LEAP_PALM* palm) { + bone->type = type; + bone->prev_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&leap_bone->prev_joint); + bone->next_joint = UltraleapTypes::ultraleap_vector3_to_godot_vector3(&leap_bone->next_joint); + bone->width = leap_bone->width / 1000; + bone->orientation = UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&leap_bone->rotation); - // If previous_bone is defined, we compute the transform from the previous bone to the current bone - if (previous_bone != NULL) { - Transform3D previous_bone_transform = Transform3D( - Basis(UltraleapTypes::ultraleap_quaternion_to_godot_quaternion(&previous_bone->rotation)), - UltraleapTypes::ultraleap_vector3_to_godot_vector3(&previous_bone->prev_joint) - ); - - Transform3D current_bone_transform = Transform3D( - Basis(ul_bone->orientation), - ul_bone->prev_joint - ); - - Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform; - ul_bone->rotation = relative_bone_transform.basis.get_quaternion(); - ul_bone->translation = relative_bone_transform.origin; - } + previous_bone->next_bone = bone; + bone->previous_bone = previous_bone; // If palm is defined, this bone is either a metacarpal or an arm. We still compute relative transform though. if (palm != NULL) { @@ -169,17 +155,34 @@ void UltraleapBone::fill_bone_data(Ref ul_bone, LEAP_BONE* bone, ); Transform3D current_bone_transform = Transform3D( - Basis(ul_bone->orientation), - type == UltraleapBone::BoneType::Arm ? ul_bone->next_joint : ul_bone->prev_joint + Basis(bone->orientation), + type == UltraleapBone::BoneType::Arm ? bone->next_joint : bone->prev_joint ); Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform; - ul_bone->rotation = relative_bone_transform.basis.get_quaternion(); - ul_bone->translation = relative_bone_transform.origin; + bone->rotation = relative_bone_transform.basis.get_quaternion(); + bone->translation = relative_bone_transform.origin; } } void UltraleapBone::compute_bone_data(Ref bone) { bone->center = (bone->prev_joint + bone->next_joint) / 2.0f; bone->length = (bone->prev_joint - bone->next_joint).length(); + + // If previous_bone is defined, we compute the transform from the previous bone to the current bone + if (bone->previous_bone != NULL) { + Transform3D previous_bone_transform = Transform3D( + Basis(bone->previous_bone->orientation), + bone->previous_bone->prev_joint + ); + + Transform3D current_bone_transform = Transform3D( + Basis(bone->orientation), + bone->prev_joint + ); + + Transform3D relative_bone_transform = previous_bone_transform.inverse() * current_bone_transform; + bone->rotation = relative_bone_transform.basis.get_quaternion(); + bone->translation = relative_bone_transform.origin; + } } \ No newline at end of file diff --git a/src/bone.h b/src/bone.h index a82bc42..03a31b6 100644 --- a/src/bone.h +++ b/src/bone.h @@ -42,6 +42,9 @@ public: Quaternion orientation; Quaternion rotation; + Ref previous_bone; + Ref next_bone; + // Getters / Setters BoneType get_type() { return type; } void set_type(BoneType value) { type = value; } @@ -70,7 +73,7 @@ public: Quaternion get_rotation() { return rotation; } void set_rotation(Quaternion value) { rotation = value; } - static void fill_bone_data(Ref ul_bone, LEAP_BONE* bone, BoneType type, LEAP_BONE* previous_bone, LEAP_PALM* palm); + static void fill_bone_data(Ref bone, Ref previous_bone, LEAP_BONE* leap_bone, BoneType type, LEAP_PALM* palm = NULL); static void compute_bone_data(Ref bone); protected: static void _bind_methods(); diff --git a/src/digit.cpp b/src/digit.cpp index 625bdcc..b770be4 100644 --- a/src/digit.cpp +++ b/src/digit.cpp @@ -139,16 +139,16 @@ void UltraleapDigit::fill_digit_data(Ref ul_digit, LEAP_DIGIT* d // We need some extra logic here because the metacarpal doesn't exist in Ultraleap's hand data if (type == UltraleapDigit::FingerType::Thumb) { - UltraleapBone::fill_bone_data(ul_digit->proximal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal, NULL, palm); + UltraleapBone::fill_bone_data(ul_digit->proximal_ref, NULL, &digit->proximal, UltraleapBone::BoneType::Proximal, palm); // Still filling it out but shouldn't be used - UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, NULL, palm); + UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, NULL, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, palm); } else { - UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, NULL, palm); - UltraleapBone::fill_bone_data(ul_digit->proximal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal, &digit->metacarpal, NULL); + UltraleapBone::fill_bone_data(ul_digit->metacarpal_ref, NULL, &digit->metacarpal, UltraleapBone::BoneType::Metacarpal, palm); + UltraleapBone::fill_bone_data(ul_digit->proximal_ref, ul_digit->metacarpal_ref, &digit->proximal, UltraleapBone::BoneType::Proximal); } - UltraleapBone::fill_bone_data(ul_digit->intermediate_ref, &digit->intermediate, UltraleapBone::BoneType::Intermediate, &digit->proximal, NULL); - UltraleapBone::fill_bone_data(ul_digit->distal_ref, &digit->distal, UltraleapBone::BoneType::Distal, &digit->intermediate, NULL); + UltraleapBone::fill_bone_data(ul_digit->intermediate_ref, ul_digit->proximal_ref, &digit->intermediate, UltraleapBone::BoneType::Intermediate); + UltraleapBone::fill_bone_data(ul_digit->distal_ref, ul_digit->intermediate_ref, &digit->distal, UltraleapBone::BoneType::Distal); } \ No newline at end of file diff --git a/src/hand.cpp b/src/hand.cpp index 098f2a0..7fa6873 100644 --- a/src/hand.cpp +++ b/src/hand.cpp @@ -266,7 +266,7 @@ void UltraleapHand::fill_hand_data(Ref ul_hand, LEAP_HAND* hand) UltraleapDigit::fill_digit_data(ul_hand->get_ring(), &hand->ring, UltraleapDigit::FingerType::Ring, &hand->palm); UltraleapDigit::fill_digit_data(ul_hand->get_pinky(), &hand->pinky, UltraleapDigit::FingerType::Pinky, &hand->palm); - UltraleapBone::fill_bone_data(ul_hand->arm_ref, &hand->arm, UltraleapBone::BoneType::Arm, NULL, &hand->palm); + UltraleapBone::fill_bone_data(ul_hand->arm_ref, NULL, &hand->arm, UltraleapBone::BoneType::Arm, &hand->palm); ul_hand->wrist = Transform3D(Basis(ul_hand->palm_ref->orientation), ul_hand->arm_ref->next_joint); } \ No newline at end of file