简体   繁体   中英

SceneKit intersection of nodes

I have a problem with calculating intersection of 2 nodes in SceneKit. Objects are like sphere and thicker plane (more complex polygons). Setup: Simple Scene with 2 irregular objects that can move. I've tried few things like:

  • intersection of physics bodies (shows something with ShapeType.convexNull but has connection points even if objects are not in contact. ShapeType.concavePolyhedron does not show anything.
  • hit testing: I had an idea to somehow calculate random vectors (a lot) that would go through both objects and checking common hit points. It does not work - shows invalid points. That Might be connected with fact that objects are moved around and scaled, but no luck here as well.

Sample code i use:

let root = SCNScene()
        let n1 = SCNNode()
        let n2 = SCNNode()
        
        root.rootNode.addChildNode(n1)
        root.rootNode.addChildNode(n2)
        
        // try1
        let options = [SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.convexHull]
        n1.physicsBody = SCNPhysicsBody(type: .kinematic, shape: SCNPhysicsShape(node: n1, options: options))
        n2.physicsBody = SCNPhysicsBody(type: .kinematic, shape: SCNPhysicsShape(node: n2, options: options))
        n1.physicsBody?.isAffectedByGravity = false
        n2.physicsBody?.isAffectedByGravity = false
        
        let opt: [SCNPhysicsWorld.TestOption : Any] = [.searchMode: SCNHitTestSearchMode.all]
        let inter = root.physicsWorld.contactTestBetween(n1.physicsBody!, n2.physicsBody!, options: opt)
        
        //does not work as expected
     
        
        // trial 2
        
        let v1 = SCNVector3(0, 0, 0)
        let v2 = SCNVector3(0, 0, 50)

        let n1v1 = root.rootNode.convertVector(v1, to: n1)
        let n1v2 = root.rootNode.convertVector(v2, to: n1)
        let n2v1 = root.rootNode.convertVector(v1, to: n2)
        let n2v2 = root.rootNode.convertVector(v2, to: n2)
        
        let n1Hit = n1.hitTestWithSegment(
            from: n1v1,
            to: n1v2,
            options: nil)
        let n2Hit = n2.hitTestWithSegment(
            from: n2v1,
            to: n2v2,
            options: nil)

Any ideas how to find intersection line/plane/points?

I'll leave it for next generations:) I found a nice framework that helps calculating intersection of meshes: https://github.com/nicklockwood/Euclid

It fits my needs so the question is closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM