Having an issue getting a function to work when trying to setup boundingBox detection in a SCNNode. I'm expecting to be able to call the boundingBox function to detect objects inside a SCNNode and hide/show those objects on a button tap, here is my current state below:
@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
}
}
}
}
which is calling the boundingBox function from this struct and extension:
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
}
}
when calling the hatNode.boundingBoxContains this error is showing: "Value of type 'SCNVector3' has no member 'boundingBoxContains'"
the hatNode is not getting set as a SCNVector3? what am i missing here? i'm new to swift so please correct me here!
this code was adapted from this question: How to detect if a specific SCNNode is located inside another SCNNode's boundingBox - SceneKit - iOS