1
votes

I want to place a car object on the plane.

I am setting the sceneview like this.

func setUpSceneView() {
    let configuration = ARWorldTrackingConfiguration()
    configuration.planeDetection = .horizontal

    sceneView.session.run(configuration)

    sceneView.delegate = self
    sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]
}

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { // 1 guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

// 2
let width = CGFloat(planeAnchor.extent.x)
let height = CGFloat(planeAnchor.extent.z)
let plane = SCNPlane(width: width, height: height)

// 3
plane.materials.first?.diffuse.contents = UIColor.transparentLightBlue

// 4
let anchorNode = SCNScene(named: "art.scnassets/car.scn")!.rootNode

// 5
let x = CGFloat(planeAnchor.center.x)
let y = CGFloat(planeAnchor.center.y)
let z = CGFloat(planeAnchor.center.z)
planeNode.position = SCNVector3(x,y,z)
planeNode.eulerAngles.x = -.pi / 2


node.addChildNode(anchorNode)[car object][1]
}

https://app.box.com/s/vdloxlqxk9rh6h4k5ggwrxm1hslktn8g

I am able to place the car but it is allover camera scene . can any one tell me problem with cooridnate system or 3D object.

1
Does your object place correctly on the plane when you add it or it just places on the front of the camera view?M Reza
By all over camera, do you mean it moves along with camera? Can you show a screenshot of result after car is placed.Alok Subedi

1 Answers

0
votes

This depends on how you want to place the car. Right now your app will place an anchor and where it decides to create its first anchor is where your car will arrive. If you would like to update your anchor as you scan, you need to call didUpdate and your anchor will move to the center of the extent of your plane:

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

However, it looks as though you have already defined the size you want your anchor to be. I've never tried it that way before and I'm not sure if you can programatically move your anchor to a desired location. In my mind it would cause issues if the app doesn't already know its a horizontal surface, but I've never tested it.

Instead what I would recommend is to create your plane as you did above (without declaring its size). Update your plane size with the 'didUpdate' function above. Then if you want to place your car in a predetermined spot, run a hittest.

Here is a good resource to walk you through: https://www.appcoda.com/arkit-horizontal-plane/