Applying impulses

In Box2D, bodies can be moved around using impulses and forces . In this recipe, we will use impulses to accurately shoot a basketball into a basketball net.

Applying impulses

Getting ready

Please refer to the project RecipeCollection02 for full working code of this recipe.

How to do it...

Execute the following code:

enum { //Object type tags TYPE_OBJ_BASKETBALL = 0, TYPE_OBJ_SHOOTER = 1, TYPE_OBJ_NET_SENSOR = 2 }; @implementation Ch4_Impulses -(CCLayer*) runRecipe { [super runRecipe]; [message setString:@"Shoot the ball in the hoop."]; //Create level boundaries [self addLevelBoundaries]; //Add level background CCSprite *bg = [CCSprite spriteWithFile:@"bball_bg.png"]; ...

Get Cocos2d for iPhone 1 Game Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.