Tuesday, February 10, 2015

Code Jam : Swift Particle Effects

This week,  I will be going a little bit in depth into particle effects of Swift.

Particle Effects module of Swift has two parts. One is SKEmitterNode and Particle Emitter editor in Xcode.

This week, I downloaded spark.sks from Github. It was having a nice spark particle effect that fell down as if affected by gravity. What I wanted was an explosion kind of effect. Hence I had to change some of its features to make sure it showed effects that I want it to. After a lot of trial and error, I realized I had to strip away most of its movement properties in order for it to work properly.

So every time a spaceship was destroyed, spark emitter node is added and displayed on screen with the following properties.

                    let FireEmitter : SKEmitterNode = SKEmitterNode(fileNamed: "Spark");
                    FireEmitter.position = location;
                    FireEmitter.xScale = 5.8;//5.8
                    FireEmitter.yScale = 5.8
                    FireEmitter.name = "emitterSpark"
                   
                    FireEmitter.yAcceleration = 0
                    FireEmitter.xAcceleration = 0
                    
                    FireEmitter.particleSpeed = 0
                    FireEmitter.numParticlesToEmit = 1 // Number of particles to create during lifetime of the emitter
                    FireEmitter.particleSpeedRange = 0
                    FireEmitter.emissionAngle = 0
                    FireEmitter.emissionAngleRange = 0
                    FireEmitter.particleZPositionSpeed = 0 ;
                    FireEmitter.particleZPosition = 0 ;
                    FireEmitter.particleZPositionRange = 0 ;
                    FireEmitter.particleScaleSequence = nil;
                    FireEmitter.particleScaleSpeed = 0;
                    FireEmitter.runAction(TrembleSequence);
                    
                    self.addChild(FireEmitter);

Basically almost all the properties that scaled it, moved it, had to be nuked. This still needs fixing to generate more than particle during the lifetime of the emitter. But for now it looks closer to what I was expecting. 

As I change y-acceleration to 0 and run, then change it again to 5 and verify again. I am noticing something. The build time is large for a project as small as mine. I wonder if it  could be because there are no header files to separate implementation details and simplify compilation time. Also all the code is available to you, but this is similar to C++. Not explicitly imported. In C++ , if a header file is included once, its memory is reused when some other program or cpp file includes it, when #pragma once is specified, or #ifndef HEADER_H is used to define a custom preprocessor keyword, to make sure it is not compiled again. Does Swift have any such rules to optimize? I am not sure.  To be clear, the compilation is noticeably slow for a small project like mine. To be approximate, to build release build when all I did was add space to a line, was about 15 seconds. Which seems pretty slow from what I see, and others in the community also seem to agree with my observation. 

This post says Xcode 6.3 seems to have optimized Swift compilation a bit. I have 6.1, so I upgraded my Xcode to see if it made any difference at all. But it is a beta version. I do not want to disrupt my stable version. So left it at that. 

Also I got into this weird error where each run was executing old build, and not the edited latest compiled one. So I had to delete the build folder for the project and it succeeded in running the latest built executable. I was expecting Cleaning build and rebuilding should have worked, but it did not. I had to manually delete the "build" directory. 

This week's code jam is short because I spent most of my time trying to change the emitter to my likes. 

Here is my uploaded code: Its not a major change, but uploaded anyways to keep it safe in Github in case anything happens to my Mac. 
https://github.com/swtsvn/CJAW/tree/master/SwiftGame

No comments:

Post a Comment