3
votes

I am trying to get the size of a node on screen. This is the code:

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

carNode.scale = SCNVector3(0.9, 1.2, 0.5)
//carNode.transform = SCNMatrix4MakeScale(0.9, 1.2, 0.5)

carNode.getBoundingBoxMin(&v1, max: &v2)
print(v2)

However the values returned by getBoundingBoxMin before and after are identical, as if the scaling had not been taken in consideration. Why is that? I need this information to set the correct size for the physics body on the node for collision detection and the determine if other nodes are within some range from the object.

1

1 Answers

8
votes

Per SCNBoundingVolume Protocol Reference:

The SCNBoundingVolume protocol's ... methods measure the location and size of an object in the object’s local coordinate space, expressed as either a box or a sphere.

Scaling a node doesn't change its local coordinate space. So you're getting a correct result.

You might not need to do anything, though. Note under SCNPhysicsShapeScaleKey documentation (under SCNPhysicsShape class reference:

SceneKit’s physics simulation ignores the scale property of nodes containing physics bodies when simulating collisions. Instead, use this option to provide a scale factor when creating custom physics shapes. (If you create a physics body for a node without specifying a custom shape, SceneKit uses the node’s scale property to infer this scale factor at creation time.)

So if your scale factor isn't automatically applied, it appears (I haven't tested it) that passing your scale vector to SCNPhysicsShape.init(node:options:), using the options key SCNPhysicsShapeScaleKey, will do what you want.