【问题标题】:SCNVector4: Rotate on x-axisSCNVector4:在 x 轴上旋转
【发布时间】:2017-09-06 08:01:19
【问题描述】:

我有一个球体并设法旋转它。但不幸的是,沿着错误的轴线。

我的目标是像地球一样沿 x 轴旋转。你能帮我申请一下吗?

这是我现有的代码:

let spin = CABasicAnimation(keyPath: "rotation")
        // Use from-to to explicitly make a full rotation around z
        spin.fromValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: 0))
        spin.toValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: Float(CGFloat(-2 * Double.pi))))
        spin.duration = 30
        spin.repeatCount = .infinity
        sphereNode.addAnimation(spin, forKey: "spin around")

【问题讨论】:

    标签: swift augmented-reality arkit cabasicanimation


    【解决方案1】:

    会是这样的

    self.imageView.layer.transform = CATransform3DConcat(self.imageView.layer.transform, CATransform3DMakeRotation(M_PI,1.0,0.0,0.0));
    

    【讨论】:

    • 球体上映射的图像已经在旋转。只是方向不对。我不明白你的代码中的 imageView 是什么?
    • 这是一个球体图像
    【解决方案2】:

    您已经有了可行的解决方案:

    spin.fromValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: 0))
    spin.toValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: CGFloat.pi * 2))
    

    如果你想改变方向,只需改变向量:)

    spin.fromValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: CGFloat.pi * 2))
    spin.toValue = NSValue(scnVector4: SCNVector4(x: 1, y: 0, z: 0, w: 0))
    

    【讨论】:

    • 是的,它正在旋转。但是沿着错误的轴。我不明白 w 是什么。
    • @netshark1000 哦,改变轴 - 改变 x、y、z:旋转 x = SCNVector4(x: 1, y: 0, z: 0, w: CGFloat.pi * 2),旋转 y = SCNVector4(x: 0, y: 1, z: 0, w: CGFloat.pi * 2),旋转 z = @ 987654328@
    【解决方案3】:

    您可以使用 SceneKit 的事务围绕 X 轴旋转地球模型:

    let scene = SCNScene(named: "art.scnassets/model.scn")!
    let modelEarth = scene.rootNode.childNode(withName: "model", 
                                              recursively: true)!
    
    SCNTransaction.begin()
    SCNTransaction.animationDuration = 500           // animation in seconds
    SCNTransaction.animationTimingFunction = .init(name: .default)
    modelEarth.rotation.x = 1
    modelEarth.rotation.y = 0
    modelEarth.rotation.z = 0
    modelEarth.rotation.w = 100 * Float.pi           // fourth component
    SCNTransaction.commit()
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多