我需要检查现有的生成的SCNNode是否包含一个点 - 如果我存储在数组中的任何节点都包含这一点,我就不能在这里生成。问题是即使我不断在相同的位置产生(我确保这一点),以下内容返回false:
func checkIfContainsPoint(ptPos: SCNVector3)->Bool
{
return self.node.boundingBoxContains(point:ptPos))
}
这是我获得这些扩展的地方:
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 {
let min: SCNVector3
let max: 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
}
}
我唯一可以做到这一点的方法是测试:
if (ptPos.x == self.node.position.x && ptPos.y == self.node.position.y && ptPos.z == self.node.position.z
但我需要查看边界框是否包含该点,而不是位置是否完全相等。这有什么不对?