I'm very new to ARKit (2 days old to be precise) and I'm trying to place a node on my ARSCNView when tapping and I am able to successfully do so 50% of the time. The other half of the time, nothing appears on screen. I've narrowed it down to the following line of code:
func getCameraTransform(for camera: ARCamera) -> MDLTransform {
return MDLTransform(matrix: camera.transform)
}
When nothing is appearing on screen, the above function is returning a transform.translation of float3(0.0, 0.0, 0.0)
I've checked and the camera.transform is providing a value that looks like simd_float4x4([[0.98134, 0.0198354, -0.191256, 0.0)], [-0.0129627, 0.999227, 0.0371193, 0.0)], [0.191844, -0.0339474, 0.980838, 0.0)], [-0.482363, -0.00405288, -0.768801, 1.0)]]) transform.translation is 0.
Why does this return 0 sometimes but work perfectly fine other times I tap?
Here is the full code:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard touches.first!.tapCount == 1 else { return }
let camera = sceneView.session.currentFrame!.camera
let transform = getCameraTransform(for: camera)
let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
let cube = SCNNode(geometry: boxGeometry)
// SOMETIMES TRANSFORM.TRANSLATION IS JUST 0s
let position = SCNVector3(
transform.translation.x,
transform.translation.y,
transform.translation.z
)
cube.position = position
sceneView.scene.rootNode.addChildNode(cube)
}
func getCameraTransform(for camera: ARCamera) -> MDLTransform {
return MDLTransform(matrix: camera.transform) // this is returning the 0 translation every other time
}