Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift SceneKit-在2个SCNNodes之间放置SCNCylinder_Swift_Scenekit - Fatal编程技术网

Swift SceneKit-在2个SCNNodes之间放置SCNCylinder

Swift SceneKit-在2个SCNNodes之间放置SCNCylinder,swift,scenekit,Swift,Scenekit,我有两个SCNSphere,我想用一个SCNCylinder(类似于这个OO的东西)来“链接”它们,但我无法实现它。我尝试使用SCNNode pivot属性应用旋转,lookAt(:)但它们都不允许我实现我想要的 作为约束,出于架构的原因,我希望圆柱体是第一个球体的子对象 假设我们有一个简单的例子,第一个和第二个球体的半径为0.1,没有旋转,只有位置相关: static func createSphere(position: SCNVector3, color: UIColor = .red)

我有两个SCNSphere,我想用一个SCNCylinder(类似于这个OO的东西)来“链接”它们,但我无法实现它。我尝试使用SCNNode pivot属性应用旋转,lookAt(:)但它们都不允许我实现我想要的

作为约束,出于架构的原因,我希望圆柱体是第一个球体的子对象

假设我们有一个简单的例子,第一个和第二个球体的半径为0.1,没有旋转,只有位置相关:

static func createSphere(position: SCNVector3, color: UIColor = .red) -> SCNNode {
    let shape = SCNSphere(radius: 0.1)
    shape.firstMaterial?.diffuse.contents = color
    let node = SCNNode(geometry: shape)
    node.position = position
    return node
}
并创建“链接节点”:

都是这样使用的:

 /// Create a sphere and the related link if there is a next sphere
 static func createPOI(position: SCNVector3, nextPosition: SCNVector3? = nil) -> SCNNode {
    var node: SCNNode = createSphere(position: position, color: .black)

    if let nextPosition = nextPosition {
        let lineNode = createLine(position: SCNVector3.zero, targetPosition: nextPosition)
        node.addChildNode(lineNode)
    }

    return node
}


static func createLine(position: SCNVector3, targetPosition: SCNVector3) -> SCNNode {
    let node = createCylinder(position: position)

    //step 1: handle rotation
    node.position = position
    //node.look(at: targetPosition)
    node.eulerAngles.x = Float.pi/2
    node.eulerAngles.y = atan2f(targetPosition.y, targetPosition.magnitude)

    //step 2: handle scaling and positioning
    node.pivot = SCNMatrix4MakeTranslation(-0.5, 0, 0)
    let width = CGFloat((targetPosition - position).magnitude)
    (node.geometry as? SCNCylinder)?.height = width

    //TODO: Cylinder should link source and target nodes
    //Tried a lot of things above but didn't manage to do it

    return node
}

您可能会在这里得到答案:
 /// Create a sphere and the related link if there is a next sphere
 static func createPOI(position: SCNVector3, nextPosition: SCNVector3? = nil) -> SCNNode {
    var node: SCNNode = createSphere(position: position, color: .black)

    if let nextPosition = nextPosition {
        let lineNode = createLine(position: SCNVector3.zero, targetPosition: nextPosition)
        node.addChildNode(lineNode)
    }

    return node
}


static func createLine(position: SCNVector3, targetPosition: SCNVector3) -> SCNNode {
    let node = createCylinder(position: position)

    //step 1: handle rotation
    node.position = position
    //node.look(at: targetPosition)
    node.eulerAngles.x = Float.pi/2
    node.eulerAngles.y = atan2f(targetPosition.y, targetPosition.magnitude)

    //step 2: handle scaling and positioning
    node.pivot = SCNMatrix4MakeTranslation(-0.5, 0, 0)
    let width = CGFloat((targetPosition - position).magnitude)
    (node.geometry as? SCNCylinder)?.height = width

    //TODO: Cylinder should link source and target nodes
    //Tried a lot of things above but didn't manage to do it

    return node
}