(Swift 4)
Hey, you can use this simpler function if you want to place the object in a certain position relative to another node (for example, the camera node), as well as in the same orientation as the reference node:
func updatePositionAndOrientationOf(_ node: SCNNode, withPosition position: SCNVector3, relativeTo referenceNode: SCNNode) {
let referenceNodeTransform = matrix_float4x4(referenceNode.transform)
var translationMatrix = matrix_identity_float4x4
translationMatrix.columns.3.x = position.x
translationMatrix.columns.3.y = position.y
translationMatrix.columns.3.z = position.z
let updatedTransform = matrix_multiply(referenceNodeTransform, translationMatrix)
node.transform = SCNMatrix4(updatedTransform)
}
If you want to put a 'node' in 2 meters right in front of a specific 'cameraNode', you would call it like this:
let position = SCNVector3(x: 0, y: 0, z: -2)
updatePositionAndOrientationOf(node, withPosition: position, relativeTo: cameraNode)
Change: Getting Camera Node
To get the camera node, it depends on whether you use SCNKit, ARKit, or another framework. The following are examples for ARKit and SceneKit.
ARKit ARSCNView SCNScene, . ARSCNView pointOfView:
let cameraNode = sceneView.pointOfView
SceneKit SCNView, SCNScene. , , - :
let scnScene = SCNScene()
scnView.scene = scnScene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3(0, 5, 10)
scnScene.rootNode.addChildNode(cameraNode)
, ARKit:
let cameraNode = scnView.pointOfView