Getting an SCNNode's bounding volume with rotation

青春壹個敷衍的年華 提交于 2021-02-07 08:58:35

问题


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:

  1. In Interface Builder, as shown on your screenshot, there's an Editing space property that you can set to World (instead of Local)

  2. 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.

  1. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!