title: SceneKit physics don't work properly in ARKit
body: <p>I'm new to <code>ARKit</code> and I'm trying to make a relatively simple app where you create a "wall" and the user "throws" balls at it, and it bounces back.</p>
  
  <p>I create the wall as a <code>SCNPlane</code> where the user is pointing the camera to like so:</p>
  
  <pre><code>private func createWall(withPosition position: SCNVector3) -&gt; SCNNode {
      let wallGeometry = SCNPlane(width: 0.3, height: 0.3)
      wallGeometry.firstMaterial?.isDoubleSided = true
      wallGeometry.firstMaterial?.diffuse.contents = UIColor.green.withAlphaComponent(0.7)
  
      let parentNode = SCNNode(geometry: wallGeometry)
  
      let (position, _) = cameraPosition()
      parentNode.position = position
  
      parentNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: wallGeometry, options: nil))
      parentNode.physicsBody?.isAffectedByGravity = false
  
      self.wall = parentNode
  
      return parentNode
  }
  </code></pre>
  
  <p>I get the direction and position of camera with this function:<br>
  <strong><code>cameraPosition()</code></strong>:</p>
  
  <pre><code>func cameraPosition() -&gt; (SCNVector3, SCNVector3) {
      guard let frame = sceneView.session.currentFrame else { return (SCNVector3(0, 0, -1), (SCNVector3(0, 0, 0))) }
      let matrix = SCNMatrix4(frame.camera.transform)
      let direction = SCNVector3(-matrix.m31, -matrix.m32, -matrix.m33)
      let location = SCNVector3(matrix.m41, matrix.m42, matrix.m43)
      return ((location + direction), direction)
  }
  
  // Helper function
  func +(left: SCNVector3, right: SCNVector3) -&gt; SCNVector3 {
      return SCNVector3(left.x + right.x, left.y + right.y, left.z + right.z)
  }
  </code></pre>
  
  <p>I create instances of <code>Ball()</code> and throw them like this:</p>
  
  <pre><code>let (position, direction) = cameraPosition()
  
  // throw ball
  let ball = Ball()
  ball.position = position //SCNVector3(0,0,0)
  sceneView.scene.rootNode.addChildNode(ball)
  
  let impulseModifier = Float(10)
  ball.physicsBody!.applyForce(SCNVector3(direction.x*impulseModifier, direction.y*impulseModifier, direction.z*impulseModifier), asImpulse: true)
  </code></pre>
  
  <p>The Ball class:  </p>
  
  <pre><code>class Ball: SCNNode {
      override init() {
          super.init()
  
          let sphere = SCNSphere(radius: 0.05)
          sphere.firstMaterial?.diffuse.contents = UIColor.red
          self.geometry = sphere
  
          self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(geometry: sphere, options: nil))        
      }
  }
  </code></pre>
  
  <p>The problem is that many times, instead of the ball actually hitting the wall, it will just travel through it, as if the physics body does not function properly. I noticed that sometimes it will work better when I change the distance and angle between the camera and the wall, but the results are never consistent as far as I've tried.<br>
  I have also tried to change the wall position to: <code>SCNVector3(0, 0, -1)</code> to position it exactly 1 meter deep away from the world origin, and the results were somewhat better but still not consistent.   </p>
  
  <p>Where could the problem lay and why?<br>
  Thanks in advance!    </p>