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.
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"]; bg.position = ccp(240,160); [gameNode addChild:bg z:0]; //Add basketball [self addBasketball]; //Add basketball net [self addBasketballNet]; //Add shooter [self addShooter]; return self; } /* Add a basketball net with a sensor */ -(void) addBasketballNet { /* CODE OMITTED */ /...