Skip to content

Commit 3e0f217

Browse files
committed
Add snippet to demonstrate how to detect a basic stop gesture
1 parent f7e08bd commit 3e0f217

File tree

1 file changed

+25
-0
lines changed
  • xr/src/main/java/com/example/xr/arcore

1 file changed

+25
-0
lines changed

xr/src/main/java/com/example/xr/arcore/Hands.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import androidx.xr.runtime.internal.GltfEntity
3030
import androidx.xr.runtime.math.Pose
3131
import androidx.xr.runtime.math.Quaternion
3232
import androidx.xr.runtime.math.Vector3
33+
import androidx.xr.runtime.math.toRadians
3334
import androidx.xr.scenecore.scene
3435
import kotlinx.coroutines.flow.Flow
3536
import kotlinx.coroutines.launch
@@ -117,3 +118,27 @@ fun ComponentActivity.renderPlanetAtFingerTip(rightHandState: Hand.State) {
117118
indexFingerEntity.setPose(Pose(position, rotation))
118119
// [END androidxr_arcore_hand_entityAtIndexFingerTip]
119120
}
121+
122+
private fun detectPinch(session: Session, handState: Hand.State): Boolean {
123+
// [START androidxr_arcore_hand_pinch_gesture]
124+
val thumbTip = handState.handJoints[HandJointType.THUMB_TIP] ?: return false
125+
val thumbTipPose = session.scene.perceptionSpace.transformPoseTo(thumbTip, session.scene.activitySpace)
126+
val indexTip = handState.handJoints[HandJointType.INDEX_TIP] ?: return false
127+
val indexTipPose = session.scene.perceptionSpace.transformPoseTo(indexTip, session.scene.activitySpace)
128+
return Vector3.distance(thumbTipPose.translation, indexTipPose.translation) < 0.05
129+
// [END androidxr_arcore_hand_pinch_gesture]
130+
}
131+
132+
private fun detectStop(session: Session, handState: Hand.State): Boolean {
133+
// [START androidxr_arcore_hand_stop_gesture]
134+
val threshold = toRadians(angleInDegrees = 30f)
135+
fun pointingInSameDirection(joint1: HandJointType, joint2: HandJointType): Boolean {
136+
val forward1 = handState.handJoints[joint1]?.forward ?: return false
137+
val forward2 = handState.handJoints[joint2]?.forward ?: return false
138+
return Vector3.angleBetween(forward1, forward2) < threshold
139+
}
140+
return pointingInSameDirection(HandJointType.INDEX_PROXIMAL, HandJointType.INDEX_TIP) &&
141+
pointingInSameDirection(HandJointType.MIDDLE_PROXIMAL, HandJointType.MIDDLE_TIP) &&
142+
pointingInSameDirection(HandJointType.RING_PROXIMAL, HandJointType.RING_TIP)
143+
// [END androidxr_arcore_hand_stop_gesture]
144+
}

0 commit comments

Comments
 (0)