gpt4 book ai didi

ios - SceneKit 子节点位置在父节点旋转期间未更改

转载 作者:行者123 更新时间:2023-11-29 00:46:57 24 4
gpt4 key购买 nike

我正在尝试在 SceneKit 中重新编码相机管理。为此,我使用 UIPanGestureRecognizer 使相机围绕物体旋转(在本例中围绕场景中心)。

接下来,我得到不同的比率或长度,以确定要在连接相机的假想球体(代码中的 cameraOrbit)的轴(X、Y、Z)处添加的角度。

我的问题是我使用相机的位置来确定要添加到球体的角度。但地位是不变的。当我改变球体旋转时,子节点相机的位置永远不会更新。然后角度永远不会改变。

import SceneKit
import UIKit

class SceneManager
{
private let scene: SCNScene
private let view: SCNView
private let camera: SCNNode
private let cameraOrbit: SCNNode
private let cameraRadius: Float

init(view: SCNView, assetFolder: String, sceneName: String, cameraName: String, backgroundColor: UIColor) {
self.view = view
self.scene = SCNScene(named: (assetFolder + "/" + sceneName))!
if (self.scene.rootNode.childNodeWithName(cameraName, recursively: true) == nil) {
print("Fatal error: Cannot find camera in scene with name :\"", cameraName, "\"")
exit(1)
}
self.camera = self.scene.rootNode.childNodeWithName(cameraName, recursively: true)! // Retrieve cameraNode created in scene file
self.cameraOrbit = SCNNode()
self.cameraOrbit.addChildNode(self.camera)
self.cameraRadius = sqrt(pow((self.camera.position.x), 2) + pow(self.camera.position.y, 2)) // CameraOrbit radius for rotation camera in panHandler
self.scene.rootNode.addChildNode(self.cameraOrbit)
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panHandler(_:)))
panGesture.maximumNumberOfTouches = 1
self.view.addGestureRecognizer(panGesture)
self.view.backgroundColor = backgroundColor
self.view.pointOfView = self.camera
self.view.scene = self.scene
}

@objc private func panHandler(sender: UIPanGestureRecognizer) {
let alpha = cos(self.camera.position.z / self.cameraRadius) // Get angle of camera

// --
print(self.camera.position) // <-------- (X, Y, Z) The axes position are always the same
// --

var ratioX = 1 - ((CGFloat)(alpha) / (CGFloat)(M_PI)) // Get the radio with angle for apply to Z and X axes rotation
var ratioZ = ((CGFloat)(alpha) / (CGFloat)(M_PI))
// Change direction of rotation depending camera's position in trigonometric circle
if (self.camera.position.z > 0 && self.camera.position.x < 0) {
ratioZ *= -1
} else if (self.camera.position.z < 0 && self.camera.position.x < 0) {
ratioX *= -1
ratioZ *= -1
} else if (self.camera.position.z > 0 && self.camera.position.x > 0) {
ratioX *= -1
}
// Set the angle rotation to add at imaginary sphere (cameraOrbit)
let xAngleToAdd = (sender.velocityInView(sender.view!).y / 10000) * ratioX
let yAngleToAdd = (sender.velocityInView(sender.view!).x / 10000) * (-1)
let zAngleToAdd = (sender.velocityInView(sender.view!).y / 10000) * ratioZ
let rotation = SCNAction.rotateByX(xAngleToAdd, y: yAngleToAdd, z: zAngleToAdd, duration: 0.5)
self.cameraOrbit.runAction(rotation)
}
}

如果有人有想法?谢谢

最佳答案

当您更新父节点的变换(位置、旋转、缩放)时,子节点的变换在局部坐标(相对于父节点)中保持不变,它仅在世界坐标中发生变化。

如果您想访问给定节点的世界变换,请使用 worldTransform属性(property)。

关于ios - SceneKit 子节点位置在父节点旋转期间未更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38521491/

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