【问题标题】:SceneKit Child Node RotationSceneKit 子节点旋转
【发布时间】:2018-12-17 17:13:15
【问题描述】:

当您在添加球后旋转并单击船添加另一个球时,击球测试坐标返回不正确并渲染球完全脱离触摸?

class Ship:SCNNode{
    var currentAlignment: ARPlaneAnchor.Alignment = .horizontal
    var rotationWhenAlignedHorizontally: Float = 0
    var objectRotation: Float {
        get {
            return childNodes.first!.eulerAngles.y
        }
        set (newValue) {
            var normalized = newValue.truncatingRemainder(dividingBy: 2 * .pi)
            normalized = (normalized + 2 * .pi).truncatingRemainder(dividingBy: 2 * .pi)
            if normalized > .pi {
                normalized -= 2 * .pi
            }
            childNodes.first!.eulerAngles.y = normalized
            if currentAlignment == .horizontal {
                rotationWhenAlignedHorizontally = normalized
            }
        }
    }

    override init() {
        super.init()
        guard let virtualObject = SCNScene(named: "art.scnassets/ship.scn") else {return}
        let wrapperNode = SCNNode()
        for child in virtualObject.rootNode.childNodes {
            child.geometry?.firstMaterial?.lightingModel = .phong
            wrapperNode.addChildNode(child)
        }
        self.addChildNode(wrapperNode)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

class Ball:SCNNode{
    override init() {
        super.init()
        let sphere = SCNSphere(radius: 0.025)
        self.geometry = sphere
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!
    var nodeOne:Ship = Ship()

    override func viewDidLoad() {
        super.viewDidLoad()
        sceneView.delegate = self
        sceneView.showsStatistics = true
        sceneView.autoenablesDefaultLighting = true

        nodeOne.position = SCNVector3(x:0,y:0,z:0)
        self.sceneView.scene.rootNode.addChildNode(nodeOne)

        let tap = UITapGestureRecognizer(target: self, action: #selector(tap(gesture: )))
        self.sceneView.addGestureRecognizer(tap)

        let rotate = UIRotationGestureRecognizer(target: self, action: #selector(rotate(gesture:)))
        self.sceneView.addGestureRecognizer(rotate)
    }

    @objc func tap(gesture:UITapGestureRecognizer){
        let tapLocation = gesture.location(in: self.sceneView)
        let hitTestNode = sceneView.hitTest(tapLocation, options: nil)
        let nodeTwo = Ball()
        nodeTwo.position = (hitTestNode.first?.worldCoordinates)!

        nodeOne.childNodes.first.addChildNode(nodeTwo)
    }

    @objc func rotate(gesture:UIRotationGestureRecognizer){
        guard gesture.state == .changed else { return }
        nodeOne.objectRotation -= Float(gesture.rotation)
        gesture.rotation = 0
    }
}

这是一个有问题的视频:https://drh2acu5z204m.cloudfront.net/items/291B1C1U0S3f192t3B32/ScreenRecording_07-10-2018%2011-19-47.mp4?X-CloudApp-Visitor-Id=3097286

【问题讨论】:

    标签: ios swift scenekit arkit


    【解决方案1】:

    我能够解决它。它与将本地点击坐标转换为世界有关

    nodeTwo.position = (hitTestNode.first?.node.convertPosition((hitTestNode.first?.localCoordinates)!, to: nodeOne))!
    

    【讨论】:

      猜你喜欢
      • 2015-07-10
      • 2018-12-24
      • 2017-04-25
      • 2018-07-05
      • 1970-01-01
      • 2016-07-22
      • 2018-03-25
      • 2018-11-01
      • 2015-03-05
      相关资源
      最近更新 更多