0

I have a node that I'm extracting from an SCNScene. I've got some information about it, but I'm confused about one thing - how the bounding box is calculated. The node is positioned once it is loaded at vector 0,0,0 using:

[myNode setPosition: SCNVector3Make(0, 0, 0)];

However, the bounding box still reports a min.x of -1.

How can this be if I've just positioned it at 0? Additionally, it doesn't matter what vector values I give, it always has a min.x of -1 - despite the node actually moving around screen as expected.

Roo
  • 259
  • 1
  • 3
  • 15

1 Answers1

2

It's hard to answer definitively without more information about what's in your scene and what you're doing with it, but it's probably one or both of these issues:

The bounding box tells you the extent of the node's content, not its position — e.g. if you have a cube 2 units wide positioned at (0, 0, 0), its bounding box min and max corners will be (-1, -1, -1) and (1, 1, 1).

Depending on what tricks you're using to move your node around, the node itself may not be changing position — SCNActions, CAAnimations, and physics all work on a separate copy of the node hierarchy. (Partly this is to support implicit animation — e.g. set the position of a node, and it'll appear to animate from the current position to the position you set, but you can still read back position and have it be the value you set it to.)

You get to this separate hierarchy with the node's presentationNode property — ask the presentation node for its bounding box and you'll see its position/extent as affected by actions/animations/physics and currently rendered.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • to elaborate: if you have a cube 2 units wide its bounding box min and max corners will *always* be `(-1, -1, -1)` and `(1, 1, 1)`, *independently of the node's position*. The bounding box is always expressed in the receiver's coordinates system, and since the cube dimensions stay the same, the node's bounding box stays the same. You will have to use conversion utilities to express these coordinates in world space. – mnuages Feb 06 '15 at 19:32
  • Right. :) So, if you want the bounding box of your node in its parent's coordinate system, you have to [convert](https://developer.apple.com/library/prerelease/ios/documentation/SceneKit/Reference/SCNNode_Class/index.html#//apple_ref/occ/instm/SCNNode/convertPosition:fromNode:) those vectors. Or, if it's the only child of the parent node, ask the parent node (or parent's presentation node) for *its* bounding box. – rickster Feb 06 '15 at 19:34
  • Thanks guys. You mention a cube of 2 units would always start at (-1,-1,-1). Is that because it's centred on (0,0,0) when you first place it? Hence 1 unit either side of zero. – Roo Feb 06 '15 at 19:46
  • Yup. The parametric geometries (`SCNBox`, `SCNSphere`, etc.) are defined relative to the center of their local coordinate space. If you want to put them elsewhere, set the `position` or the `pivot` of the node containing them. – rickster Feb 06 '15 at 19:59