Jetpack XR için ARCore kullanarak bir cihazın duruşunu izleme

Uygun XR cihazlar
Bu kılavuz, bu tür XR cihazlar için deneyimler geliştirmenize yardımcı olur.
XR Kulaklığı
Kablolu XR Gözlükleri

Jetpack XR için ARCore ile uygulamanız, cihazın duruşunu (cihazın dünya orijinine göre yönü [eğim, sapma, yuvarlanma] ve konumu [X, Y, Z]) alabilir.

Bu bilgileri kullanarak dijital içeriği gerçek dünyada oluşturabilir veya cihazın duruşunu coğrafi duruşa dönüştürerek konuma duyarlı veriler oluşturabilirsiniz.

Oturuma erişme

Uygulamanızın oluşturması gereken Jetpack XR Runtime Session aracılığıyla cihazın duruş bilgilerine erişme

Oturumu yapılandırma

Cihaz duruş bilgileri, XR oturumlarında varsayılan olarak etkin değildir. Uygulamanızın cihazın duruş bilgilerini alabilmesi için oturumu yapılandırın ve DeviceTrackingMode.SPATIAL modunu ayarlayın:

// Define the configuration object to enable tracking device pose.
val newConfig = Config.Builder(session.config)
    .setDeviceTracking(DeviceTrackingMode.SPATIAL)
    .build()
// Apply the configuration to the session.
try {
    when (val configResult = session.configure(newConfig)) {
        is SessionConfigureSuccess -> {
            // The session is now configured to track the device's pose.
        }
        else -> {
            // Catch-all for other configuration errors returned using the result class.
        }
    }
} catch (e: UnsupportedOperationException) {
    // Handle configuration failure. For example, if the specific mode is not supported on the current device or API version.
}

DeviceTrackingMode.SPATIAL modu tüm XR cihazlarda desteklenmez. Session.configure() başarılı olursa cihaz bu modu destekliyor demektir.

Cihazın duruşunu alma

Oturumu yapılandırdıktan sonra, ArDevice nesnesini kullanarak cihazın AR koordinat sistemindeki duruşunu elde edebilirsiniz:

// Get the ArDevice instance
val arDevice = ArDevice.getInstance(session)
// There are two ways to get the device pose.

// 1. Get the current device pose once.
// This is the device's position and orientation relative to the tracking origin.
val devicePose = arDevice.state.value.devicePose
processDevicePose(devicePose)

// 2. Continuously receive updates for the device pose.
// `collect` is a suspending function that will run indefinitely and process new poses.
arDevice.state.collect { state ->
    processDevicePose(state.devicePose)
}

Cihaz pozunun çevirisini ve rotasyonunu alma

Cihaz Pose, cihazın izleme kaynağına göre konumunu (çeviri) ve yönünü (döndürme) temsil eder. Uygulamanızın deneyimini iyileştirmek için bu bilgileri uygulamanızda kullanın:

  • Konum açısından doğru navigasyon talimatları sağlama: Kullanıcının yönünü bulmasına ve yer paylaşımlı dijital içeriklerin yardımıyla çevresinde gezinmesine yardımcı olmak için konum verilerini kullanın.

fun processDevicePose(pose: Pose) {
    // Extract Translation and Rotation
    val translation = pose.translation // Vector3(x, y, z)
    val rotation = pose.rotation // Quaternion (x, y, z, w)
    TODO(/* Use the translation and rotation in your app. */)
}