From 113a5e0380a985aba6dd936a0f81a6178efaca94 Mon Sep 17 00:00:00 2001 From: James Nachbar Date: Thu, 20 Jun 2024 22:21:25 -0700 Subject: [PATCH 1/2] Change parentFromJointTranform to anchorFromJointTransform parentFromJointTransform only gives the transform from the previous joint (the distal interphalangeal joint in the case of the fingertip), whereas anchorFromJointTransform gives the transform between the index fingertip and the wrist Also, add commented-out diagnostic code for walking the skeleton back to the wrist, reporting the joints and the distances --- TimeForCube/TimeForCubeViewModel.swift | 40 ++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/TimeForCube/TimeForCubeViewModel.swift b/TimeForCube/TimeForCubeViewModel.swift index a66416b..acfd63b 100644 --- a/TimeForCube/TimeForCubeViewModel.swift +++ b/TimeForCube/TimeForCubeViewModel.swift @@ -37,6 +37,16 @@ import RealityKit print ("failed to start session: \(error)") } } + + /* + // Function to return the translation distance from a transform; for inspecting the various transforms + func distanceTranslationFromTransform(_ transform: simd_float4x4) -> Float { + let translation = transform.columns.3 + let translationVector = SIMD3(translation.x, translation.y, translation.z) + let translationDistance = sqrt( pow(translationVector.x, 2) + pow(translationVector.y, 2) + pow(translationVector.z, 2)) + return translationDistance + } + */ func processHandUpdates() async { for await update in handTracking.anchorUpdates { @@ -48,8 +58,34 @@ import RealityKit guard ((fingerTip?.isTracked) != nil) else { continue } - let originFromWrist = handAnchor.originFromAnchorTransform - let wristFromIndex = fingerTip?.parentFromJointTransform + let originFromWrist = handAnchor.originFromAnchorTransform // location of the 'hand' (wrist) in world space + + // walk the skeleton back to the wrist, printing out names and distances + // sequence: + // .indexFingerTip = tip of finger + // .indexFingerIntermediateTip = DIP joint + // .indexFingerIntermediateBase = PIP joint + // .indexFingerKnuckle = MP joint + // .indexFingerMetacarpal = metacarpal shaft + // .wrist = wrist, which has no parent, and pfj and afj are 0.0 + // + /* // uncomment to see the components of the skeleton and how they relate to each other + // (will also have to uncomment the function above) + var thisJoint = fingerTip + while (thisJoint != nil) { + let nextJoint = thisJoint!.parentJoint + let jointName = thisJoint!.name + let jointDescription = thisJoint!.description // description is really long, & spells out components of transform + let pfj = thisJoint!.parentFromJointTransform + let afj = thisJoint!.anchorFromJointTransform + print("Joint \(jointName) has pfj \(distanceTranslationFromTransform(pfj)) and afj \(distanceTranslationFromTransform(afj))") + thisJoint = nextJoint + } + */ + + // anchorFromJointTransform gives transfrom from base joint of skeleton; parent just gives transform from immediate prior joint + + let wristFromIndex = fingerTip?.anchorFromJointTransform let originFromIndex = originFromWrist * wristFromIndex! fingerEntities[handAnchor.chirality]?.setTransformMatrix(originFromIndex, relativeTo: nil) From 6ad06d1a297431f419efe45b2c29b644a6643ff7 Mon Sep 17 00:00:00 2001 From: James Nachbar Date: Thu, 20 Jun 2024 22:23:53 -0700 Subject: [PATCH 2/2] Add comment to line of code that reveals the fingertip tracker --- TimeForCube/ModelEntity+Extension.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TimeForCube/ModelEntity+Extension.swift b/TimeForCube/ModelEntity+Extension.swift index 67f5198..bb7021c 100644 --- a/TimeForCube/ModelEntity+Extension.swift +++ b/TimeForCube/ModelEntity+Extension.swift @@ -19,7 +19,7 @@ extension ModelEntity { ) entity.components.set(PhysicsBodyComponent(mode: .kinematic)) - entity.components.set(OpacityComponent(opacity: 0.0)) + entity.components.set(OpacityComponent(opacity: 0.0)) // comment out this line to visualize the fingertip tracker return entity }