Tuesday, March 24, 2015

Code Jam : Swift Physics, Joints, FieldNodes.

This week, I created something similar to pendulum, with Physics bodies, physics joints, and Field Nodes.

I say something similar, because it did not reach the exact pendulum swing that I wanted. But it is closer. If the values are tweaked a bit, it would reach the optimal pendulum swing motion. Before that I thought I would publish this post.

First create a simple empty game in Xcode for swift language.  In GameScene.swift, inside class GameScene, in didMoveToView(), add the following nodes.

/* Setup your scene here */
        let myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "Pendulum";
        myLabel.fontSize = 15;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
        self.addChild(myLabel)

        
These lines create label node, set its font size, and mention the idea of the game.  Though the empty project is a game. This is no where closer to being a game. This is purely for experimentation and blogging purpose. 

Place the label at the center of the screen. CGRectGetMidX() is a CGGeometry function which returns center x position of the rectangle passed as the parameter. Similarly CGRectGetMidY. Csdfs


I created three main nodes. One is a rectangle which will act as the rod. A circle which will act as the bob. And an invisible circle which will act as the anchor point for the rod. It will be on the other side of the rod, on opposite end to the bob circle. 

        let rectNode = SKShapeNode(rect: CGRectMake(500,300, 5, 250))
        rectNode.strokeColor = SKColor.greenColor()
        rectNode.fillColor = SKColor.yellowColor();
        rectNode.lineWidth = 1
        let rectBody = SKPhysicsBody(rectangleOfSize: rectNode.frame.size)
        rectBody.affectedByGravity = false
        rectBody.mass  = 5
        rectBody.dynamic = true // should be dynamic
        rectNode.physicsBody = rectBody;
        self.addChild(rectNode)

This snippet creates a rectangle node positioned at 500,300. Stroke color is green, fill color is yellow. You can provide any color you want. I simply created colors to provide contrast against black background. 

Then comes the physics body. The Physics body gives movement to the node, and makes it behave in accordance with the gravity law. Collision is taken care of by the physics bodies. 

Create a rectangular physics body of the same size as that of the rectangular node. Then I made sure the physics body is not affected by gravity because, I want them to be affected only by the two Field bodies' gravitational forces. 

Let the physics body have a mass of 5.  Make sure dynamic is set to true, so that they can move with respect to the forces acting on them. 

Set the physics Body of the rectangular SKNode to the newly created physics body. Then add the node to the scene. Scene is the parent of the node. 

Similarly create circle node :
        let circleNode = SKShapeNode(circleOfRadius: 20);
        circleNode.position.x = 500;
        circleNode.position.y = 300
        circleNode.strokeColor = SKColor.greenColor()
        circleNode.fillColor = SKColor.orangeColor()
        let circleBody = SKPhysicsBody(circleOfRadius: 20)
        circleBody.affectedByGravity = rectBody.affectedByGravity
        circleBody.mass = 10
        circleBody.dynamic = true //should be dynamic
        circleNode.physicsBody = circleBody

        self.addChild(circleNode)

And create the anchor node:
  
        let AnchorNode = SKShapeNode(circleOfRadius: 25);
        AnchorNode.position.x = circleNode.position.x
        AnchorNode.position.y = circleNode.position.y + 250
        AnchorNode.alpha = 0
        AnchorNode.strokeColor = SKColor.greenColor()
        AnchorNode.fillColor = SKColor.redColor()
        let AnchorBody = SKPhysicsBody(circleOfRadius: 20)
        AnchorBody.affectedByGravity = false
        AnchorBody.dynamic = false //should be dynamic
        
        AnchorNode.physicsBody = AnchorBody
        AnchorBody.affectedByGravity = false
        self.addChild(AnchorNode)

The important points to note in the anchor node are as follows: Anchor node is invisible because I want the rod to look closer to a real pendulum. Position is same as that of the other end of the rod. It also has a body but it is not affected by gravity. It is not dynamic as well. It should stay put like a nail on the wall. It needs a physics body, in order to create a Pin joint later with the rod. Red circle is the anchor, orange is the pendulum bob.



For the sake of clarity, the red circle has an alpha of 1, and is visible. But it will be rendered transparent in the final code. 

Now that the three major nodes are created, I am going to create two Field Nodes which would act as radial gravity nodes, which means, objects would be attracted towards them and would follow a natural curve of moving towards them and moving away from them. 

I tried to create pendulum with one such node. but It did not give expected results, hence I am falling back to two nodes. 




