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...
hit-test initial click location you're doing. gets
scnhittestresult
object identifying node want move, right?check
worldcoordinates
property of hit test result. if node want move child of scene'srootnode
, these vector want finding offset. (otherwise you'll need convert coordinate system of parent of node want move — seeconvertposition:tonode:
orconvertposition:fromnode:
.)you're going need reference depth point can compare
mousemoved:
locations it. useprojectpoint:
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 duringmousemoved:
.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.on
mousemoved:
, construct new 3d vector screen coordinates of new mouse location , depth value got in step 3. then, convert vector scene coordinates usingunprojectpoint:
— mouse location in scene's 3d space (equivalent 1 got hit test, without needing "hit" scene geometry).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 currentscntransaction
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
Post a Comment