Creating Physics Enabled Game Using Cocos2d and SpaceManager
Physics enabled games always gain the attention of users. The simplicity of nature mixed with the cutting edge technology and effects makes physics game more appealing and desirable. There are number of frameworks which allows the developers to implement physics enabled games. In this article we are going to demonstrate how to use SpaceManager framework along with Cocos2d framework to enable gravity in our application.
Scenario:
We are going to implement a game similar to Bubble Ball. The game will
not have the ability to move different pieces to allow the ball to reach
the final destination. Instead we will allow the user to drop the ball
anywhere on the screen and the ball will hit obstacles to reach the
final destination.
SpaceManager 0.6:
The first task is to download the SpaceManager library. The download link is given below:
The easiest way to get started with the SpaceManager library is to build
your application off the sample project. This technique will make sure
that all the necessary files are added to the project. Compile and build
the application and make sure that it runs.
Implementation:
By default the app runs with a black background. Let's change that and
add a nice background for the app. The background code is added in the
GameDelegate file which is contained in the Base Resource folder.
The next step is to add the ball and the obstructions to the scene.
Since, we will be tracking the ball collision with the final destination
which in this case is an image of the star we will declare the ball
sprite as a property. The GameLayer.h declares the following properties.
The next step is to setup and initialize the SpaceManager. The
SpaceManager framework is responsible for managing the physics
effects. It is a wrapper around the Chipmunk framework. The SpaceManager
is initialized inside the init method of the GameLayer as shown below:
The above code will initialize an empty space. Now, it is time to add
some objects to the space. The implementation below adds a ball,
obstructions and the star to the space.
The rectangleShape is marked with STATIC_MASS which means the gravity
will have no effect on this object. The ball on the other hand will
experience all the gravitational forces. Run the application and you
will notice that the ball immediately starts to fall down. The
screenshot is shown below:
Looks pretty neat right!
Our next step is to allow the user to drag and drop the ball anywhere on
the screen. Since this game is just a demo we are going to allow the user to access the ball by touching anywhere on the screen. If you remember
correctly the ball is not a STATIC_MASS object. This means when you
start dragging the ball it will also start falling towards the bottom. We
need someway to make the ball STATIC_MASS for the lifetime of the drag
operation. The ccTouchesBegin method to the rescue! The ccTouchesBegin
method is invoked when the user starts touching the device screen.
The morphShapeToStatic method of SpaceManager class changes the behavior
of the non static mass object to a static mass object. Finally, when
the ccTouchesEnded is triggered we need to make the ball to an
non-static shape. The implementation is shown below:
Unfortunately, it is not enough to implement ccTouchesBegin and
ccTouchesEnded method. We also need to implement ccTouchesMoved which is
fired when the user drags the object on the screen. The ccTouchesMoved
is implemented below:
Inside the ccTouchesMoved event we find the current location of the
touch event. The ballSprite is then moved to the current touch position.
Now, if you run the app you will notice that you can easily drag the
ball across the screen.
Implementing Collision Detection:
At this point if the ball hits the star nothing happens. This is because
we have not yet implemented the collision detection. There are
multiple ways of implementing collision detection. SpaceManager library
provides number of classes to work with collisions but we are going to
use our custom method for collision detection.
This method is contained inside the CollisionManager class. The usage is shown below:
If the collision is detected then we simply rotate the star and puts a
congratulations message on the screen. The video below shows the game in
action:
Conclusion:
Physic games are always popular in any app store. Cocos2d and
SpaceManager allows the developers to easily create appealing physics
enabled games.