I’m new to Swift and SceneKit and I am trying to implement a solution where users can add a 3D object to a scene and it will be placed on the edge of a plane’s surface.
I tried using the plane node’s bounding box values to assume its edge and the object’s potential position, but since the plane is a custom geometry, the object often ends up outside of the plane's surface.
On top of that, if the plane's specific edge is diagonal line, I also need the object to rotate so it always runs parallel to the plane’s edge.
Here’s a snippet of my code and an image illustrating the problem and the desired outcome:
//creating plane out of custom geometry values
let surfaceNode = SCNNode(geometry: surfaceGeometry)
//defining plane’s bounding box & center point
let minimum = SIMD3<Float>(surfaceNode.boundingBox.min)
let maximum = SIMD3<Float>(surfaceNode.boundingBox.max)
let translation = (maximum - minimum) * 0.5
//fixing pivot point
surfaceNode.pivot = SCNMatrix4MakeTranslation(surfaceNode.boundingBox.min.x + translation.x, 0, surfaceNode.boundingBox.min.z + translation.z)
//adding custom plane to the scene
surfaceNode.position = SCNVector3(0, 0 ,0)
sceneView.scene.rootNode.addChildNode(surfaceNode)
//creating object
let object = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
let objectNode = SCNNode(geometry: object)
objectNode.pivot = SCNMatrix4MakeTranslation(0, -0.1, 0)
//positioning object on top of the plane’s surface
objectNode.position = SCNVector3(surfaceNode.position.x, surfaceNode.position.y, surfaceNode.position.z - translation.z)
//adding object to scene
sceneView.scene.rootNode.addChildNode(objectNode)