1

尝试在 SCNNode 中设置 boundingBox 检测时遇到问题。我希望能够调用 boundingBox 函数来检测 SCNNode 内的对象并在按钮点击时隐藏/显示这些对象,以下是我当前的状态:

 @IBAction func hideCubeButtonTapped(sender: UIButton) {
    guard let hatNode = hatNode?.presentation.worldPosition else { return }


    for cubeNode in cubeNodes {


        // hide/show cubeNodes in the hat
        if (hatNode.boundingBoxContains(point: cubeNode.presentation.worldPosition)) {
            print("Hide Button Tapped")
            if cubeNode.isHidden == true {
                cubeNode.isHidden = false
            } else {
                cubeNode.isHidden = true
            }
        }
    }
}

从这个结构和扩展调用 boundingBox 函数:

extension SCNNode {
func boundingBoxContains(point: SCNVector3, in node: SCNNode) -> Bool {
    let localPoint = self.convertPosition(point, from: node)
    return boundingBoxContains(point: localPoint)
}

func boundingBoxContains(point: SCNVector3) -> Bool {
    return BoundingBox(self.boundingBox).contains(point)
}

}

struct BoundingBox { 让最小值:SCNVector3 让最大值:SCNVector3

init(_ boundTuple: (min: SCNVector3, max: SCNVector3)) {
    min = boundTuple.min
    max = boundTuple.max
}

func contains(_ point: SCNVector3) -> Bool {
    let contains =
        min.x <= point.x &&
            min.y <= point.y &&
            min.z <= point.z &&

            max.x > point.x &&
            max.y > point.y &&
            max.z > point.z

    return contains
}

}

调用 hatNode.boundingBoxContains 时显示此错误:“'SCNVector3' 类型的值没有成员 'boundingBoxContains'”

hatNode 没有被设置为 SCNVector3?我在这里想念什么?我是 swift 新手,所以请在这里纠正我!

这段代码改编自这个问题: How to detect if a specific SCNNode is located inside another SCNNode's boundingBox - SceneKit - iOS

4

2 回答 2

2

worldPosition and its siblings, and their simd cousins are newly introduced to SCNNode in iOS11, even it is poorly (or not) documented, you can get some clues in the term "world" itself, and comments in source as below.

/*!
 @abstract Determines the receiver's position in world space (relative to the scene's root node).
 */
@available(iOS 11.0, *)
open var simdWorldPosition: simd_float3

To your problem, there are 2 solutions, but you have to choose one style you preferred. From your code, you were mixing using 2 different coordinate space.

  1. If you choose to use "world" space, you should convert both hat and cube into "world" space. Obviously, your cubes are, but hat not.

  2. If you do not want to bother with world, rather believed in relativity, you can simply convert cube into hat's coordinate/space, using convertPosition or simdConvertPosition at your favorite , before boundingBoxContains

于 2018-01-15T22:29:03.567 回答
0

在第二行代码中,您将 worldPosition(一个 SCNVector3)分配给 hatNode 变量。所以当你在 hatNode 上调用 boundingBoxContains 时,hatNode 是一个 SCNVector3。如果代码从第二行中删除 .worldPosition,以便将表示节点分配给 hatNode。

于 2017-12-06T05:11:52.090 回答