The cyan colored circles are the Field nodes which would try to pull the bob towards them. Note: SKFieldNode is available only for iOS 8.0 and above. 

Here is the code snippet for them. 


        let GravityNode = SKFieldNode.radialGravityField()
        GravityNode.position.x = 350
        GravityNode.position.y = 290
        GravityNode.strength = 6
        GravityNode.falloff = 3.5
        

        self.addChild(GravityNode)

lAs we already know, let keyword creates a constant reference to the object, meaning, the GravityNode cannot point to any other object. The properties inside the object are editable, but not the GravityNode. 

Set the position of one of the radial gravity node on the left side of the pendulum. Set its strength to be 6, and falloff to be 3.5 . Strength is the pull strength of its gravity. FallOff is the variable which sets the exponent value which determines how much of gravitational pull the objects would feel, after a certain distance. 

Strength and falloff had to be tweaked in order to get a smooth swinging pendulum. 

Add the node to the scene. 

        let GCircleNode = SKShapeNode(circleOfRadius: 25);
        GCircleNode.position = GravityNode.position
        GCircleNode.strokeColor = SKColor.greenColor()
        GCircleNode.fillColor = SKColor.cyanColor()
        GCircleNode.alpha = 1
        self.addChild(GCircleNode)

This is a debug node which was created to show the position of the field node. This is not required to be rendered for the final run. 

Similarly create a second field node, to pull the pendulum towards the other direction. 

        let GravityNode1 = SKFieldNode.radialGravityField()
        GravityNode1.position.x = 650
        GravityNode1.position.y = GravityNode.position.y
        GravityNode1.strength = GravityNode.strength
        GravityNode1.falloff = GravityNode.falloff
        self.addChild(GravityNode1)

        
        let GCircleNode1 = SKShapeNode(circleOfRadius: 25);
        GCircleNode1.position = GravityNode1.position
        GCircleNode1.strokeColor = SKColor.greenColor()
        GCircleNode1.fillColor = SKColor.cyanColor()
        GCircleNode1.alpha = 1
        self.addChild(GCircleNode1)

Now the rod needs to be pinned to the invisible anchor node, and the bob circle needs to be pinned to the other end of the rod, in order to move in alignment  with the rod. 

        let PinJoint = SKPhysicsJointPin.jointWithBodyA(circleNode.physicsBody, bodyB: 
          rectNode.physicsBody, anchor: circleNode.position)
        
        let stickJoint = SKPhysicsJointPin.jointWithBodyA(rectNode.physicsBody, bodyB: 
          AnchorNode.physicsBody, anchor: AnchorNode.position)
        
        self.physicsWorld.addJoint(PinJoint)
        self.physicsWorld.addJoint(stickJoint)

The physics joint Pin, fixes bodyA to Body B at a certain point. This point is called anchor point. Body A is circle (bob) and body B is the rod in the first PinJoint. They are fixed at the bob's position. 

Second stickJoint(for lack of better term) joins the rod and the invisible anchor node at the anchor node's position. 

Add these two joints to the world. Now since the anchor's dynamic property is set to false, and it is not affected by gravity, It would stay put, and the rod has no other option than to stay put at that end. But the other end can move freely, and the bob circle will move whereever the other end moves because it is pinned to the rod at that point. 

Now we have a working pendulum, whose bob is being pulled equally by left and right gravity nodes. When the bob reaches the center after being pulled by right node, it will be pulled by the left one, thereby swinging from one end to the other. 




Code Jam for the week of Mar 15 2015
Project uploaded here : https://github.com/swtsvn/CJAW/tree/master/PhysicsGame

Sunday, March 8, 2015

Code Jam : Swift : Gradient and Graphics

Today I am going to enter graphics in swift, or at least the basics of it. 
In a simple view controller, ( I assume you know how to create a view controller by now, if not do refer to my previous blogs), Create a view. A view is nothing but a sub window inside a view controller. It is a subclass of UIView, and can be used to draw elements on screen. 

This week, I am going to create a bezier path with transformations, and color it with gradient color. This would be the simplest of all the graphics we can do in swift, but this is where it starts. 

In viewController.swift (or the file name you have for the main view controller), create a new class called MyView (or any other name you prefer), and subclass it to UIView.

