-[SCNView hitTest:options:] doesn’t work correctly for orthographic projections

How frustrating it was to find this out.  Luckily, it didn’t take long to realise that there was a pattern to its madness.  It turns out it’s not taking into account the scale of the projection (as set by the camera node’s scale property).  So you can fudge the location appropriately to work around the bug, using code like:

- (void)mouseDown:(NSEvent*)theEvent {
    const NSPoint locationInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    const NSSize boundsSize = self.bounds.size;
    const NSPoint fudgedLocationInView = NSMakePoint((boundsSize.width / 2.0) + ((locationInView.x - (boundsSize.width / 2.0)) * self.pointOfView.scale.x),
                                                     (boundsSize.height / 2.0) + ((locationInView.y - (boundsSize.height / 2.0)) * self.pointOfView.scale.y));
    NSLog(@"Real location = {%f, %f}.  Bounds size = {%f, %f} and scale is %f, therefore translating perceived location to {%f, %f} in order to work around SceneKit bug (rdar://12890554)",
          locationInView.x, locationInView.y,
          boundsSize.width, boundsSize.height,
          fudgedLocationInView.x, fudgedLocationInView.y);

    NSArray *clickedObjects = [self hitTest:fudgedLocationInView options:nil];

One thought on “-[SCNView hitTest:options:] doesn’t work correctly for orthographic projections”

Leave a Reply