Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARKit 1.5 how to get the rotation of a vertical plane

Tags:

ios

swift

arkit

I'm experimenting with the vertical plane, and I'm trying to place a node on a wall with the correct rotation based on that vertical plane.

here's the ARHitTestResult of the vertical plane that gets tapped:

let hitLocation = sceneView.hitTest(touchPoint, types: .existingPlaneUsingExtent)

I've tried the following:

let hitRotation = hitLocation.first?.worldTransform.columns.2

and

let anchor = hitLocation.first?.anchor
let hitRotation = anchor?.transform.columns.2

neither one of them seem to work.

And this is what I'm hoping to do:

boardNode.eulerAngles = SCNVector3((hitRotation?.x)!, (hitRotation?.y)!, 0)

I'd much appreciate it if someone could help me out with this as I can't find a lot of tutorials on ARKit yet.

EDIT

so here's what worked (thanks to Josh):

let hit = sceneView.hitTest(touchPoint, types: .existingPlaneUsingExtent)
let planeAnchor = hit.first?.anchor as? ARPlaneAnchor

guard let anchoredNode =  sceneView.node(for: planeAnchor!) else { return }

let anchorNodeOrientation = anchoredNode.worldOrientation

boardNode.eulerAngles.y = .pi * anchorNodeOrientation.y

Note: .worldOrientation is way more accurate than .rotation in this case, just wanted to mention this.

like image 483
LeftyT Avatar asked Feb 27 '18 14:02

LeftyT


1 Answers

Is this what your looking for? Here I am using the screen center for the CGPoint value, but you can use touch etc:

func addObjectToScreen() {

        if let hit = self.sceneView?.hitTest(self.viewCenter, types: [.existingPlaneUsingExtent]).last {

            let hitTestTransform = SCNMatrix4(hit.worldTransform)

            let vector = SCNVector3Make(hitTestTransform.m41, hitTestTransform.m42, hitTestTransform.m43)

            node.position = vector

            return
        }
}

Update: If you want to rotation of the node associated with the Plane couldn't you could get it like so:

 func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

      print(node.rotation)

}

Then use it as you need?

Further Update:

   override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

 /*
 1. Get The Current Touch Location
 2. Check That We Have Touched A Valid Node
 3. Check That Our Touched Object Is An ARPlane Anchor
 */

 guard let touchLocation = touches.first?.location(in: augmentedRealityView),
 let hitTest = augmentedRealityView.hitTest(touchLocation, types: .existingPlaneUsingExtent).first,
 let planeAnchor = hitTest.anchor as? ARPlaneAnchor
 else {

 // No Valid Plane Has Been Detected So Hide The Plane Information Label

 return

 }

    //We Have A Valid Plane So Display It's Current Info
    guard let anchoredNode =  augmentedRealityView.node(for: planeAnchor) else { return }
    print(anchoredNode.rotation)

 }
like image 148
BlackMirrorz Avatar answered Nov 15 '22 07:11

BlackMirrorz