如何更改对象的位置Swift SceneKit

时间:2018-09-09 16:56:40

标签: ios swift scenekit

我在一个SceneKit中有两个对象。一个是地球,另一个是月球。它们都位于x:0,y:0,z:0并重叠。我应该如何改变月球的坐标使其围绕地球?

代码在这里:

import UIKit
import SceneKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = SCNScene()

        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()

        cameraNode.position = SCNVector3(x:0, y:0, z:10)

        scene.rootNode.addChildNode(cameraNode)

        let lightNode = SCNNode()
        lightNode.light = SCNLight()
        lightNode.light?.type = .directional
        lightNode.position = SCNVector3(x:0, y:0, z:2)

        scene.rootNode.addChildNode(lightNode)

        let stars = SCNParticleSystem(named: "StarsParticles.scnp", inDirectory: nil)!
        scene.rootNode.addParticleSystem(stars)

        let moonNode = MoonNode()
        scene.rootNode.addChildNode(moonNode)

        let sceneview = self.view as! SCNView
        sceneview.scene = scene

        let earthNode = EarthNode()
        scene.rootNode.addChildNode(earthNode)

        let sceneView = self.view as! SCNView
        sceneView.scene = scene

        sceneView.showsStatistics = false
        sceneView.backgroundColor = UIColor.black
        sceneView.allowsCameraControl = true
    }

    override var prefersStatusBarHidden: Bool {
        return true 
    }
}

2 个答案:

答案 0 :(得分:0)

您可以将视图控制器设置为SCNSceneRendererDelegate,并实现委托方法renderer:willRenderScene,该方法将为您提供time。根据该time,您可以设置以下内容:

moonNode.position = SCNVector3(r * cos(Float(time), r * sin(Float(time)), 0)

其中r是您的Earth节点的半径-如果您在编译时不知道此半径,则只需在该行上方包含以下内容:

let r = length(float3(earthNode.boundingBox.max) - float3(earthNode.boundingBox.min))

对于我所包含的代码(它会导致在xy平面内旋转),这可能不太准确,但至少会为您提供r的正确数量级。如果遇到无法找到float3或length的错误,请尝试和import simd

让我知道您是否对此有任何疑问,因为我是从内存中键入的。

文档中的更多信息:https://developer.apple.com/documentation/scenekit/scnscenerendererdelegate/1523483-renderer

答案 1 :(得分:0)

您可以使用simdPivot实例属性轻松地做到这一点。

var simdPivot: simd_float4x4 { get set }

这是测试代码:

import SceneKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = SCNScene()
        let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, 
                                                          around: SCNVector3(0,1,0), 
                                                        duration: 1))

        let earthNode = SCNNode(geometry: SCNSphere(radius: 3.57))
        earthNode.geometry?.materials.first?.diffuse.contents = UIColor.blue
        scene.rootNode.addChildNode(earthNode)

        let moonNode = SCNNode(geometry: SCNSphere(radius: 0.15))
        moonNode.geometry?.materials.first?.diffuse.contents = UIColor.yellow
        moonNode.simdPivot.columns.3.x = 5
        moonNode.runAction(action)
        scene.rootNode.addChildNode(moonNode)

        let sceneView = self.view as! SCNView
        sceneView.scene = scene

        sceneView.backgroundColor = UIColor.black
        sceneView.allowsCameraControl = true
    }
}

enter image description here