2015-04-03 67 views
1

我想旋轉一個粒子,它是一個簡單的線條,在屏幕的中心發射一次。如何以編程方式在SceneKit中以特定角度旋轉粒子?

當我觸摸屏幕後,調用該方法,並且旋轉一直改變。對於10°或180°,圍繞着xz軸,結果是相同的:角度爲N°,然後是Y°,然後是Z°(總是不同的數字,相互間具有隨機差異:10° ,每次不會被10次偏移,而是隨機數)。你知道爲什麼嗎?

func addParticleSceneKit(str:String){ 
    var fire = SCNParticleSystem(named: str, inDirectory: "art.scnassets/Particles") 
    fire.orientationMode = .Free 
    fire.particleAngle = 90 
    //fire.propertyControllers = [ SCNParticlePropertyRotationAxis : [1,0,0] ] // should it be a SCNParticlePropertyController? I don't know how to use it then. But it would not be for an animation in my case. 
    emitter.addParticleSystem(fire) 

由於

回答

1

particleAngleVariation屬性控制在初始粒子的角度隨機變化。通常情況下,默認爲零,這意味着粒子角度不是隨機的,但是您正在從文件中加載粒子系統,因此您將獲得該文件中的任何內容 - 將其設置爲零應停止您所看到的隨機化。 (你也可以這樣對待你加載它通過編輯在Xcode該文件的文件中的粒子系統)。


順便說一句,你不添加其他新的粒子系統現場每次你想發出一個單一的粒子,你呢?遲早會造成問題。相反,保持一個單一的粒子系統,並點擊時發出更多的粒子。

想必您已設置了emissionDurationbirthRateloops性能在Xcode粒子系統編輯器,以便它發出的,當你將它添加到場景中的單個粒子?然後,只需調用它的reset方法,它就會重新開始,而不需要在場景中添加另一個。


此外,關於你的評論

fire.propertyControllers = [ SCNParticlePropertyRotationAxis : [1,0,0] ] 

它應該是一個SCNParticlePropertyController?那麼我不知道如何使用它。但這不適用於我的情況下的動畫。

閱讀the documentation可能對此有幫助。但這裏的要點是:propertyControllers應該是[String: SCNParticlePropertyController]的字典。我知道,它說[NSObject : AnyObject],但是這是因爲這個API是從ObjC導入的,它沒有類型化的集合。這就是爲什麼文檔很重要 - 它說「這個字典中的每個鍵都是粒子屬性鍵中列出的常量之一,每個鍵的值都是一個SCNParticlePropertyController對象......」這對於同樣的事情來說只是冗長的英語。

因此,傳遞一個字典,其中的鍵是一個字符串,並且該值是一個整數數組不會對您有所幫助。

docs也表示屬性控制器用於動畫屬性,並且您可以從Core Animation動畫創建一個屬性。所以,你會使用一個屬性控制器的角度,如果你想每個粒子隨時間旋轉:

let angleAnimation = CABasicAnimation() 
angleAnimation.fromValue = 0 // degrees 
angleAnimation.toValue = 90 // degrees 
angleAnimation.duration = 1 // sec 
let angleController = SCNParticlePropertyController(animation: angleAnimation) 
fire.propertyControllers = [ SCNParticlePropertyAngle: angleController ] 

或爲旋轉軸,如果你想顆粒(即已經自由,由於方向性模式和角速度旋轉)來從一個旋轉軸平滑過渡到另一個:

let axisAnimation = CABasicAnimation() 
axisAnimation.fromValue = NSValue(SCNVector3: SCNVector3(x: 0, y: 0, z: 1)) 
axisAnimation.toValue =NSValue(SCNVector3: SCNVector3(x: 0, y: 1, z: 0)) 
axisAnimation.duration = 1 // sec 
let axisController = SCNParticlePropertyController(animation: axisAnimation) 
fire.propertyControllers = [ SCNParticlePropertyRotationAxis: axisController ] 
+0

謝謝。我試圖將變化設置爲0,但它不起作用。當我嘗試添加從0到90的動畫時,結果是相同的:旋轉一直改變,每次都隨機旋轉。我用黑色背景的白線作爲圖像,用簡單的alpa從0到1,再到0。你試過了嗎?也許我犯了一個錯誤,但我不知道在哪裏。 – Paul 2015-04-03 23:55:12

+0

不起作用,請更新示例代碼 – StackUnderflow 2017-12-19 11:16:34

相關問題