Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move a specific node in SceneKit using touch

Tags:

ios

move

scenekit

I have a scene with multiple nodes. I want to select a node by tapping it (if I tap nothing I want nothing to happen) and make him follow my finger only on XY axis (I know position on Z axis). Is there any method that converts location in view to SceneKit coords?

After few researches I found this and it's exactly what I want, but I don't get the code. Can somebody explain me or help me figure how can I solve my problem? https://www.youtube.com/watch?v=xn9Bt2PFp0g

like image 977
Alec Firtulescu Avatar asked Sep 29 '22 09:09

Alec Firtulescu


1 Answers

func CGPointToSCNVector3(view: SCNView, depth: Float, point: CGPoint) -> SCNVector3 {
    let projectedOrigin = view.projectPoint(SCNVector3Make(0, 0, depth))
    let locationWithz   = SCNVector3Make(Float(point.x), Float(point.y), projectedOrigin.z)
    return view.unprojectPoint(locationWithz)
}

Looks like was pretty simple, I've made a function that gets 3 parameters. View is the SCNView where scene is attached to, depth is the z value of node, and point is a CGPoint that represents projection of 3D scene on screen.

like image 107
Alec Firtulescu Avatar answered Oct 06 '22 20:10

Alec Firtulescu