How do I get orientation data of an ARImageAnchor
in relation to a given SCNNode
in ARKit
? This is the basic scenario.
A node placed at some point after the AR session has been started. Let's say its named originNode. Then ARKit
is used to scan for image placed in the environment. When the camera detects an image, I want to calculate the X, Y, Z and the orientation (tilt
, pan
, roll
) of the detected image relative to the originNode. Please note that the device is fixed at UIDeviceOrientation.landscapeRight from the start.
So the user can either scan for images while holding the device horizontally and camera pointing forward, or device is horizontal and camera is pointing directly down.
When I scan an image placed flat on a table, the values I'm getting with my current code is accurate I believe
Configuration:
let configuration = ARWorldTrackingConfiguration()
guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "AR Resources",
bundle: nil)
else { fatalError("Missing expected asset catalog resources.") }
configuration.detectionImages = referenceImages
configuration.maximumNumberOfTrackedImages = 100
Delegate:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
if let imageAnchor = anchor as? ARImageAnchor {
let anchorPositionRelativeToOrigin = node.convertPosition(SCNVector3Zero, to: originNode)
let euler2 = node.eulerAngles
print("Tilt: \(euler2.x.rad2deg()), Pan: \(euler2.y.rad2deg()), Roll: \(euler2.z.rad2deg())")
let anchorTransformRelativeToHome = node.convertTransform(SCNMatrix4(), to: originNode)
let rotation = simd_float4x4(anchorTransformRelativeToHome)
let direction = SIMD3<Float>(-rotation.columns.2.x, -rotation.columns.2.y, -rotation.columns.2.z)
let tiltAngle = atan2(direction.y, direction.z)
let panAngle = atan2(direction.x, direction.z)
let rollAngle = atan2(-rotation.columns.1.x, rotation.columns.0.x)
print("Tilt: \(tiltAngle.rad2deg()) Pan: \(panAngle.rad2deg()) roll: \(rollAngle.rad2deg())")
}
}
These are the two print results I get no matter which orientation I place the image.
Tilt: 0.0, Pan: -0.0, Roll: 0.0
Tilt: -180.0 Pan: -180.0 roll: -0.0
Any help would be much appreciated.