In an application using ARKit, I am using the ARSCNPlaneGeometry class update method to obtain a SCNGeometry from an ARPlaneGeometry. I obtain a SCNPhysicsShape from that geometry to use as the SCNPhysicsBody for a plane node:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }
// Plane Geometry
let planeGeometry = ARSCNPlaneGeometry(device: MTLCreateSystemDefaultDevice()!)
planeGeometry?.update(from: planeAnchor.geometry)
let planeNode = SCNNode(geometry: planeGeometry)
// Plane Physics
planeNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: planeGeometry!))
// Add Node to Scene
node.addChildNode(planeNode)
}
The problem I have is that even though the plane node displayed is a plane, the physics shape that is generated is not that of a plane but that of a rough sphere. I know this because I am using the scene view debug option .showPhysicsShapes.
I am also continuously updating the geometry and physics shape using the renderer(_:didUpdate:for:) method of the ARSCNViewDelegate and the shape remains the same.
Anybody else run into this problem before?
Well, it seems the shape abstraction that the SCNPhysicsShape class calculates from an ARSCNPlaneGeometry is completely off unless you help it a little bit with an option. If instead of:
planeNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: planeGeometry!))
you use:
planeNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: planeGeometry!, options: [SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.boundingBox]))
The "approximate", although not quite exact, shape is obtained.