Calculate the Position on rotated node using unProject on plane

64 Views Asked by At

Please HELP

I am not good in maths

Update --> What I am trying to do is drag the Orthographic camera using mouse pointer around the screen , Below calculation is works when Camera is not rotated otherwise it is only works in the one direction

SO I am able to drag the camera using gestures on the 2D screen

to do this I did following , Which is only work if Camra is not rotated ,

Now Suppose Camera is rotated eular angel 90 degree this calculation not working

How can I add initial rotation in this calculation

Please suggest

var previousLocation = SCNVector3(x: 0, y: 0, z: 0)
@objc func panned(gesture:UIPanGestureRecognizer) {


let view = self.sceneView1!
let translation = gesture.translation(in: view)

let location = gesture.location(in: view)
let secLocation = CGPoint(x: location.x + translation.x, y: location.y + translation.y)

let P1 = view.unprojectPoint(SCNVector3(x: Float(location.x), y: Float(location.y), z: 0.0))
let P2 = view.unprojectPoint(SCNVector3(x: Float(location.x), y: Float(location.y), z: 1.0))

   let Q1 = view.unprojectPoint(SCNVector3(x: Float(secLocation.x), y: Float(secLocation.y), z: 0.0))
   let Q2 = view.unprojectPoint(SCNVector3(x: Float(secLocation.x), y: Float(secLocation.y), z: 1.0))

   let t1 = -P1.z / (P2.z - P1.z)
   let t2 = -Q1.z / (Q2.z - Q1.z)

   let x1 = P1.x + t1 * (P2.x - P1.x)
   let y1 = P1.y + t1 * (P2.y - P1.y)

   let P0 = SCNVector3Make(x1, y1,0)

   let x2 = Q1.x + t1 * (Q2.x - Q1.x)
   let y2 = Q1.y + t1 * (Q2.y - Q1.y)

   let Q0 = SCNVector3Make(x2, y2, 0)

   var diffR = Q0 - P0

  diffR = SCNVector3Make(diffR.x * -1, diffR.y * -1, diffR.z * -1)

let cameraNode = view.pointOfView

   switch gesture.state {
   case .began:
       previousLocation = cameraNode!.position
       break;
   case .changed:
    cameraNode?.position = SCNVector3Make(previousLocation.x + diffR.x, previousLocation.y + diffR.y, previousLocation.z + diffR.z)
       break;
   default:
       break;
   }

 }

Update 2


formula I have used is

P1 , P2 ==> ORIGIN 3D VECTOR (X,Y,Z ) AND Q1, Q2 ==> NEW 3D VECTOR (WHERE I NEED TO TRANSLATE)

T1 ,T2 , X1,Y1,PO,Q0 Is calculated as per above

enter image description here