gpt4 book ai didi

swift - 旋转 SCNNode 然后相对于旋转移动?

转载 作者:搜寻专家 更新时间:2023-11-01 07:03:47 24 4
gpt4 key购买 nike

我的节点设置:

let node = SCNNode()
node.position = SCNVector3(0.0, 0.0, 0.0)

然后我希望将节点向左旋转 90 度,这可以通过以下方式实现:

node.transform = SCNMatrix4Rotate(node.transform, .pi/2, 0.0, 1.0, 0.0)

接下来,我想将节点相对于当前旋转沿负 z 轴向前平移一个单位,并以:

node.position = SCNVector3(-1.0, 0.0, 0.0)

我不知道如何以编程方式从节点的旋转到节点的位置。

Basically node begins at (0, 0, 0), its forward vector is the -z axis, node turns left and moves one unit forward to end up at (-1, 0, 0).

这不起作用:

func move(_ direction: moveDirection) {
switch direction {
case .forward: characterNode.position = SCNVector3(characterNode.position.x, characterNode.position.y, characterNode.position.z - 1.0)
case .left: characterNode.pivot = SCNMatrix4Rotate(characterNode.pivot, -.pi/32, 0.0, 1.0, 0.0)
case .backward: characterNode.position = SCNVector3(characterNode.position.x, characterNode.position.y, characterNode.position.z + 1.0)
case .right: characterNode.pivot = SCNMatrix4Rotate(characterNode.pivot, .pi/32, 0.0, 1.0, 0.0)
}
}

最佳答案

假设我没有理解错,一旦你旋转了你的 SCNNode,你想要将它朝那个方向向前移动。

这可以通过使用 worldFront 值来完成,它很简单:

The local unit -Z axis (0,0,-1) in world space.

因此这可能是您正在寻找的答案:

    //1. Create A Node Holder
let nodeToAdd = SCNNode()

//2. Create An SCNBox Geometry
let nodeGeometry = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
nodeGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
nodeGeometry.firstMaterial?.lightingModel = .constant
nodeToAdd.geometry = nodeGeometry

//3. Position It 1.5m Away From The Camera
nodeToAdd.position = SCNVector3(0, 0, -1.5)

//4. Rotate The Node By 90 Degrees On It's Y Axis
nodeToAdd.rotation = SCNVector4Make(0, 1, 0, .pi / 2)

//5. Add The Node To The ARSCNView
augmentedRealityView.scene.rootNode.addChildNode(nodeToAdd)

//6. Use The World Front To Move The Node Forward e.g

/*
nodeToAdd.simdPosition += nodeToAdd.simdWorldFront * 1.2
*/

nodeToAdd.simdPosition += nodeToAdd.simdWorldFront

//7. Print The Position
print(nodeToAdd.position)

/*
SCNVector3(x: -0.999999881, y: 0.0, z: -1.50000024)
*/

关于swift - 旋转 SCNNode 然后相对于旋转移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49375366/

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