将SCNNodes放在彼此旁边

时间:2018-01-22 06:25:40

标签: ios scenekit

假设我有三个平面几何图形,每个几何图形都显示一个白色平面(通过diffuse.contents设置)。如何将第二个和第三个SCNNode放在一起?

我的飞机有以下尺寸: (w,h) (100,100) (200,100) (50,100)

我把第一个放在(0,0,-100)。现在,我希望第二个放在第一个的右边,第三个放在第二个的右边。

是唯一可以跟踪X坐标并根据之前的SCNNode宽度继续增加它的选项吗?是否有可能以更好的方式做到这一点?

1 个答案:

答案 0 :(得分:0)

这使用您提供的硬编码数字,但您可以随时将其更改为动态或填写函数以获得运行时的大小以使其更友好。取决于你需要什么。

nodeA.position = SCNVector3(0,0,-100)

// add half the width of nodeA to get the edge
let nodeAEdge : SCNVector3 = nodeA.position + SCNVector3(50,0,0)

// place center of nodeB half nodeB's width from the edge of nodeA
nodeB.position = nodeAEdge + SCNVector3(100,0,0)

// add half the width of nodeB to get the edge
let nodeBEdge : SCNVector3 = nodeB.position + SCNVector3(100,0,0)

// place center of nodeC half nodeC's width from the edge of nodeB
nodeC.position = nodeBEdge + SCNVector3(25,0,0)

// add all nodes to scene 

// Use this to overload the '+' to add two SCNVector3() (place outside the class)
func + (left: SCNVector3, right: SCNVector3) -> SCNVector3 {
    return SCNVector3Make(left.x + right.x, left.y + right.y, left.z + 
right.z)
}

另一种方法是获取每个节点的boundingBox 这是Apple Documentation

相关问题