class MyView : UIView{

Override the function drawRect().

override func drawRect(rect: CGRect) {

1. Transformations : I assume the readers know about graphics transformations. If not here are few good pages on it (http://www.cs.uic.edu/~jbell/CourseNotes/ComputerGraphics/2DTransforms.html and http://www.cs.utexas.edu/~fussell/courses/cs384g-fall2010/lectures/lecture07-Affine.pdf)
In this week, I am going to add rotation, and translation to the bezier curve. Rotation is created by Core Graphics function  CGAffineTransformMakeRotation.  cThis function creates a rotation matrix from unit matrix. The argument is angle in radians.CGAffineTransformMakeRotation is one of the methods to rotate objects on screen. The other way is to directly use CGContextRotateCTM, which rotates within a context. For this week, we will see affine transformation matrix. Eventually I will end up using context to append the transformation matrices.

 var rot = CGAffineTransformMakeRotation(CGFloat(M_PI/ 4)

The angle of rotation is 45 degrees ( Pi/4).  Then I translate the object to bring it inside the view. 


var trans = CGAffineTransformMakeTranslation(100, -150)

This creates another matrix that would translate the object by 100 units along x axis and -150 units along y axis. Since iOS coordinate system origin is at top left corner of screen, this would move the object towards right and up.

Next I am saving the current context, this is to make sure, we can restore back to the previous context after our customized changes to the current transformation matrix (CTM)

 let context = UIGraphicsGetCurrentContext()
 CGContextSaveGState(context)
UIGraphicsGetCurrentContext This function returns back a  CGContextRef of the current context ( https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html ) being used. Current context is nil by default. the UIView object (MyView class object in our case) pushes a valid context into the stack, making it current, to draw objects, and this happens just before calling drawRect() method. In case you are not using UIView object, then you must manually push context using UIGraphicsPushContext method. 

 CGContextConcatCTM(context, rot)
 CGContextConcatCTM(context, trans)

These lines concatenates rotation matrix, and translation matrix to the existing matrix of the context. The order is important. These lines would first rotate the object then translate. 

2. Bezier Curve : Next is to draw a bezier curve (  http://web.iitd.ac.in/~hegde/cad/lecture/L13_Bezi ercurve.pdf ) . To create the curve programmatically. I am creating a rectangle with a circle at its bottom to look similar to a pendulum but not exactly one. This would include two bezier paths inside one. 

First create the rectangle path. 
  var bezierPath = UIBezierPath();
  let r = CGRectInset(rect, rect.size.width * 0.475, rect.size.height * 0.05)
  var rpath = UIBezierPath(roundedRect: r, cornerRadius: 60.0)
      
Create a new UIBezierPath, then create a rounded rectangle by using CGRectInset with rectangle as the view’s rectangle itself, and the roundness to reduce the width by a lot, but height by a small fraction, so that a long slender rectangle is created. Then create a bezier path using the provided easy API UIBezierPath instead of creating your own control points using  addLineToPoint or addCurveToPoint functions. 

Then create another bezier path that would create a circle. 
let newrect = CGRectMake(20, 105, 250  , 250)
let h = CGRectInset(newrect, newrect.size.width * 0.3, newrect.size.width * 0.3)
var headpath = UIBezierPath(ovalInRect: h);
let rad = 20.0
        
This creates another rectangle that would position the circle at the bottom part of the rectangle that I already created. CGRectInset is used to create circle with this rectangle. UIBezierPath is used to create bezier path. using ovalInRect parameter. 

 rpath.appendPath(headpath)
 bezierPath.appendPath(rpath);
 bezierPath.addClip()

Add headpath to rpath ( circle to rectangle), then append that combined path to the main empty bezierPath. Then do addClip(). This method intersects the shape with current clipping region of the graphics context. This method is important to create the gradient color filling to the path. 

3. Gradient filling:

 let colorspace = CGColorSpaceCreateDeviceRGB()
 let g1 = UIColor(red: 0.5, green: 0.2, blue: 0.8, alpha: 1)
 let g2 = UIColor(red: 1, green: 0.6, blue: 0.2, alpha: 1
 let gc : CFArray = [g1.CGColor, g2.CGColor, UIColor.blueColor().CGColor, UIColor.redColor().CGColor]
 let gl : [CGFloat] = [0, 0.3,0.5,1]
 let gra = CGGradientCreateWithColors(colorspace, gc, gl)
       
 CGContextDrawLinearGradient(context, gra, CGPointMake(self.bounds.width/2, self.bounds.height - 20), CGPointMake(self.bounds.width / 2, 20), 0)

First create device dependent RGB color space by using CGColorSpaceCreateDeviceRGB . This is important to set the colors for the gradient. 
Create two colors g1 and g2, and setup 4 colors for the gradient to change from. Then create color array using 4 colors (g1, g2, blue, red). Create coordinates array, which specifies where the color should change. This is again an array of 4 elements. 
Then create gradient with colors and coordinates. using CGGradientCreateWithColors . Then draw gradient colors. Since we already clipped using addClip, the gradient colors will be applied to the bezier path. 

 CGContextDrawLinearGradient(context, gra, CGPointMake(self.bounds.width/2, self.bounds.height - 20), CGPointMake(self.bounds.width / 2, 20), 0)
       
 CGContextRestoreGState(context)

CGContextDrawLinearGradient is used to draw the gradient colors to the bezier shapes.  The third and fourth parameters are CGPoints that define the coordinates for starting and ending points.  CGContextRestoreGState is used to restore the context that was used before we saved it and changed it. 
For a simple project like this one, saving and restoring might not be necessary. But it is required for complex ones with multiple objects to be rendered on the screen. 

The final rendered view looks like this :

The viewController.swift code :


import UIKit

class MyView : UIView{
   
override func drawRect(rect: CGRect) {
       
       
       
       
// 1. transformations
        var rot = CGAffineTransformMakeRotation(CGFloat(M_PI) / 4)
       
var trans = CGAffineTransformMakeTranslation(100, -150)
       
       
let context = UIGraphicsGetCurrentContext()
       
CGContextSaveGState(context)
       
CGContextConcatCTM(context, rot)
       
CGContextConcatCTM(context, trans)
       
   
       
//bezier path
        var bezierPath = UIBezierPath();
       
       
let r = CGRectInset(rect, rect.size.width * 0.475, rect.size.height * 0.05)
       
var rpath = UIBezierPath(roundedRect: r, cornerRadius: 60.0)
      
       
       
let newrect = CGRectMake(20, 105, 250  , 250)
       
let h = CGRectInset(newrect, newrect.size.width * 0.3, newrect.size.width * 0.3)
       
var headpath = UIBezierPath(ovalInRect: h);
       
let rad = 20.0
     
        rpath.
appendPath(headpath)
   
       
        bezierPath.
appendPath(rpath);
        bezierPath.
addClip()
      
       
//gradient
        let colorspace = CGColorSpaceCreateDeviceRGB()
       
let g1 = UIColor(red: 0.5, green: 0.2, blue: 0.8, alpha: 1)
       
let g2 = UIColor(red: 1, green: 0.6, blue: 0.2, alpha: 1)
       
let gc : CFArray = [g1.CGColor, g2.CGColor, UIColor.blueColor().CGColor, UIColor.redColor().CGColor]
       
let gl : [CGFloat] = [0, 0.3,0.5,1]
       
       
let gra = CGGradientCreateWithColors(colorspace, gc, gl)
       
       
CGContextDrawLinearGradient(context, gra, CGPointMake(self.bounds.width/2, self.bounds.height - 20), CGPointMake(self.bounds.width / 2, 20), 0)
       
       
CGContextRestoreGState(context)
    
    }
}


class MyViewController: UIViewController{
  
   
   
let myview = MyView(frame: CGRectMake( 10, 100, 300, 300))
   
@IBOutlet var colorLabel: UILabel!
   
override func viewDidLoad() {
       
super.viewDidLoad()
       
// Do any additional setup after loading the view, typically from a nib.
        view.addSubview(myview);
    }

   
override func didReceiveMemoryWarning() {
       
super.didReceiveMemoryWarning()
       
// Dispose of any resources that can be recreated.
    }
}




Error & Solution : If you get a blank screen instead of the main view controller, and an error in the debug window stating "perhaps the designated entry point is not set?”, then it means there is no initial view controller for the story board. Select the storyboard->then the navigation controller or the view controller that you want as the first screen -> attributes inspector -> check the “Set as initial view controller” option. It should be enabled.

Code Jam for the week of Mar 1 2015

Wednesday, February 18, 2015

Code Jam : Swift : Working with Segues.

Today we are going to see about Segues. There is a sudden shift from particle effects to segues from my previous blog. That is because I was digging into the Swift book that I have, and found segues to be interesting. Hence shifting gears for this week. 

Segues can be considered as connection between two view controllers, which can be created as easy as click dragging between two view controllers. It seems like creating delegates to get information back might be more difficult. But one thing at a time. 

Note : Optionals are not considered boolean values. Hence if(optional1 != nill) will work and not if(optional1) . 

Step1: In storyboard, click on view controller. I expect things to be interesting, since this is not a new project, and I already have a mini module running. I wonder if the views will be overlay’ed on the existing game. 

Step2: Embed this view controller in a navigation controller. Navigation Controller is a controller that has a stack of view controllers from which they can be pushed and popped. It automatically takes care of bringing in the pushed view controller to display with animations, and taking the existing view controller out of display, with animation when it is popped out. 

Step3: Add a label from objects library (normally at the bottom right side of screen) . Navigation controller does not allow you to add a label to it, because it is the work of view controller. Hence, on the top of the view subwindow single click on Main.Storyboard Base and you would see “Game View Controller scene” and “Navigation Controller Scene”. Chose Game view controller scene and add label to it. Set its text to anything you want. Or if you scroll the view you can see a connection between the navigation controller scene and the view controller scene. Click on the view controller scene and add label to it. 

Step4. In similar way add a bar button to the view controller. 

Step5. Create a second view. File->new->File -> Cocoa touch class that subclasses UIViewController. Chose the language to be Swift. 
Go back to story board, and create a new view controller. Now control+ drag from the bar button to the new view controller, and chose : Action Segue - show. 
In the identity inspector, set the custom class to be view2 (class name of the second view). This step links the view controller in the story board with the new class. 

Step6. Now add label, navigation item, bar button item, and three buttons. Navigation item represents state of the navigation bar including the title. The bar button has to be added to the navigation bar. 


Step 7. Open Assistant editor in Xcode if it is not already there. This editor shows the code for the view controller on a side-by-side  basis, to help with faster and easier code implementation. Control+Click drag the label to the the first empty line in  class view2. This will bring a popup menu showing options to create an Outlet (Links between objects to enable communication between them) or an Outlet Collection (Simply an array of outlets). Object: specifies the view controller. It cannot be changed. Name: provide “textLabel” as name of the outlet, because we are going to change the text of the label with button click. Type: UILabel. This also cannot be changed, because you know, it is a label. Storage: Weak is the default option for outlets, except for those from File Owner’s to top level objects in nib file or in iOS a storyboard scene, which should be strong as per Apple Docs (https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6)

Normally strong outlets are used by framework classes like UIViewController’s view outlet or NSWindowController’s window outlet. They should be generally weak because normally they do not own the objects in subviews of view controller or windows’ controller. 

I am going to set the storage as strong because this outlet is in iOS storyboard scene, and from the docs it looks like the right thing to do. 

Click on “Connect”. This creates following line inside the class 
class GameViewController: UIViewController {
    @IBOutlet var TextLabel: UILabel!

Step8. Similarly control drag from the bar button to the view2 class, and create an action. 

Step9. Similarly command+select all three color buttons and control drag them to view2 class. Create an action for UIButton. Inside this color button action function, add the code to change the label of the text to that of the button title. 
labelOutlet.text = sender.titleLabel!.text!

Step10. In ViewDidLoad() function add this line to set the label’s text in the beginning. 
labelOutlet.text = colorString. 
Create a var colorString =“” as global member in the view2 class. 

Here is code snippet for view2 class:
class view3: UIViewController {
    @IBOutlet var labelOutlet: UILabel!
    var colorString = "new"
    @IBAction func greenAction(sender: UIButton, forEvent event: UIEvent) {
        labelOutlet.text = sender.titleLabel!.text!
    }
    @IBAction func bbuttonaction(sender: UIBarButtonItem) {
    }
    @IBAction func blueaction(sender: UIButton) {
        labelOutlet.text = sender.titleLabel!.text
    }
    @IBAction func redaction(sender: UIButton) {
        labelOutlet.text = sender.titleLabel!.text
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        labelOutlet.text = colorString;

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }
 }


Here is code from the first view:
class ViewController: UIViewController {

    @IBOutlet var colorLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

And here is how the story board looks like. 
It has default names. You can set the names of the labels to whatever you might like. 

Data via Segue:

Now that a segue connection has been made, data needs to be passed from one view to the other via the segue.

Step1. For this purpose, prepareForSegue has to be overridden. Start typing prepareForSegue in the first view class, and XCode automatically fills in the override keyword and function signature for you. 

Step2. Set the identifier name of the segue in attributes inspector as “mySegue"

Step3. Inside prepareForSegue write this line of code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "mySegue"{
            let vc = segue.destinationViewController as view3
            vc.colorString = colorLabel.text! // color label is the outlet for the label.
        }
    }

UIStoryboardSegue object is responsible for transitioning between view controllers. It also prepares the target view with data from the first view. 

When you hit run, text label from the first view is passed on to the second view when segue activates during navigation bar button press/touch by user. 



Code Jam for the week of Feb 9

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