In ARKit, when a horizontal plane is detected. I add a plane like structure (SCNNode
) with very small height so it looks like floor. Then i add more objects like cube etc. on tap of that plane. Now i want that user can move the cubes on that plane only, it should look like you are pushing objects on that surface only. Cube nodes also have been added to main scene node as child nodes.
I have added a pan gesture like this:
let globalPanRecognizer: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self,
action: #selector(dragObject(sender:)))
self.sceneView.addGestureRecognizer(globalPanRecognizer)
And this is what i do in the dragObject
function:
@objc func dragObject(sender: UIPanGestureRecognizer) {
if(movingNow) {
let translation = sender.translation(in: self.sceneView)
//let result : SCNVector3 = CGPointToSCNVector3(view: self.sceneView, depth: currentTouchLocationInPlane.z, point: translation)
let result : SCNVector3 = SCNVector3Make(Float(translation.x), Float(tappedObjectNode.position.y),currentTouchLocationInPlane.z)
tappedObjectNode.position = result
} else {
let hitResults = self.sceneView.hitTest(sender.location(in: self.sceneView), options: nil)
if hitResults.count > 0 {
//tappedObjectNode = (hitResults.last?.node)!
tappedObjectNode = hitResults[0].node
movingNow = true
}
}
if(sender.state == UIGestureRecognizerState.ended) {
}
}
This code results in moving the objects but they does not follow the boundary of the plane on which they are placed.
The code right now does not have any boundary restriction in it, how can i add those so if user moves cube to the edge of the plane it will not go further than that.