実装メモ:デバイス座標系における Entity の座標を求める

以下実装のために、デバイス座標系における Entity の座標を求める必要があった。

trackingMode = .continuous とした AnchorEntity(.head) の座標系に変換すれば簡単にできるかと思ったが、デバイス位置にアンカーがうまく追従されず、原因は分からなかったが先に進めたかったので、座標変換して求めることにした。

デバイス→ワールド座標系の変換行列 の逆行列をとれば、ワールド→ワールド座標系の変換行列となり、ワールド座標系における任意の座標をデバイス座標系に変換することができる。

let worldTracking = WorldTrackingProvider()

func positionInDeviceSpace(of entity: Entity) -> SIMD3<Float> {
    guard let deviceAnchor = worldTracking.queryDeviceAnchor(atTimestamp: CACurrentMediaTime()) else { return .zero }
    let worldFromDevice = deviceAnchor.originFromAnchorTransform
    let deviceFromWorld = worldFromDevice.inverse
    let worldFromEntity = entity.transformMatrix(relativeTo: nil)
    let deviceFromEntity = deviceFromWorld * worldFromEntity
    return SIMD3<Float>(
        deviceFromEntity.columns.3.x,
        deviceFromEntity.columns.3.y,
        deviceFromEntity.columns.3.z
    )
}

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny