Creating physics game in 1 hour
-
Upload
linkou-bian -
Category
Technology
-
view
2.618 -
download
2
Transcript of Creating physics game in 1 hour
![Page 1: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/1.jpg)
COCOS2D + BOX2DCreating physics game in 1 hour
![Page 2: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/2.jpg)
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
![Page 4: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/4.jpg)
SNOWBALL TOSS!
![Page 5: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/5.jpg)
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!
![Page 6: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/6.jpg)
GETTING COCOS2D
http://github.com/cocos2d/cocos2d-iphone
![Page 7: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/7.jpg)
GETTING COCOS2D - PART 2
1. Clone the Git Repository2. Install the Templates
![Page 8: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/8.jpg)
XCODE TEMPLATES
• New Project -> Cocos2D Templates
![Page 9: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/9.jpg)
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, ...
![Page 10: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/10.jpg)
COCOS2DESSENTIALS
• 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 ...
![Page 11: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/11.jpg)
LAYERS AND SCENES
CCSprite(s)
Background
CCLayer
GameplayScene
CCSceneGameplay
CCLayer
![Page 12: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/12.jpg)
LAYERS AND TOUCHAccelerometer
GameplayScene
CCLayer
CCScene
CCLayer
CCLayer
Touch
![Page 13: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/13.jpg)
MULTIPLE SCENES
GameplayScene
CCLayer
CCScene
CCLayer
CCLayer
Level Completed
SceneCCLayer
CCScene
CCLayer
CCLayerDirector
CCScene
![Page 14: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/14.jpg)
COCOS2DACTIONS
• 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
![Page 15: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/15.jpg)
COCOS2D+BOX2DESSENTIALS 2
• Box2D is C++
• 2D rigid physics simulation engine with continuous collision detection
• All your files that touch C++ or include it must be Objective-C++ (.mm)
• Tuned for 1 meter sized objects!
• Use a fixed time step!
![Page 16: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/16.jpg)
A GAME IN 7 STEPS
• Let’s begin
![Page 17: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/17.jpg)
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
![Page 18: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/18.jpg)
STEP 2
• init()
• createPhysicsWorld
• debug draw
• ground body
![Page 19: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/19.jpg)
INIT()-(id)init {
! if ((self=[super init])) {! ! CGSize screenSize = [CCDirector sharedDirector].winSize;! !! ! // enable touches! ! self.isTouchEnabled = YES;! ! // enable accelerometer! ! self.isAccelerometerEnabled = YES;! !! ! [self createPhysicsWorld];! !! ! [self addNewBodyWithCoords:ccp((screenSize.width/2)+80.0f, screenSize.height/2) ! ! ! ! ! withDimensions:ccp(1.0f,0.5f) ! ! ! ! ! ! andDensity:3.0f ! ! ! ! ! andWithSprite:ICE_BLOCK_FILENAME_1];! !! ! // Start the scheduler to call the tick function! ! [self schedule: @selector(tick:)];! }! return self;}
![Page 20: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/20.jpg)
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);
![Page 21: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/21.jpg)
STEP 3
• Create the dynamic blocks
![Page 22: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/22.jpg)
CODE+DEMOPhysics World + Dynamic Blocks
![Page 23: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/23.jpg)
STEP 4
• Touch Events
![Page 24: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/24.jpg)
CODE+DEMOTouch Events Demo
![Page 25: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/25.jpg)
STEP 5
• Time to add graphics!
![Page 26: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/26.jpg)
GRAPHICS DETAILS
• Background Layer
• Sprites for the static shapes
• Sprites for the snowballs
• Penguin animation
• Instructions Layer
![Page 27: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/27.jpg)
STEP 6
Let it snow! - Fun with Particle Systems
![Page 28: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/28.jpg)
STEP 7
Pump up the volume!
![Page 29: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/29.jpg)
CLOSING THOUGHTS
• Sample Code != Production Code
• Cocos2d Website:
http://www.cocos2d-iphone.org/
Look at the sample tests included with Cocos2D!
![Page 30: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/30.jpg)
THANK YOU
• twitter.com/rodstrougo
• www.prop.gr
• www.cocos2dbook.com
![Page 31: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/31.jpg)
EXTRAS
• SpriteSheets
• Creating Physics Models
• Collision detection
• Accelerometer Filters
• Multi-touch handling
![Page 32: Creating physics game in 1 hour](https://reader034.fdocuments.us/reader034/viewer/2022052522/554bd62fb4c905ac708b4f4d/html5/thumbnails/32.jpg)
HOW-TO PHYSICS MODELS
• VertexHelper & Mekanimo
http://github.com/jfahrenkrug/VertexHelper
http://www.mekanimo.net/
• Ricardo’s LevelSVG
http://www.sapusmedia.com/levelsvg/