2016-11-16 60 views
0

我正在尋找一種輕鬆旋轉SceneKit中的SCNNode的方法。所以我們假設我有兩個矢量h = (1 0 0)u = (0 1 0)。 (在我的腦海中)指向節點正在前進的方向,而u指向上。SceneKit使用兩個矢量設置旋轉

旋轉後我想hh' = (0 5 1)uu' = (0 -1 -5)。所以兩個矢量之間的角度仍然是90度。

如何旋轉我的SCNNode那樣?

回答

0

我不知道你想做什麼,但基本上你做的是(把在操場的代碼):

import UIKit 
import SceneKit 
import PlaygroundSupport 

let scnView = SCNView(frame: CGRect(x: 0, y: 0, width: 500, height: 500)) 
scnView.autoenablesDefaultLighting = true 

let scene = SCNScene() 
scnView.scene = scene 

let light = SCNLight() 
light.type = .directional 
let lightNode = SCNNode() 
lightNode.light = light 
scene.rootNode.addChildNode(lightNode) 

let camera = SCNCamera() 
let cameraNode = SCNNode() 
cameraNode.camera = camera 
cameraNode.position = SCNVector3(0,0,10) 
scene.rootNode.addChildNode(cameraNode) 

let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0) 
let boxNode = SCNNode(geometry: box) 
boxNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red 
scene.rootNode.addChildNode(boxNode) 

let rotateAction = SCNAction.rotateBy(x: 0, y: -1, z: 5, duration: 1) 
let repeatAction = SCNAction.repeatForever(rotateAction) 
SCNTransaction.begin() 
SCNTransaction.animationDuration = 1 
boxNode.runAction(repeatAction) 
SCNTransaction.commit() 

PlaygroundPage.current.liveView = scnView 
+0

這並不符合要求。 'rotateBy'以弧度旋轉一個角度。所以如果你有一個向量'(1 0 0)'並且將它在y軸上旋轉-1弧度並且在z軸上旋轉5弧度,你將不會得到'(1 -1 5)',但是完全不同的東西。 –

+0

換句話說:如果我在原始問題中給出的位置上有兩個節點,並且每個節點都包含一個球體,那麼我將如何旋轉'rootNode'使它們最終在原始問題的最終位置? –

+0

只需將它們作爲子節點添加到空節點,將空節點添加到場景rootNode。當您旋轉該空節點時,childNodes保持在同一位置。這是場景圖背後的基本思想。欲瞭解更多信息,請看這裏:https://en.wikipedia.org/wiki/Scene_graph –