2017-07-16 71 views
0

我試圖對齊兩個SCNNodes在一起,但似乎無法弄清楚如何。這就是我現在所擁有的:如何將2個SCNNode水平對齊?

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) 

我該如何做到這一點?謝謝!!

回答

0

要水平對齊它們,請將position的Y值設置爲相同。 X是左右的,Y是上下的,Z是近的(直到你開始移動相機!)。

+0

我這樣做,而且他們仍然沒有水平和垂直排列在一起:(,我改變了Y和Z的.POSITION部分 –

+0

編輯您的原始信息,包括你有什麼截圖,和 –

+0

好吧,已經做到了!希望它有更多的幫助! –

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) 
} 
+0

這甚至沒有水平對齊......它只是將對象放置在一個隨機的地方 –