Physics enabled games are currently among the top games being sold on the iTunes. The gravitational forces of nature pulls the users towards these mind blowing games. In this article we are going to demonstrate how to get started with the SpaceManager physics engine for Cocos2d iPhone application.

What is SpaceManager?

SpaceManager is a wrapper on top of the Chipmunk physics framework. Chipmunk uses C++ to create physics enabled applications. SpaceManager uses the default objective-c language to provide the same functionality in fewer lines of code.

Downloading SpaceManager:

The first step is to download the SpaceManager from Google code. You can use the following link to download the latest version of the SpaceManager framework.  

Download SpaceManager

Creating the Project:

We will create a Chipmunk application by using the Cocos2d template. If you have not installed Cocos2d template for xCode then you can use the following link to download and install the template. The template will allow you to quickly setup a Cocos2d application.

Cocos2d Templates

The Chipmunk application alone will not provide much assistance for the SpaceManager application and needs to add SpaceManager files to our project. The screenshot below shows which files and folders needs to be added to enable the SpaceManager framework in a Cocos2d application.

Make sure all the SpaceManager dependent files are added to your project or else it will not function properly. Run the application after adding all the files and you should see a character displayed on the screen. If you tap on the screen a duplicate character is added to the touched position. This is the default template for the Chipmunk application. To start from scratch remove all the code from the init method as shown below:


Now if you will run the app you will only see the black screen with FPS (Frames Per Second) information displayed on the lower left corner of the screen. In the next section we will add a ball to our empty space.

Adding Sprite to the Space:

An empty space is an empty space. In order to play with the gravitational forces of nature we need to add an element to our space. In this section we are going to add a sprite to our space and apply laws of nature to it. Before writing any code we need to make our sprite. Open your favourite drawing application and draw a ball. If you are bad at drawing you can always ask your better half.

The following implementation adds a ball in the space.

The first line initializes the SpaceManager. Next, we add attributes to our space. The elasticity attribute is the most important since it will determine how the sprite will behave in the space. The default value for elasticity is 1.0 which represents normal objects in space. The value of 0 makes the object fall like a rock and higher values makes the object rebound like a rubber ball.

After setting the space and its attributes we create a cpShape object which represents the shape of the sprite. Since, our sprite is a ball we will use addCircleAt method of the SpaceManager class. You can also use addRectAt and addPolyAt methods to create rectangles and polygons. Next, we create our sprite using the cpCCSprite class. The sprite is assigned a ball shape so it will behave like a ball. The sprite is then added to the layer using the [self addChild:node] method. Finally, we invoke the start method on the SpaceManager object to start the space.

Run the application and you will notice that the ball drops on the floor because of gravitational pull. You have just made your first physics enabled iPhone application. Give yourself a pad on the back!      

Adding Static Mass Object to the Space:

In the previous section we added a ball to our space. The ball abides by the laws of nature. The space might contains some elements on which the forces of nature do not apply. These elements are called STATIC MASS elements. Let's add a static mass element to our space.

The only difference between a regular element and a static mass element is the STATIC_MASS value applied to the mass attribute. If you run the application you will notice that the ball falls on top of the block. If you want to tilt the block then simply adjust the value for rotation. Keep in mind that the rotation value is in radians and not degrees.

Convert Degrees to Radians

Applying Force to the Ball:

Currently the only force applying to the ball is the gravitational pull. However, you can always apply different forces to our objects in space. We will apply a push force in our example. The ball will be pushed to the location touched by the user. First make sure that isTouchedEnabled is set to "YES" inside the init method as shown below:


Next, we will implement the ccTouchesBegin event which is fired each time the user touches the screen.

The ballSprite is declared in the header file as a property which is synthesize inside the implementation file for use. Run the application and touch the screen. The ball will be thrown at the touched position. You can also hit the ball on the static mass object "block" and see the interaction between the two objects.


In this article we learned how to add physics to our iPhone applications using SpaceManager library. In the next article we will discuss how to interact between different objects in space.

[Download Sample