Bullets and other fast moving objects are a fundamental part of many video games. In this recipe, we will see how to properly implement bullet physics.
Please refer to the project RecipeCollection02 for full working code of this recipe. Also note that some of the following code has been omitted for brevity.
@implementation Ch4_Bullets /* Fire the gun */ -(void) fireGun { //Fire 10 bullets per second if(fireCount > 0){ return; } fireCount = 0.2f; CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache]; //Fire bullet in the correct direction float gunAngle = -gunman.body->GetAngle() + PI/2; if(gunmanDirection == DPAD_LEFT){ gunAngle += PI; } CGPoint bulletVector = ccp( sin(gunAngle), cos(gunAngle) ); //Create bullet and shell casing for(int i=0; i<2; i++){ //Create bullet or casing object //NOTE: It might be more efficient to re-use a group of bullet objects...