1

Why this 2 object pass through each other and not interact, what I'm doing wrong? on each object I applied the property physicsBody which should allow to the physic engine to work.

I add the square object using a tap gesture, creating the anchor with name "base" and in the render add the object base.

for the ball I use a raycast query to position the ball over the plane.

all working fine, only the dynamic looks wrong, they pass each other.

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

and this ball:

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

they should interact each other , but actually my ball pass through the base.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Damiano Miazzi
  • 1,514
  • 1
  • 16
  • 38

1 Answers1

0

If you're trying to simulate two objects' collision, first of all, you must implement the protocol called SCNPhysicsContactDelegate and its delegate:

weak var contactDelegate: SCNPhysicsContactDelegate? { get set }

for using its three optional instance methods:

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

And, of course, category bit mask and collision bit mask are required.

If you need a more detailed info, please look at THIS POST.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220