
我找到了有关过程elsewhere on Stack Overflow的有用描述.使用该描述,我编写了下面代码的初始版本.我的问题是点击坐标和片段节点位置之间的差异 – 它们的坐标是不同的数量级.我仍然不清楚如何匹配它们 – 把它们放在“同一个宇宙”中.我已经尝试了Apple论坛的一些建议以及我自己的幻想.
这是我目前的尝试,主要是基于上面的链接恢复到原始版本,以及沿途记录的坐标值.结果是拖动棋子会导致它突然跳出屏幕:
- (NSPoint)vIEwPointForEvent: (NSEvent *) event_{ NSPoint windowPoint = [event_ locationInWindow]; NSPoint vIEwPoint = [self.vIEw convertPoint: windowPoint fromVIEw: nil]; return vIEwPoint;}- (SCNHitTestResult *)hitTestResultForEvent: (NSEvent *) event_{ NSPoint vIEwPoint = [self vIEwPointForEvent: event_]; CGPoint cgPoint = CGPointMake (vIEwPoint.x,vIEwPoint.y); NSArray * points = [(SCNVIEw *) self.vIEw hitTest: cgPoint options: @{}]; return points.firstObject;}- (voID)mouseDown: (NSEvent *) theEvent{ SCNHitTestResult * result = [self hitTestResultForEvent: theEvent]; SCNVector3 clickWorldCoordinates = result.worldCoordinates; log output: clickWorldCoordinates x 208.124578,y -12827.223365,z 3163.659073 SCNVector3 screenCoordinates = [(SCNVIEw *) self.vIEw projectPoint: clickWorldCoordinates]; log output: screenCoordinates x 245.128906,y 149.335938,z 0.985565 // save the z coordinate for use in mouseDragged mouseDownClickOnObjectZCoordinate = screenCoordinates.z; selectedPIEce = result.node; // save selected pIEce for use in mouseDragged SCNVector3 pIEceposition = selectedPIEce.position; log output: pIEceposition x -18.200000,y 6.483060,z 2.350000 offsetofMouseClickFromPIEce.x = clickWorldCoordinates.x - pIEceposition.x; offsetofMouseClickFromPIEce.y = clickWorldCoordinates.y - pIEceposition.y; offsetofMouseClickFromPIEce.z = clickWorldCoordinates.z - pIEceposition.z; log output: offsetofMouseClickFromPIEce x 226.324578,y -12833.706425,z 3161.309073 }- (voID)mouseDragged: (NSEvent *) theEvent;{ NSPoint vIEwClickPoint = [self vIEwPointForEvent: theEvent]; SCNVector3 clickCoordinates; clickCoordinates.x = vIEwClickPoint.x; clickCoordinates.y = vIEwClickPoint.y; clickCoordinates.z = mouseDownClickOnObjectZCoordinate; log output: clickCoordinates x 246.128906,y 0.000000,z 0.985565 log output: pIEceWorldtransform = m11 = 242.15889219510001,m12 = -0.000045609300002524833,m13 = -0.00000721691076126,m14 = 0,m21 = 0.0000072168760805499971,m22 = -0.000039452697396149999,m23 = 242.15890446329999,m24 = 0,m31 = -0.000045609300002524833,m32 = -242.15889219510001,m33 = -0.000039452676995750002,m34 = 0,m41 = -4268.2349924762348,m42 = -12724.050221935429,m43 = 4852.6652710104272,m44 = 1) SCNVector3 newPIEceposition; newPIEceposition.x = offsetofMouseClickFromPIEce.x + clickCoordinates.x; newPIEceposition.y = offsetofMouseClickFromPIEce.y + clickCoordinates.y; newPIEceposition.z = offsetofMouseClickFromPIEce.z + clickCoordinates.z; log output: newPIEceposition x 472.453484,z 3162.294639 selectedPIEce.position = newPIEceposition;} 到目前为止,我已经获得了许多有趣且有用的评论和建议.但我已经意识到要继续前进,我可能需要一个工作代码示例,它显示允许点击和向量存在于“相同的宇宙”中的秘密酱.
解决方法 你不需要“玩”来找到原点.你可以这样做代码:let originZ = sceneVIEw.projectPoint(SCNVector3Zero).zlet vIEwLocation = /* Location in SCNVIEw coordinate system */let vIEwLocationWithOriginZ = SCNVector3( x: float(vIEwLocation.x),// X & Y are in vIEw coordinate system y: float(vIEwLocation.y),z: originZ // Z is in scene coordinate system)var position = sceneVIEw.unprojectPoint(vIEwLocationWithOriginZ)/* "position" is in the scene's coordinate system with z of 0 */总结
以上是内存溢出为你收集整理的macos – 场景工具包 – 拖动对象全部内容,希望文章能够帮你解决macos – 场景工具包 – 拖动对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)