title: Scene Kit - Dragging an Object
body: <p>I’m trying to drag a chess piece with the mouse/trackpad using Scene Kit. All objects (board and pieces) are children of the root node, loaded from a Collada file.</p>
  
  <p>I found a helpful description of the process <a href="https://stackoverflow.com/questions/22925879/how-do-i-find-my-mouse-point-in-a-scene-using-scenekit/22944797#22944797">elsewhere on Stack Overflow</a>.  Using that description I wrote the initial version of the code below.  My problem is the disparity between the click coordinates and the piece node position — their coordinates are different orders of magnitude. I remain unclear on how to match them up — put them in the “same universe”. I’ve tried a number of suggestions from the Apple forums along with flights of fancy of my own.</p>
  
  <p>Here’s my current attempt, mostly reverted back to the original version based on the link above, along with logged coordinate values along the way.  The result is that dragging a chess piece causes it to abruptly jump off screen:</p>
  
  <pre><code>- (NSPoint)
  viewPointForEvent: (NSEvent *) event_
  {
      NSPoint   windowPoint    = [event_ locationInWindow];
      NSPoint   viewPoint        = [self.view convertPoint: windowPoint
                                               fromView: nil];
      return viewPoint;
  }
  
  - (SCNHitTestResult *)
  hitTestResultForEvent: (NSEvent *) event_
  {
      NSPoint      viewPoint        = [self viewPointForEvent: event_];
      CGPoint      cgPoint        = CGPointMake (viewPoint.x, viewPoint.y);
      NSArray * points        = [(SCNView *) self.view hitTest: cgPoint
                                                       options: @{}];
      return points.firstObject;
  }
  
  - (void)
  mouseDown: (NSEvent *) theEvent
  {
      SCNHitTestResult * result = [self hitTestResultForEvent: theEvent];
  
      SCNVector3 clickWorldCoordinates = result.worldCoordinates;
        log output: clickWorldCoordinates x 208.124578, y -12827.223365, z 3163.659073
      SCNVector3 screenCoordinates = [(SCNView *) self.view projectPoint: clickWorldCoordinates];
      log output: screenCoordinates x 245.128906, y 149.335938, z 0.985565
      // save the z coordinate for use in mouseDragged
      mouseDownClickOnObjectZCoordinate = screenCoordinates.z;
  
      selectedPiece = result.node;  // save selected piece for use in mouseDragged
  
      SCNVector3    piecePosition = selectedPiece.position;
        log output: piecePosition x -18.200000, y 6.483060, z 2.350000
  
      offsetOfMouseClickFromPiece.x = clickWorldCoordinates.x - piecePosition.x;
      offsetOfMouseClickFromPiece.y = clickWorldCoordinates.y - piecePosition.y;
      offsetOfMouseClickFromPiece.z = clickWorldCoordinates.z - piecePosition.z;
      log output: offsetOfMouseClickFromPiece x 226.324578, y -12833.706425, z 3161.309073  
  }
  
  - (void)
  mouseDragged: (NSEvent *) theEvent;
  {
      NSPoint   viewClickPoint        = [self viewPointForEvent: theEvent];
  
      SCNVector3 clickCoordinates;
      clickCoordinates.x = viewClickPoint.x;
      clickCoordinates.y = viewClickPoint.y;
      clickCoordinates.z = mouseDownClickOnObjectZCoordinate;
        log output:  clickCoordinates x 246.128906, y 0.000000, z 0.985565
  
        log output:  pieceWorldTransform = 
        m11 = 242.15889219510001, m12 = -0.000045609300002524833, m13 = -0.00000721691076126, m14 = 0, 
        m21 = 0.0000072168760805499971, m22 = -0.000039452697396149999, m23 = 242.15890446329999, m24 = 0, 
        m31 = -0.000045609300002524833, m32 = -242.15889219510001, m33 = -0.000039452676995750002, m34 = 0, 
        m41 = -4268.2349924762348, m42 = -12724.050221935429, m43 = 4852.6652710104272, m44 = 1)
  
      SCNVector3 newPiecePosition;
      newPiecePosition.x = offsetOfMouseClickFromPiece.x + clickCoordinates.x;
      newPiecePosition.y = offsetOfMouseClickFromPiece.y + clickCoordinates.y;
      newPiecePosition.z = offsetOfMouseClickFromPiece.z + clickCoordinates.z;
        log output: newPiecePosition x 472.453484, y -12833.706425, z 3162.294639
  
      selectedPiece.position = newPiecePosition;
  }
  </code></pre>
  
  <p>Up to this point, I’ve gotten a lot of interesting and useful comments and advice.  But I’ve realized that to move forward, I’m probably  going to need a working code sample which shows the secret sauce which allows clicks and vectors to exist in the “same universe”.</p>