Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SCNCamera移动摄像机&x27;s轴_Ios_Swift_Macos_Scenekit - Fatal编程技术网

Ios SCNCamera移动摄像机&x27;s轴

Ios SCNCamera移动摄像机&x27;s轴,ios,swift,macos,scenekit,Ios,Swift,Macos,Scenekit,我需要聚焦到某个节点,例如金字塔。然后将距离应用于摄影机,然后根据用户单击移动摄影机。我的方法是这样的: import SceneKit class GameViewController: UIViewController { let scene = SCNScene() override func viewDidLoad() { super.viewDidLoad() let camera = SCNCamera() camera.usesOrthographicPr

我需要聚焦到某个节点,例如金字塔。然后将距离应用于摄影机,然后根据用户单击移动摄影机。我的方法是这样的:

import SceneKit

class GameViewController: UIViewController {
let scene = SCNScene()
override func viewDidLoad() {
    super.viewDidLoad()
    let camera = SCNCamera()
    camera.usesOrthographicProjection = true
    camera.orthographicScale = 4
    camera.zNear = 1
    camera.zFar = 100
    let cameraNode = SCNNode()
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 6)
    cameraNode.camera = camera
    let cameraOrbit = SCNNode()
    cameraOrbit.name = "orbit"
    cameraOrbit.addChildNode(cameraNode)
    scene.rootNode.addChildNode(cameraOrbit)

    let Py = SCNPyramid(width: 2, height: 3, length: 2)
    Py.firstMaterial?.diffuse.contents = UIColor.purple()
    let P = SCNNode(geometry: Py)
    P.position = SCNVector3(x:0,y:0,z:2) //see the note
    scene.rootNode.addChildNode(P)

    /* N O T E :
     the position of the pyramid must not be changed
     as my intention is to rotate the camera
     not the pyramid node
     I repeat, I don't want to rotate the pyramid
     */

    let scnView = self.view as! SCNView
    scnView.scene = scene
    scnView.allowsCameraControl = false
    scnView.backgroundColor = UIColor.black()

    // user rotates the camera by tapping
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
    scnView.addGestureRecognizer(tapGesture)
}


//the function which does camera rotation :
func handleTap(_ gestureRecognize: UIGestureRecognizer) {
    //I guess the solution is around here
    //like modify the cameraOrbit.position ?
    //or cameraNode.position ?
    //tried both but doesn't work
    //or I used them wrong
    let cameraOrbit = scene.rootNode.childNode(withName: "orbit", recursively: true)!
    SCNTransaction.begin()
    SCNTransaction.animationDuration = 2
    cameraOrbit.eulerAngles.z += Float(M_PI_2) //see below
    SCNTransaction.commit()
    /*
     I realise that by performing rotation on the camera
     (the camera position is unchanged) works for z rotation,
     but this is not what I want. What I want is a solution,
     which also works for eulerAngles.x and eulerAngles.y.

     I used eulerAngles.z as example since it's easier to observe.

     I guess the true solution involves moving the camera
     with specific trajectory, not only rotation of "anchored" camera.
     */
     }

//...
}
结果是:

我想要实现的是相对于其中心进行旋转:

我的问题是,如何调整枢轴,以便实现相对于金字塔中心的旋转


注意:我不想旋转金字塔。

我找到了解决方案,但我无法解释原因。请评论,如果你能解释

摄影机动态观察位置必须设置为与对象的位置(在本例中为棱锥体)匹配。所以

然后是神秘的解决方案,将cameraOrbit.position.y乘以π的一半:

cameraOrbit.position.y += Float(M_PI_2)
//therefore we have the final cameraOrbit.position as (0, pi/2, 2)
经过测试,适用于所有cameraOrbit.eulerAngles。 但我不知道它为什么会起作用。如果π/2来自于某个投影,那么为什么它仅限于y?我的意思是,当我执行任何cameraOrbit.euleranges时,我不需要将这个pi/2指定给x或z

这里是完整的代码

import SceneKit

class GameViewController: UIViewController {
let scene = SCNScene()
override func viewDidLoad() {
    super.viewDidLoad()
    let camera = SCNCamera()
    camera.usesOrthographicProjection = true
    camera.orthographicScale = 4
    camera.zNear = 1
    camera.zFar = 100
    let cameraNode = SCNNode()
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 6)
    cameraNode.camera = camera
    let cameraOrbit = SCNNode()
    cameraOrbit.name = "orbit"
    cameraOrbit.addChildNode(cameraNode)
    scene.rootNode.addChildNode(cameraOrbit)

    let Py = SCNPyramid(width: 2, height: 3, length: 2)
    Py.firstMaterial?.diffuse.contents = UIColor.purple()
    let P = SCNNode(geometry: Py)
    P.position = SCNVector3(x:0,y:0,z:2)
    scene.rootNode.addChildNode(P)

    // S O L U T I O N :
    cameraOrbit.position = P.position
    cameraOrbit.position.y += Float(M_PI_2)
    //therefore we have the final cameraOrbit.position as (0, pi/2, 2)

    let scnView = self.view as! SCNView
    scnView.scene = scene
    scnView.allowsCameraControl = false
    scnView.backgroundColor = UIColor.black()

    // user rotates the camera by tapping
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
    scnView.addGestureRecognizer(tapGesture)
}

//the function which does camera rotation :
func handleTap(_ gestureRecognize: UIGestureRecognizer) {
    //I was wrong, the solution is not here
    let cameraOrbit = scene.rootNode.childNode(withName: "orbit", recursively: true)!
    SCNTransaction.begin()
    SCNTransaction.animationDuration = 2
    cameraOrbit.eulerAngles.z += Float(M_PI_2) //works also for x and y
    SCNTransaction.commit()

     }

...
}
import SceneKit

class GameViewController: UIViewController {
let scene = SCNScene()
override func viewDidLoad() {
    super.viewDidLoad()
    let camera = SCNCamera()
    camera.usesOrthographicProjection = true
    camera.orthographicScale = 4
    camera.zNear = 1
    camera.zFar = 100
    let cameraNode = SCNNode()
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 6)
    cameraNode.camera = camera
    let cameraOrbit = SCNNode()
    cameraOrbit.name = "orbit"
    cameraOrbit.addChildNode(cameraNode)
    scene.rootNode.addChildNode(cameraOrbit)

    let Py = SCNPyramid(width: 2, height: 3, length: 2)
    Py.firstMaterial?.diffuse.contents = UIColor.purple()
    let P = SCNNode(geometry: Py)
    P.position = SCNVector3(x:0,y:0,z:2)
    scene.rootNode.addChildNode(P)

    // S O L U T I O N :
    cameraOrbit.position = P.position
    cameraOrbit.position.y += Float(M_PI_2)
    //therefore we have the final cameraOrbit.position as (0, pi/2, 2)

    let scnView = self.view as! SCNView
    scnView.scene = scene
    scnView.allowsCameraControl = false
    scnView.backgroundColor = UIColor.black()

    // user rotates the camera by tapping
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
    scnView.addGestureRecognizer(tapGesture)
}

//the function which does camera rotation :
func handleTap(_ gestureRecognize: UIGestureRecognizer) {
    //I was wrong, the solution is not here
    let cameraOrbit = scene.rootNode.childNode(withName: "orbit", recursively: true)!
    SCNTransaction.begin()
    SCNTransaction.animationDuration = 2
    cameraOrbit.eulerAngles.z += Float(M_PI_2) //works also for x and y
    SCNTransaction.commit()

     }

...
}