4

I'm trying to convert the 3d position of a nested SCNNode in to an x, y (CGPoint) coordinate (for overlaying a view over the object).

My SCNNode structure looks like:

      +-----------+
      | Root node |
      +-----+-----+
            |
            |
       +----+---+
    +--+ Node A +--+
    |  +--------+  |
    |              |
    |              |
    |              |
+---+----+    +----+---+
| Node B |    | Node C |
+--------+    +--------+

I'd like to get the screen space 2d position of NodeB. I'm aware of the convertPosition: and projectPoint: methods but I don't think I'm using them correctly.

Any pointers?

Edit:

Currently trying something like this:

SCNVector3 rootPosition = [[[[self view3D] scene] rootNode] convertPosition:[nodeB position] fromNode:nodeB];
SCNVector3 projectedPoint = [[self view3D] projectPoint:rootPosition];
NSLog(@"x %f y %f z %f", projectedPoint.x, projectedPoint.y, projectedPoint.z);

The resulting x, y, z are way off (and sometimes negative, which doesn't make sense in screen space as the object is plainly in view).

jxd
  • 43
  • 5

2 Answers2

2

NodeB's position is specified in terms of NodeA's coordinate system. So I believe you want

rootPosition = [rootNode convertPosition:nodeB.position fromNode:nodeA]
Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
2

On swift4:

nodeAchildPosition = scnScene.rootNode.convertPosition(nodeA.position, from: nodeAchild)
Serhii Didanov
  • 2,200
  • 1
  • 16
  • 31
  • Is this answer getting the position of nodeB, or nodeA? (It seems reversed from Hal Mueller's answer!) And is there some (any?) reason you can't just use nodeB.parent for the `from` parameter? – livingtech Sep 28 '20 at 21:00