SlideShare a Scribd company logo
COCOS2D + BOX2D
Creating physics game in 1 hour
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
In Progress...
www.cocos2dbook.com
SNOWBALLTOSS!
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!
GETTING COCOS2D
http://github.com/cocos2d/cocos2d-iphone
GETTING COCOS2D - PART 2
1. Clone the Git Repository
2. Install theTemplates
XCODETEMPLATES
• New Project -> Cocos2DTemplates
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, ...
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 ...
LAYERS AND SCENES
CCSprite(s)
Background
CCLayer
Gameplay
Scene
CCScene
Gameplay
CCLayer
LAYERS ANDTOUCH
Accelerometer
Gameplay
Scene
CCLayer
CCScene
CCLayer
CCLayer
Touch
MULTIPLE SCENES
Gameplay
Scene
CCLayer
CCScene
CCLayer
CCLayer
Level
Completed
Scene
CCLayer
CCScene
CCLayer
CCLayer
Director
CCScene
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
COCOS2D+BOX2D
ESSENTIALS 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!
A GAME IN 7 STEPS
• Let’s begin
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
STEP 2
• init()
• createPhysicsWorld
• debug draw
• ground body
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;
}
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);
STEP 3
• Create the dynamic blocks
CODE+DEMO
Physics World + Dynamic Blocks
STEP 4
• Touch Events
CODE+DEMO
Touch Events Demo
STEP 5
• Time to add graphics!
GRAPHICS DETAILS
• Background Layer
• Sprites for the static shapes
• Sprites for the snowballs
• Penguin animation
• Instructions Layer
STEP 6
Let it snow! - Fun with
Particle Systems
STEP 7
Pump up the volume!
CLOSINGTHOUGHTS
• Sample Code != Production Code
• Cocos2d Website:
http://www.cocos2d-iphone.org/
Look at the sample tests included with Cocos2D!
THANKYOU
• rod@prop.gr
• twitter.com/rodstrougo
• www.prop.gr
• www.cocos2dbook.com
EXTRAS
• SpriteSheets
• Creating Physics Models
• Collision detection
• Accelerometer Filters
• Multi-touch handling
HOW-TO PHYSICS MODELS
• VertexHelper & Mekanimo
http://github.com/jfahrenkrug/VertexHelper
http://www.mekanimo.net/
• Ricardo’s LevelSVG
http://www.sapusmedia.com/levelsvg/

More Related Content

PDF
Creating physics game in 1 hour
PDF
iOS 2D Gamedev @ CocoaHeads
PPTX
Iphone game developement - In a game things always move
PPTX
Unity project
PDF
JARO Office Furniture
PDF
What every developer should know about database scalability, PyCon 2010
KEY
HTML5 is the Future of Mobile, PhoneGap Takes You There Today
PDF
cocos2d for i Phoneの紹介
Creating physics game in 1 hour
iOS 2D Gamedev @ CocoaHeads
Iphone game developement - In a game things always move
Unity project
JARO Office Furniture
What every developer should know about database scalability, PyCon 2010
HTML5 is the Future of Mobile, PhoneGap Takes You There Today
cocos2d for i Phoneの紹介

Similar to Demo creating-physics-game. (20)

PDF
Cocos2d programming
KEY
Chipmunk physics presentation
PDF
Cocos2d 소개 - Korea Linux Forum 2014
KEY
cocos2d 事例編 HungryMasterの実装から
PPTX
Game development with Cocos2d
PPTX
PPTX
iOS Gaming with Cocos2d
PDF
Introduction to Cocos2d
PDF
Introduction to-cocos2d
PDF
Cocos2d game programming 2
KEY
Cocos2d実践編 1.0.0rc
PDF
Basics cocos2d
PPTX
Box2D with SIMD in JavaScript
KEY
Cocos2dを使ったゲーム作成の事例
PDF
Game development with Cocos2d-x Engine
PDF
Box2D and libGDX
PPTX
Webelinx - iOS development: earn 100k daily with 1h of coding #tnt3
PPTX
Cocos2d for beginners
PPTX
Unity workshop
PDF
Introduction to Box2D Physics Engine
Cocos2d programming
Chipmunk physics presentation
Cocos2d 소개 - Korea Linux Forum 2014
cocos2d 事例編 HungryMasterの実装から
Game development with Cocos2d
iOS Gaming with Cocos2d
Introduction to Cocos2d
Introduction to-cocos2d
Cocos2d game programming 2
Cocos2d実践編 1.0.0rc
Basics cocos2d
Box2D with SIMD in JavaScript
Cocos2dを使ったゲーム作成の事例
Game development with Cocos2d-x Engine
Box2D and libGDX
Webelinx - iOS development: earn 100k daily with 1h of coding #tnt3
Cocos2d for beginners
Unity workshop
Introduction to Box2D Physics Engine
Ad

More from sagaroceanic11 (20)

PPTX
Module 21 investigative reports
PPTX
Module 20 mobile forensics
PPTX
Module 19 tracking emails and investigating email crimes
PPTX
Module 18 investigating web attacks
PPTX
Module 17 investigating wireless attacks
PPTX
Module 04 digital evidence
PPTX
Module 03 searching and seizing computers
PPTX
Module 01 computer forensics in todays world
PPT
Virtualisation with v mware
PPT
Virtualisation overview
PPT
Virtualisation basics
PPT
Introduction to virtualisation
PPT
6 service operation
PPT
5 service transition
PPT
4 service design
PPT
3 service strategy
PPT
2 the service lifecycle
PPT
1 introduction to itil v[1].3
PPTX
Visual studio 2008 overview
PPT
Vb introduction.
Module 21 investigative reports
Module 20 mobile forensics
Module 19 tracking emails and investigating email crimes
Module 18 investigating web attacks
Module 17 investigating wireless attacks
Module 04 digital evidence
Module 03 searching and seizing computers
Module 01 computer forensics in todays world
Virtualisation with v mware
Virtualisation overview
Virtualisation basics
Introduction to virtualisation
6 service operation
5 service transition
4 service design
3 service strategy
2 the service lifecycle
1 introduction to itil v[1].3
Visual studio 2008 overview
Vb introduction.
Ad

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation theory and applications.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Cloud computing and distributed systems.
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
cuic standard and advanced reporting.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Approach and Philosophy of On baking technology
Chapter 3 Spatial Domain Image Processing.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Encapsulation theory and applications.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Unlocking AI with Model Context Protocol (MCP)
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The Rise and Fall of 3GPP – Time for a Sabbatical?
Cloud computing and distributed systems.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Advanced methodologies resolving dimensionality complications for autism neur...
cuic standard and advanced reporting.pdf
Big Data Technologies - Introduction.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
MIND Revenue Release Quarter 2 2025 Press Release
Approach and Philosophy of On baking technology

Demo creating-physics-game.