I'm experimenting with ARKit and I'm trying to place some models around the user. So what I want is that when the app starts it just places some models around the user so He needs to find them.
When he moves like for example 10 meters I want to add some random models again. I thought I could do it this way:
let cameraTransform = self.sceneView.session.currentFrame?.camera.transform
let cameraCoordinates = MDLTransform(matrix: cameraTransform!)
let camX = CGFloat(cameraCoordinates.translation.x)
let camY = CGFloat(cameraCoordinates.translation.y)
let cameraPosition = CGPoint(x: camX, y: camY)
let anchors = self.sceneView.hitTest(cameraPosition, types: [.featurePoint, .estimatedHorizontalPlane])
if let hit = anchors.first {
let hitTransform = SCNMatrix4(hit.worldTransform)
let hitPosition = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)
self.sceneView.session.add(anchor: ARAnchor(transform: hit.worldTransform))
return Coordinate(hitPosition.x, hitPosition.y, hitPosition.z)
}
return Coordinate(0, 0, 0)
}
The problem is sometimes it doesn't find any anchors and then I don't know what to do. And when it finds some anchors it is randomly placed behind me not in front of me but behind me. I don't know why because never turn the camera so it can't find any anchors.
Is there a better way to place random models in the real world?