gpt4 book ai didi

macos - 场景套件 - 拖动对象

转载 作者:行者123 更新时间:2023-12-03 16:15:21 26 4
gpt4 key购买 nike

我正在尝试使用场景套件用鼠标/触控板拖动棋子。所有对象(棋盘和棋子)都是根节点的子节点,从 Collada 文件加载。

我找到了对 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, y -12833.706425, z 3162.294639

selectedPiece.position = newPiecePosition;
}

到目前为止,我已经收到了很多有趣且有用的评论和建议。但我意识到,要继续前进,我可能需要一个工作代码示例,它展示了允许点击和矢量存在于“同一个宇宙”中的 secret 武器。

最佳答案

你不需要“玩弄”来找到起源。您可以执行如下代码:

let originZ = sceneView.projectPoint(SCNVector3Zero).z
let 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 - 场景套件 - 拖动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23707582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com