1

I need to check simply whether an existing spawned SCNNode contains a point - if any of my nodes that I store in an array contain this point, I can't spawn here. Problem is even though I continually spawn in the SAME POSITION (I make sure of this) the following returns false:

func checkIfContainsPoint(ptPos: SCNVector3)->Bool
    {

        return self.node.boundingBoxContains(point:ptPos))
    }

Here's where I got these extensions:

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
    }
}

Only way I can get it to be true is to test for this:

if (ptPos.x == self.node.position.x && ptPos.y == self.node.position.y && ptPos.z == self.node.position.z

But I need to see if the bounding box contains the point, not if position is equal EXACTLY. What's wrong here?

jscs
  • 63,694
  • 13
  • 151
  • 195
blue
  • 7,175
  • 16
  • 81
  • 179

0 Answers0