0

I'm trying to align two SCNNodes together but can't seem to figure out how. This is what I have right now:

cube.scale = SCNVector3(x: 0.1, y: 0.1, z: 0.1)
cube.position = SCNVector3(0, 3, -3)

cubeTwo.scale = SCNVector3(x: 0.15, y: 0.15, z: 0.15)
cubeTwo.position = SCNVector3(0.5, 3, -3)

cubeThree.scale = SCNVector3(x: 0.2, y: 0.2, z: 0.2)
cubeThree.position = SCNVector3(1, 3, -3)

How can I achieve this? Thank you!!

Amit Kalra
  • 4,085
  • 6
  • 28
  • 44

2 Answers2

0

To align them horizontally, set the position Y values to be the same. X is left-right, Y is up-down, Z is near-far (until you start moving the camera around!).

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • I did that, and they're still not aligned horizontally and vertically together :(, I changed the Y and Z on the .position part – Amit Kalra Jul 16 '17 at 23:24
  • Edit your original post to include a screenshot of what you have, and explain how that differs from what you want. – Hal Mueller Jul 16 '17 at 23:25
0
parentNode.addChildNode(cube)
parentNode.addChildNode(cubeTwo)
parentNode.addChildNode(cubeThree)


func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

    parentNode.position = self.getPositionRelativeToCameraView(distance: 1.0).position


}


func getPositionRelativeToCameraView(distance: Float) -> (position: SCNVector3, rotation: SCNVector4) {
    var x = Float()
    var y = Float()
    var z = Float()

    let cameraLocation = self.sceneView.pointOfView!.position //else { return (SCNVector3Zero) }
    let rotation = self.sceneView.pointOfView!.rotation //else { return (SCNVector3Zero) }
    let direction = calculateCameraDirection(cameraNode: rotation)

    x = cameraLocation.x + distance * direction.x
    y = cameraLocation.y + distance * direction.y
    z = cameraLocation.z + distance * direction.z

    let position = SCNVector3Make(x, y, z)
    return (position, rotation)
}

func calculateCameraDirection(cameraNode: SCNVector4) -> SCNVector3 {
    let x = -cameraNode.x
    let y = -cameraNode.y
    let z = -cameraNode.z
    let w = cameraNode.w
    let cameraRotationMatrix = GLKMatrix3Make(cos(w) + pow(x, 2) * (1 - cos(w)),
                                              x * y * (1 - cos(w)) - z * sin(w),
                                              x * z * (1 - cos(w)) + y*sin(w),

                                              y*x*(1-cos(w)) + z*sin(w),
                                              cos(w) + pow(y, 2) * (1 - cos(w)),
                                              y*z*(1-cos(w)) - x*sin(w),

                                              z*x*(1 - cos(w)) - y*sin(w),
                                              z*y*(1 - cos(w)) + x*sin(w),
                                              cos(w) + pow(z, 2) * ( 1 - cos(w)))

    let cameraDirection = GLKMatrix3MultiplyVector3(cameraRotationMatrix, GLKVector3Make(0.0, 0.0, -1.0))
    return SCNVector3FromGLKVector3(cameraDirection)
}
Juani
  • 1