2017-10-16 19 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

いや:(それは少し高いように見えますが、それは!感謝を働いたキューブ –

関連する問題