Book Image

Cocos2d Game Development Blueprints

By : Jorge Jordán
Book Image

Cocos2d Game Development Blueprints

By: Jorge Jordán

Overview of this book

Table of Contents (15 chapters)
Cocos2d Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Coding custom physics


In this section, you will learn how to code the custom physics that will be involved in the game, such as gravity and collisions.

As we all know, gravity is a force that pushes the objects (in this case, the Yeti) down until they collide with the floor or another object stops their fall.

To represent this programmatically, we need to decrease our main character's height position until it collides with some floor block or falls into an abyss.

Remember that we have set the gravity force value to be -5.0, so let's make this force influence the Yeti's position. To do this, we need to implement the update method in the following way:

- (void)update:(NSTimeInterval)delta {
    // Make the yeti fall
    [_yeti setPosition:CGPointMake(_yeti.yetiSprite.position.x, _yeti.yetiSprite.position.y + _gravity)];
}

As you can see, this method is pretty simple as we are just adding the gravity's value to the Yeti's height. If you want to check how this will affect the game, run the project...