Chapter 3. Focusing on Physics

This chapter is for those of you who want to incorporate physics into your games. Whether you're building a mini golf game that uses realistic wall bounces or a platformer with endless gravity, this chapter is for you. It will have tutorials on the physics side of games, as well as show you how to use a physics engine without gravity.

In this chapter, we will cover the following topics:

  • How Chipmunk works
  • Setting up a project and creating basic objects
  • Setting gravity by tilting the device
  • Handling collisions in Chipmunk

Note

You must use Chipmunk for collision detection only (and not physics). Not all games need (or should even consider using) a physics engine. Sometimes, it's better to leave it out. However, if you feel your game will either be more polished or be produced faster, then by all means use it. That being said, the project in this book will not need a physics engine. So, instead of following the project here, we will create a small project that has many modular examples that can be adapted in your other projects. The book's main project will continue in the next chapter.

You may have been used to using the Box2D physics engine, but ever since version 3.0 of Cocos2d, there is no longer any support from the developers to make Box2D work out of the box as it did in previous versions. That being said, this chapter will focus on Chipmunk. If this chapter does not cover everything you need as far as physics is concerned, feel free to check out the documentation at http://chipmunk-physics.net/documentation.php. It also has a variety of online tutorials.

Learn how Chipmunk works

As previously mentioned, Chipmunk is the physics engine that's integrated with Cocos2d, and is the main physics engine from version 3.0 onwards. The good news for both newcomers and fans of Box2D alike is that Chipmunk is very simple to use. Let's get down to how Chipmunk basically works.

Overall structure of Chipmunk

Chipmunk is a physics engine in Cocos2d that simulates real-world physics, that is, making use of gravity, collisions, objects bouncing off each other, and so on.

Chipmunk uses a "bodies within a world" way of doing things. This means that, as shown in the following diagram, there's a physics simulation happening (called a world), and anything with physics applicable to it is a body. You just create a world that will simulate physics on the bodies within, and off you go. Each world you create will have its own gravity attached to it.

This is a simple representation of bodies within a world. Note that the entire green rectangle is the world, and the individual squares are bodies within it.

Overall structure of Chipmunk

Each body has a type (explained in the next section) as well as properties such as density, mass, friction, elasticity, velocity, and more. In Cocos2d, you can attach a physics body to a sprite with a single line of code, and the sprite will move around to the place where the body is located.

When these objects have their boundaries touching/intersecting one another, it's a collision. When a collision occurs, you can handle it however you wish.

Types of bodies

Chipmunk has three types of physics bodies that can be added to the world. They are static, dynamic, and kinematic:

  • Static bodies: These are the walls, ground, immovable rocks, and other objects in the game. They will not be affected by any gravity or other forces that try to interact with them.
  • Dynamic bodies: These are the default when creating a CCPhysicsBody object. These are the objects that will go flying around, colliding with other objects, and have forces act upon them.
  • Kinematic bodies: These are a sort of hybrid body type that cannot be influenced by forces or gravity, but can still be moved by CCActions and other methods.

In general, you will only be using static and dynamic bodies (and so is the case with this chapter). If you feel that you need more help with kinematic body types, check out the Cocos2d documentation about physics bodies at http://www.cocos2d-swift.org/docs/api/Constants/CCPhysicsBodyType.html.

Now that you know how Chipmunk works from a technical perspective, let's actually get down to the coding so that we can see these physics bodies for ourselves.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.200.71