【问题标题】:ARKit physics – Why these two objects don't interact with each other?ARKit 物理 - 为什么这两个对象不相互交互?
【发布时间】:2021-05-11 18:53:30
【问题描述】:

为什么这两个对象相互通过而不交互,我做错了什么? 在每个对象上,我应用了应允许物理引擎工作的属性physicsBody。

我使用点击手势添加方形对象,创建名称为“base”的锚点,并在渲染中添加对象 base。

对于球,我使用光线投射查询将球定位在平面上。

一切正常,只有动态看起来不对,它们相互传递。

// my square base

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        
    if let nome = anchor.name, nome == "base" {

        let geometry = SCNPlane(width: 1, height: 1)
        let material = SCNMaterial()
        material.diffuse.contents = UIColor(red: 90/255, green: 200/255, blue: 250/255, alpha: 0.50)
        geometry.materials = [material]
        // physics
        let physSchape  = SCNPhysicsShape(geometry: geometry, options: nil)
        let planePhysic = SCNPhysicsBody(type: .static, shape: physSchape)
        planePhysic.restitution = 0.0
        planePhysic.friction = 1.0
                
        let nodo = SCNNode(geometry: geometry)
        nodo.eulerAngles.x = -.pi / 2
        nodo.physicsBody = planePhysic
        node.addChildNode(nodo)
    }
}

还有这个球:

func addBall(recognizer: UITapGestureRecognizer){
            
    let tapLocation = recognizer.location(in: view)
    guard let query = view.raycastQuery(from: tapLocation, allowing: .existingPlaneGeometry, alignment: .horizontal) else {return}
    // ottengo posizione real world
    guard let translation = view.castRay(for: query).first?.worldTransform.translation else {return}
          
    let x = translation.x
    let y = translation.y
    let z = translation.z
            
    let ballGeometry = SCNSphere(radius: 0.1)
    let mat = SCNMaterial()
    mat.diffuse.contents = UIImage(named: "ball")
    ballGeometry.materials = [mat]
    //Physics
    let physSchape  = SCNPhysicsShape(geometry: ballGeometry, options: nil)
    let ballPhysic = SCNPhysicsBody(type: .dynamic, shape: physSchape)
    ballPhysic.mass = 0.2
    ballPhysic.friction = 0.8
            
    let nodeBall = SCNNode(geometry: ballGeometry)
    nodeBall.position = SCNVector3(x,y+0.3,z)
    nodeBall.physicsBody = ballPhysic
    view.scene.rootNode.addChildNode(nodeBall)
}

他们应该互相互动,但实际上我的球穿过了基地。

【问题讨论】:

    标签: swift xcode scenekit game-physics arkit


    【解决方案1】:

    如果你想模拟两个对象的碰撞,首先你必须实现名为SCNPhysicsContactDelegate的协议及其委托:

    weak var contactDelegate: SCNPhysicsContactDelegate? { get set }
    

    使用它的三个可选实例方法:

    func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) 
    func physicsWorld(_ world: SCNPhysicsWorld, didUpdate contact: SCNPhysicsContact)
    func physicsWorld(_ world: SCNPhysicsWorld, didEnd contact: SCNPhysicsContact)
    

    当然,category bit maskcollision bit mask 是必需的。

    如果您需要更详细的信息,请查看THIS POST

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多