简体   繁体   English

SceneKit 节点的交集

[英]SceneKit intersection of nodes

I have a problem with calculating intersection of 2 nodes in SceneKit.我在 SceneKit 中计算 2 个节点的交集时遇到问题。 Objects are like sphere and thicker plane (more complex polygons).对象就像球体和更厚的平面(更复杂的多边形)。 Setup: Simple Scene with 2 irregular objects that can move.设置:带有 2 个可以移动的不规则物体的简单场景。 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.物理实体的交集(使用ShapeType.convexNull显示一些东西,但即使对象不接触也有连接点ShapeType.concavePolyhedron不显示任何东西。
  • hit testing: I had an idea to somehow calculate random vectors (a lot) that would go through both objects and checking common hit points.命中测试:我有一个想法以某种方式计算随机向量(很多),这些向量将通过两个对象 go 并检查共同的命中点。 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我会把它留给下一代:) 我找到了一个很好的框架来帮助计算网格的交集: https://github.com/nicklockwood/Euclid

It fits my needs so the question is closed.它符合我的需求,因此问题已结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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