2017-07-16 4 views
0

Ich versuche zwei SCNNodes zusammen auszurichten, aber ich kann nicht herausfinden, wie. Dies ist, was ich gerade habe:Wie richtet man 2 SCNNode zusammen horizontal aus?

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) 

Wie kann ich das erreichen? Vielen Dank!!

Antwort

0

Um sie horizontal auszurichten, setzen Sie die position Y-Werte auf den gleichen Wert. X ist links-rechts, Y ist oben-unten, Z ist nah-fern (bis Sie anfangen, die Kamera herum zu bewegen!).

+0

ich das täte, und sie sind immer noch nicht horizontal und vertikal zusammen :(ausgerichtet ist, änderte ich die Y und Z auf der .position Teil –

+0

Bearbeiten Sie Ihre Original-Beitrag einen Screenshot aufzunehmen, was Sie haben, und erklären, wie das unterscheidet sich von dem, was Sie wollen. –

+0

in Ordnung, habe es getan! hoffe, es hilft mehr! –

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

Dies ist nicht einmal horizontal ausgerichtet ... es ist nur die Objekte an einem zufälligen Ort im Grunde platzieren –

Verwandte Themen