Squirrels are cute and furry animals but they are no friend to cats and dogs. In this article we are going to implement a Angry Birds like physics enabled game called "Nutty Squirrels" using the SpaceManager framework.

Downloading the Art:

We are fortunate to have Vicki Wenderlich in the indie developer community who provides artwork for FREE. You can download the Squirrel artwork from Vicki's website using the link below:

Vicki Wenderlich

Setting Up Project for the Space Manager:

Space Manager is a wrapper on top of the chipmunk framework and makes it very easy to create physics enabled games. You must have Cocos2d installed in order to create projects that uses SpaceManager framework. You can download Cocos2d framework using the link below:

Cocos2d Official Website

After downloading the Cocos2d framework you must download the Space Manager framework using this link. In this article we are going to use the Space Manager 0.0.7 version. The version 1.0.0 has some issues with one of the methods that we will be utilizing in our project. After downloading the SpaceManager project create a new Cocos2d Chipmunk project. Copy the SpaceManager src folder into the root of your application. Finally, set the header paths to the Chipmunk folder as shown below:



Make sure to add the quotation marks since xCode behaves strangely without the quotation marks added. Build the project again and now it should not have any errors. In the next section we are going to start implementing the game level.

Implementing Game Level:

The first task in implementing the game level is to setup the background for the game. Inside the init method we have created a background for the game and added it to the layer.



We added the background to the z index -1 so that it does not interfere with the other sprites on the layer since by default everything is added at layer 0. The screenshot below shows the background in action.



After setting the background image we can initialize the SpaceManager object using the code below:



The above code also sets up the gravity and friction for the space. Next we need to add different sprites to the layer. These sprites includes cat, squirrel, acorn and blocks.



Don't be scared of the above code! It simply creates shapes and assigns the shapes to the sprites. The shape can be a rectangle, circle or a polygon. In this article we will be using rectangle and circle shapes.

Next, we can add all the cpCCSprite objects into the layer as shown in the code below:



Finally, we start the space manager by calling the start method on the space manager instance.



The effect is shown below in the screenshot:



In the next section we are going to demonstrate how the squirrel can throw nuts at the cat.

Throwing Acorn at the Cat:

Before doing anything make sure that the layer can receive the touch events. This can be accomplished using the following line of code inside the init method.



Next we will implement the ccTouchesBegin method which is invoked anything a user touches on the screen.



The following two lines are the most important lines in the above method.

   

The method morphShapeToActive changes the static mass of the acorn into a dynamic mass which can move and on which the gravitational forces can act. The applyImpulse method applies a shot gun type force on the sprite. The screenshot below shows the squirrel attacking the cat with an acorn.



At this point the acorn hits the cat but nothing happens. This is because we have not yet implemented collision detection. In the next section we are going to implement collision detection between the acorn and the cat.

NOTE: The morphShapeToAction method does not work in the Space Manager version 1.0.0. This is the reason we are using the older version.

Implementing Collision Detection:

Space Manager makes it very easy to implement collision detection. Add the following code inside the init method.



We have given integer values to the collision_type property which uniquely identifies the sprite. The handleCollisionBetweenAcornAndCat method is implemented below:



When an acorn collides with the cat the handleCollisionBetweenAcornAndCat will be triggered and the console will output "COLLISION".

You can get creative or inhumane and throw a smoked acorn at the cat. This can easily be achieved by using the particle effects.



The screenshot below shows the sick effect:



Conclusion:

In this article we learned how to use Space Manager framework to create physics enabled games. We also learned about collision detection and adding particle effects to the game. In the next article we are going to use the LevelHelper and SpriteHelper tools to create levels with much less effort.