2017-10-16 20 views
0

상자 위에 구를 넣으려고하는데 위치가 이상합니다. 상자 안에 공이 절반 숨겨져 있습니다. 상자를 바꾸고 피벗 피벗을 시도했지만 도움이되지 않았습니다.SceneKit - 다른 SCNNode에 SCNNode 추가하기

let cubeGeometry = SCNBox(width: 10, height: 10, length: 10, 
    chamferRadius: 0) 
    let cubeNode = SCNNode(geometry: cubeGeometry) 
    //cubeNode.pivot = SCNMatrix4MakeTranslation(0, 1, 0) 
    scene.rootNode.addChildNode(cubeNode) 

    let ballGeometry = SCNSphere(radius: 1) 
    let ballNode = SCNNode(geometry: ballGeometry) 
    ballNode.pivot = SCNMatrix4MakeTranslation(0.5, 0, 0.5) 
    ballNode.position = SCNVector3Make(0, 5, 0) 
    cubeNode.addChildNode(ballNode)` 

및 결과 : 내가 잘못 뭘하는지 enter image description here

여기 코드는? 상자의 윗면에 공을 올려 놓는 법?

UPDATE : 내가 대신 볼의 큐브를 추가하면 내가

답변

1

. 따라서, 당신은해야합니다

Ball on top of the cube

관련 전체 코드 :

override func viewDidLoad() { 
    super.viewDidLoad() 

    // create a new scene 
    let scene = SCNScene() 

    let cubeGeometry = SCNBox(width: 10, height: 10, length: 10, 
           chamferRadius: 0) 
    cubeGeometry.firstMaterial?.diffuse.contents = UIColor.yellow 
    let cubeNode = SCNNode(geometry: cubeGeometry) 
    scene.rootNode.addChildNode(cubeNode) 

    let ballGeometry = SCNSphere(radius: 1) 
    ballGeometry.firstMaterial?.diffuse.contents = UIColor.green 
    let ballNode = SCNNode(geometry: ballGeometry) 
    ballNode.position = SCNVector3Make(0, 6, 0) 
    cubeNode.addChildNode(ballNode) 

    // retrieve the SCNView 
    let scnView = self.view as! SCNView 

    // set the scene to the view 
    scnView.scene = scene 

    // allows the user to manipulate the camera 
    scnView.allowsCameraControl = true 

    // show statistics such as fps and timing information 
    scnView.showsStatistics = true 

    // configure the view 
    scnView.backgroundColor = UIColor.gray 
} 

이 업데이트 : 왜 반경 및/2

반경하지 여기
ballNode.position = SCNVector3Make(0, 6, 0) 

는 스크린 샷입니다

이 scr보기 Xcode의 장면 편집기에서 eenshot. 큐브의 원래 위치는 (0, 0, 0)이며 볼의 위치도 같습니다. 따라서 볼은 r/2가 아니라 r만큼 이동해야합니다. r/2의 경우 높이 r/2 인 볼의 하부가 여전히 정육면체 내부에있게됩니다. 아래 장면에서와 같이 큐브와 구를 편집기에 추가하면 명확히 할 수 있습니다.

Why r and not r/2

+0

그래, 아니에요!하지만 난이 전체 반경을 추가해야하는 이유 (1) 대신 0.5? 나는 앵커 생각 구의 점이 중간에 있습니까? –

+1

@TimurMustafaev 왜 r과 r/2가 아닌지에 대한 대답이 업데이트되었습니다. –

0

모든 것이 정상적으로 실행되고 원하는대로, 그것은 좋아 보인다. 큐브 노드에 볼 노드를 추가했습니다. 그래서 볼 노드의 원점은 입방체의 중심에 있습니다. 그런 다음 공의 위치를 ​​y 축의 큐브 크기의 절반으로 변경합니다. 그래서 기본적으로 튕겨 나오고 공의 절반 만 볼 수 있습니다 (반경은 1입니다). 그냥 큐브의 상단에을 넣어

그래서 당신이 다시 공 크기의 절반을 추가 할 수있다 : 당신은 cube-height/2 + sphere-radius하여 Y 축에 번역해야

ballNode.position = SCNVector3Make(0, 5.5, 0)

+0

아니 :(그것은 조금 더 보이지만, 그것은! 감사합니다 일 큐브 –