SceneKit - adding SCNNode on another SCNNode

送分小仙女□ 提交于 2021-02-08 07:20:02

问题


I'm trying to put sphere on top of the box, but the position is strange: Ball is half hidden inside box. I tried to change box and sphere pivot, but it didn't help. Here's the code:

    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)`

and the result:

what I'm doing wrong? How to put the ball right on the top side of the box?

UPDATE: If I add Cube instead of ball, it looks good as I want


回答1:


You need to translate on the Y-axis by cube-height/2 + sphere-radius. Thus you should have:

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

Here is the screenshot:

The relevant complete code:

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
}

Update : Why radius and not radius / 2

See this screenshot from the Scene Editor in Xcode. The original position of cube is (0, 0, 0) and so is that of the ball; hence the ball needs to be moved by r and not r / 2; with r / 2 the lower section of the ball with height r/2 will be still inside the cube. You can just add a cube and sphere as in the scene below in the editor and that should help clarify.




回答2:


Everything is running normally. You added the ball node to the cube node. So the origin of the ball node is on the center of the cube. Then you change the position of the ball to half the size of the cube on the y axis. So basically it pops off and you see just the half of the ball (its radius is 1).

So you have to add again half the size of the ball to put it just on the top of the cube :

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



来源:https://stackoverflow.com/questions/46777901/scenekit-adding-scnnode-on-another-scnnode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!