title: How to get the distance between two SKSpriteNode objects for use in Potential Fields algorithm?
body: <p>First off as a disclaimer, I am perfectly aware that the SpriteKit Physics bodies are capable of collecting collisions. This is not what I'm trying to do. I'm attempting to build a potential field AI and to do that part of the calculations for the attraction and repel vectors require the distance between two objects. </p>
  
  <p>Finding the distance I thought would be simply using the distance formula, but for some reason I'm getting very wonky results for my distances, so I'd like to consult if someone can catch what I'm doing wrong. </p>
  
  <p>Below I use a simple distance formula with the points positions aligned to their centers. </p>
  
  <pre><code>func getDistanceBetweenTwoObjects(sourceNode:SKSpriteNode, destinationNode:SKSpriteNode) -&gt; CGFloat{
      var centerSourceNode = CGPoint(x: sourceNode.position.x, y: sourceNode.position.y)
      centerSourceNode.x += sourceNode.size.width/2
      centerSourceNode.y += sourceNode.size.width/2
  
      var centerDestNode = CGPoint(x: destinationNode.position.x, y: destinationNode.position.y)
      centerDestNode.x += destinationNode.size.width/2
      centerDestNode.y += destinationNode.size.width/2
      var distance = sqrt(pow((centerSourceNode.x - centerDestNode.x), 2) + pow((centerSourceNode.y - centerDestNode.y), 2))
      return distance
  }
  </code></pre>
  
  <p>Here is the function I use to create an attraction vector.</p>
  
  <pre><code>func getAttractionVector(node:SKSpriteNode, destinationNode:SKSpriteNode, spread:CGFloat, strength:CGFloat) -&gt; (xSpeed: CGFloat, ySpeed: CGFloat){
      var sourceNodeRadius = sqrt(pow(node.size.width/2,2))
      var destNodeRadius = sqrt(pow(destinationNode.size.width/2,2))
      var radius = sourceNodeRadius + destNodeRadius
      var distance = getDistanceBetweenTwoObjects(node, destinationNode: destinationNode)
      if distance &lt; radius{
          return (0,0)
      }
      var angle = atan((destinationNode.position.y - node.position.y)/(destinationNode.position.x - node.position.x))
      var xSpeed:CGFloat = 0
      var ySpeed:CGFloat = 0
  
      if radius &lt;= distance &amp;&amp; distance &lt;= spread + radius{
          xSpeed = strength * (distance - radius) * cos(angle)
          ySpeed = strength * (distance - radius) * sin(angle)
      }else if distance &gt; spread + radius{
          xSpeed = strength * spread * cos(angle)
          ySpeed = strength * spread * sin(angle)
      }
      return (xSpeed, ySpeed)
  }
  </code></pre>
  
  <p>I use a similar math to do the repel vector:</p>
  
  <pre><code>    func getRepelVector(node:SKSpriteNode, destinationNode:SKSpriteNode, spread:CGFloat, strength:CGFloat) -&gt; (xSpeed: CGFloat, ySpeed: CGFloat){
  
      var sourceNodeRadius = sqrt(pow(node.size.width/2,2))
      var destNodeRadius = sqrt(pow(destinationNode.size.width/2,2))
      var radius = sourceNodeRadius + destNodeRadius
      var distance = getDistanceBetweenTwoObjects(node, destinationNode: destinationNode)
      if distance &gt; spread + radius{
          return (0,0)
      }
  
      var angle = atan((destinationNode.position.y - node.position.y)/(destinationNode.position.x - node.position.x))
      var xSpeed:CGFloat = 0
      var ySpeed:CGFloat = 0
      println("\(distance) : \(radius)")
      if radius &lt;= distance &amp;&amp; distance &lt;= spread + radius{
          xSpeed = strength * (spread + radius - distance) * cos(angle)
          ySpeed = strength * (spread + radius - distance) * sin(angle)
      }else if distance &lt; radius{
          xSpeed = getSign(cos(angle)) * CGFloat.max
          ySpeed = getSign(sin(angle)) * CGFloat.max
          println("HERE")
      }
      return (xSpeed, ySpeed)
  }
  </code></pre>
  
  <p>Unfortunately, the distance formula is clearly failing me, I've caught the node at multiple times either touching and it not picking it up, or not touching, and thinking its touching. Can anyone see where I'm doing this wrong or suggest a simpler way to accurately calculating distance between two SpriteNodes? Hope its something simple. Thanks.</p>
  
  <p>The following image shows my AINode solving my maze (orange), an obstacle to avoid (blue) and my goal (green).</p>
  
  <p>The AINode has a width of 20 in this example.
  The obstacle has a width of 50 in this example.</p>
  
  <p>With the debugger paused in this invalid state where the AINode has illegally intersected the obstacle, the distance between the center points using the formulas supplied above is reading "28.28". The real distance is 7. 
  <a href="https://i.stack.imgur.com/3AljM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3AljM.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've caught a major error in my attraction and repel vectors that was augmenting the problem. My distance formula was a little off, but it was magnified due to an error in how I calculate the angle. The angle needs to be calculated with <code>atan2</code> not <code>atan</code>. The reason being is that the angle will be returned in the incorrect quadrant if <code>atan2</code> is not used, this distorted the direction of the repel vectors and caused major errors. </p>