问题
I have a very simple scene set up using the XCode level editor.
As you can see, one of the 1x5 rectangular cuboids has been rotated by 90º. This was achieved by setting the y attribute of the Euler Angles property to 90 in the Node Inspector.
But notice that the Bounding Box doesn't account for the cuboids rotation. This is also the case when I iterate through the SCNNodes in the scene. Both cuboids have the same Bounding Volumne
for wall:SCNNode in walls.childNodes {
var v1 = SCNVector3Zero
var v2 = SCNVector3Zero
wall.getBoundingBoxMin(&v1, max:&v2)
print(v1, v2)
}
// Prints
// SCNVector3(x: -0.5, y: 0.0, z: -2.5) SCNVector3(x: 0.5, y: 0.5, z: 2.5)
// SCNVector3(x: -0.5, y: 0.0, z: -2.5) SCNVector3(x: 0.5, y: 0.5, z: 2.5)
So it seems the Bounding Volume is calculated using the child node's own coordinate system, not node parent's (or scene's) coordinate system. Which leaves me wondering; How do I calculate the SCNNodes bounding volume in the scene?
Looking through the documentation for SCNNode, it seems all the information is there. I can read the eulerAngles
which gives me the correct rotation around the y axis. I can read the transform
property.
I thought I might be able to multiply the vectors by the nodes transform matrix, but the transform matrix (4x4) and bounding box vectors (1x3) don't appear to be compatible. Nor does SceneKit appear to provide any methods for applying a transformation matrix to a vector. However, I'm not great with Geometry so I could be missing something obvious.
回答1:
A few ideas:
In Interface Builder, as shown on your screenshot, there's an Editing space property that you can set to World (instead of Local)
In code, you can use a temporary node to compute the bounding box. Make sure that only has one child node (the node whose bounding box you want to compute), apply any transform on that child node, and they ask the parent node for its bounding box. From the documentation:
The bounding volume of a node containing child nodes is the minimal volume that encloses the bounding volumes of the node’s children.
- Get the node's bounding box and then use utils such as
-convertPosition:toNode:
to compute the bounding box in another's coordinate system
来源:https://stackoverflow.com/questions/34831701/getting-an-scnnodes-bounding-volume-with-rotation