简体   繁体   中英

How to attach SCNNode fixed to ARKit's pointOfView?

I'm trying to do something simple and I'm running into an issue that breaks the ARKit experience.

I want to attach a 3d model (SCNNode) fixed to a spot on the phone's screen. I'm able to do that, but the experience seems broken at the moment. When I add my node to the scene, my node has about a second or so to move around before it freezes in place. This initial movement is really bad because the node can freeze out of the camera's view (so you don't see it on the screen).

The code I'm using:

let scene = SCNScene(named: "resources.scnassets/model.scn")!
let node = scene.rootNode.childNode(withName: "model", recursively: true)!

// this puts the node in front & slightly below the camera
let orientation = SCNVector3(x: 0, y: -0.4, z: -1)

node.position = orientation

let physicsBody = SCNPhysicsBody(
    type: .static,
    shape: SCNPhysicsShape(geometry: SCNSphere(radius: 0.1))
)
node.physicsBody = physicsBody

sceneView.pointOfView?.addChildNode(node)

This code is simple, it adds the node in front of the scene's camera node. But I'm running into the issue where when the node is added to the scene, it has a second to move around the screen (as I move the phone) before it freezes in place. What I need to do is have the node appear on the screen in a fixed place (fixed to the camera's point of view).

I don't want to anchor my node to a spot in the real world. I want my node's location to be dependent on the camera's point of view.

What is the proper way to attach a node so that it stays in the same spot on the phone screen?

You should use .kinematic instead of .static

Use kinematic bodies for scene elements that you want to control directly directly but whose movement manipulates other elements. For example, to allow the user to push objects around with a finger, you might create a kinematic body and attach it to an invisible node that you move to follow touch events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM