opengl - How do I find my mouse point in a scene using SceneKit? -


i have set scene in scenekit , have issued hit-test select item. however, want able move item along plane in scene. continue receive mouse drag events, don't know how transform 2d coordinates 3d coordinate in scene.

my case simple. camera located @ 0, 0, 50 , pointed @ 0, 0, 0. want drag object along z-plane z-value of 0.

the hit-test works charm, how translate mouse point drag event new position in scene 3d object dragging?

you don't need use invisible geometry — scene kit can coordinate conversions need without having hit test invisible objects. need same thing in 2d drawing app moving object: find offset between mousedown: location , object position, each mousemoved:, add offset new mouse location set object's new position.

here's approach use...

  1. hit-test initial click location you're doing. gets scnhittestresult object identifying node want move, right?

  2. check worldcoordinates property of hit test result. if node want move child of scene's rootnode, these vector want finding offset. (otherwise you'll need convert coordinate system of parent of node want move — see convertposition:tonode: or convertposition:fromnode:.)

  3. you're going need reference depth point can compare mousemoved: locations it. use projectpoint: convert vector got in step 2 (a point in 3d scene) screen space — gets 3d vector x- , y-coordinates screen-space point , z-coordinate tells depth of point relative clipping planes (0.0 on near plane, 1.0 on far plane). hold onto z-coordinate use during mousemoved:.

  4. subtract position of node want move mouse location vector got in step 2. gets offset of mouse click object's position. hold onto vector — you'll need until dragging ends.

  5. on mousemoved:, construct new 3d vector screen coordinates of new mouse location , depth value got in step 3. then, convert vector scene coordinates using unprojectpoint: — mouse location in scene's 3d space (equivalent 1 got hit test, without needing "hit" scene geometry).

  6. add offset got in step 3 new location got in step 5 - new position move node to. (note: live dragging right, should make sure position change isn't animated. default duration of current scntransaction zero, don't need worry unless you've changed already.)

(this sort of off top of head, should double-check relevant docs , headers. , might able simplify bit math.)


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -