This document provides an overview of how to create a physics-based game in 1 hour using Cocos2D and Box2D. It outlines a 7 step process: 1) initialize the director and create the physics world, 2) add dynamic blocks, 3) handle touch events, 4) add graphics, 5) add particles for effects, 6) add sound, and 7) notes it is a sample and not production code. Cocos2D provides scene management, textures, audio and rendering while Box2D adds 2D physics simulation.
2. WHO IS ROD?
? Founder of Prop Group
www.prop.gr
? Background in enterprise
software, now iPhone+iPad
games!
? 2D physics game, Payload in
the AppStore
5. WHY COCOS2D
? Games are fun! Making a game does not have to be hard.
? Write less infrastructure code, spend more time on design
and core gameplay
? OpenGL ES rendering and performance without having to
learn OpenGL ES to get started
? It is free!
9. COCOS2D
? Objective-C framework for games
? Scene Management,Textures,Audio
? Everything but the kitchen sink (3D stuff)*
? OpenGL ES rendering and optimizations,
Actions,Tile Maps, Parallax Scrolling, Scheduler,
High Score service, ...
10. COCOS2D
ESSENTIALS
? Your game is divided into scenes, scenes into
layers
? Layers have what you care about, the Sprites
? Director is used to switch between scenes
? Everything uses the CC namespace, so layers
are CCLayers, CCScenes, CCSprites ...
14. COCOS2D
ACTIONS
? Actions are an easy way to apply transitions, effects, and
animations to your sprites
? MoveTo, MoveBy, ScaleBy, ScaleTo, FadeIn, FadeOut ...
CCAction *moveAction = [CCMoveBy actionWithDuration:2.0f
position:CGPointMake(50.0f,0.0f)];
[playerSprite runAction:moveAction];
2 seconds
15. COCOS2D+BOX2D
ESSENTIALS 2
? Box2D is C++
? 2D rigid physics simulation engine with
continuous collision detection
? All your ?les that touch C++ or include it
must be Objective-C++ (.mm)
? Tuned for 1 meter sized objects!
? Use a ?xed time step!
17. STEP 1
? Attach the director to the AppDelegate
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setUserInteractionEnabled:YES];!// cocos2d will inherit these values
[window setMultipleTouchEnabled:YES]; // cocos2d will inherit these values
// create an openGL view inside a window
[[CCDirector sharedDirector] attachInView:window];!
[window makeKeyAndVisible];! !
? Director Options
20. CREATEPHYSICSWORLD()
// Define the gravity vector.
! b2Vec2 gravity;
! gravity.Set(0.0f, -10.0f);
!
! // Do we want to let bodies sleep?
! // This will speed up the physics simulation
! bool doSleep = true;
!
! // Construct a world object, which will hold and simulate the rigid bodies.
! world = new b2World(gravity, doSleep);
!
! world->SetContinuousPhysics(true);
b2BodyDef groundBodyDef;
! groundBodyDef.position.Set(0, 0); // bottom-left corner
!
! // Call the body factory which allocates memory for the ground body
! // from a pool and creates the ground box shape (also from a pool).
! // The body is also added to the world.
! b2Body* groundBody = world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;! !
!
! // bottom
! groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
! groundBody->CreateFixture(&groundBox);