Get dae node size

Suppose I have a collada file that has a box in it, and I import the dae file into the scene. Now, after importing to the scene, I know that the dae object is a field. How can I get window sizes in a script after adding it to the scene

If I import node as SCNBox, I get errors saying that SNCBox is not a subtype of SCNNode.

floorNode = scene.rootNode.childNodeWithName("floor", recursively: false) as SCNBox floorNode?.physicsBody = SCNPhysicsBody.staticBody() floorNode?.physicsBody?.categoryBitMask = 2 let floorGeo: SCNBox = floorNode.geometry! as SCNBox 

How to get dimensions if SCNNode is the only way to import nodes?

+7
scenekit
source share
2 answers

SCNBox is just a helper subclass of SCNGeometry for creating window geometry. When you import Collada into a scene, you get a SCNNodes scene graph with SCNGeometries, not SCNBox'es or SCNCones, etc. It doesnโ€™t matter what they look like. If you want to get the dimensions of any node, you must use the SCNBoundingVolume protocol, which is implemented by the SCNNode and SCNGeometry classes:

func getBoundingBoxMin (_ min: UnsafeMutablePointer, max max: UnsafeMutablePointer) -> Bool

Using this method, you will get boundary frames. For rectangular geometry, the dimensions will correspond to the bounding box.

Example:

 var v1 = SCNVector3(x:0, y:0, z:0) var v2 = SCNVector3(x:0, y:0, z:0) node.getBoundingBoxMin(&v1, max:&v2) 

Where node is node, you want to get a bounding box. Results will be in versions v1 and v2.

Swift 3

Using Swift 3, you can simply use node.boundingBox.min and node.boundingBox.max respectively.

+9
source share

quick code example on how to use getBoundingBoxMin:

  var min = SCNVector3Zero var max = SCNVector3Zero let b = shipNode.getBoundingBoxMin(&min , max: &max) let w = CGFloat(max.x - min.x) let h = CGFloat(max.y - min.y) let l = CGFloat( max.z - min.z) let boxShape = SCNBox (width: w , height: h , length: l, chamferRadius: 0.0) let shape = SCNPhysicsShape(geometry: boxShape, options: nil) shipNode.physicsBody!.physicsShape = SCNPhysicsShape(geometry: boxShape, options: nil) shipNode.physicsBody = SCNPhysicsBody.dynamicBody() 
+5
source share

All Articles