Reputation: 473
Alright, so I'm currently in the process of creating an application that allows you to download certain objects and then display those objects in real space using ARkit from Apple. I have been able to download the item and display it real space but the position is all off and for some reason I can't get the roots node child node. This is part of what I have so far:
@objc func tapped(sender: UITapGestureRecognizer){
let sceneView = sender.view as! ARSCNView
let tapLocation = sender.location(in: sceneView)
let hitTest = sceneView.hitTest(tapLocation, types: .existingPlaneUsingExtent)
print(hitTest)
if !hitTest.isEmpty{
print("in tapped part")
self.sceneView.scene.rootNode.enumerateChildNodes({ (node, stop) in
node.removeFromParentNode()
})
self.addItem(hitTestResult: hitTest.first!)
}else{
print("not in tapped")
}
}
func addItem(hitTestResult: ARHitTestResult){
print("in add item part")
self.node = scene!.rootNode
let transform = hitTestResult.worldTransform
let thirdColumn = transform.columns.3
self.node!.position = SCNVector3Make(0, 0, 0)
self.sceneView.scene.rootNode.addChildNode(self.node!)
}
I tried doing this:
self.node = scene!.rootNode.childNode(withName: "Cow", recursively: false)
But I kept getting nil as the result. The name of the object file is Cow and I even tried naming it Cow.obj but that didn't work either. I'm pretty sure that's the reason my positioning is off. Does it have to do with the name that I use in the childNode method? I can post more code if needed.
Upvotes: 0
Views: 1902
Reputation: 144
This is what I have to receive the location of where they touched.
guard let touch = touches.first else {return} //Get's touch on screen
let result = sceneView.hitTest(touch.location(in: sceneView), types: ARHitTestResult.ResultType.featurePoint) //searches for real world objects
guard let pointResult = result.last else {return} //a hit test result (where the point was clicked)
let pointTransform = SCNMatrix4(pointResult.worldTransform) //turns the point into a point on the world grid
let pointVector = SCNVector3Make(pointTransform.m41, pointTransform.m42, pointTransform.m43) //the X, Y, and Z of the clicked cordinate
So it looks like you need to turn your hitTestResult into a SCNMatrix4 location, then make a SCNVector3Make using the SCNMatrix4.m41, m42, and m43. Then, make the position of your node by using that scnvector3make.
Upvotes: 1