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
SNOWBALL TOSS!
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 the Templates
XCODE TEMPLATES

• New   Project -> Cocos2D Templates
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
              CCLayer
                           CCScene
              Gameplay

                           Gameplay
CCSprite(s)                 Scene
              CCLayer
              Background
LAYERS AND TOUCH
Accelerometer

                   CCLayer   CCScene

                   CCLayer   Gameplay
                              Scene
                   CCLayer


   Touch
MULTIPLE SCENES
CCLayer     CCScene

CCLayer      Gameplay       CCScene
              Scene
CCLayer
                            Director
CCLayer     CCScene
              Level
CCLayer     Completed
              Scene
CCLayer
COCOS2D
                           ACTIONS
• Actionsare 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++

• 2Drigid 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!
CLOSING THOUGHTS

• Sample   Code != Production Code

• Cocos2d Website:

 http://www.cocos2d-iphone.org/

 Look at the sample tests included with Cocos2D!
THANK YOU


• 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

Similar to Creating physics game in 1 hour (20)

PDF
Demo creating-physics-game.
sagaroceanic11
 
PDF
Cocos2d 소개 - Korea Linux Forum 2014
Changwon National University
 
PPTX
Stefan stolniceanu spritekit, 2 d or not 2d
Codecamp Romania
 
PPTX
Stefan stolniceanu spritekit, 2 d or not 2d
Codecamp Romania
 
PDF
Under Cocos2D Tree @mdvecon 2013
Maxim Zaks
 
PDF
Under Cocos 2 D Tree_mdevcon 2013
Wooga
 
PDF
cocos2d for i Phoneの紹介
Jun-ichi Shinde
 
PDF
Introduction to CocosSharp
James Montemagno
 
PDF
2 d gameplaytutorial
unityshare
 
PPTX
Unity workshop
fsxflyer789Productio
 
PPTX
Bringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
Unity Technologies
 
PDF
Box2D and libGDX
Jussi Pohjolainen
 
PPTX
2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb
mochimedia
 
PDF
Basics cocos2d
sagaroceanic11
 
PDF
Tools for developing Android Games
Platty Soft
 
PDF
The not so short
AXM
 
PDF
CoreOS at Carnival
Toby Cox
 
PDF
CoreOS at Carnival
Toby Cox
 
KEY
CATiled Layer - Melbourne Cocoheads February 2012
Jesse Collis
 
KEY
Implementing CATiledLayer
Jesse Collis
 
Demo creating-physics-game.
sagaroceanic11
 
Cocos2d 소개 - Korea Linux Forum 2014
Changwon National University
 
Stefan stolniceanu spritekit, 2 d or not 2d
Codecamp Romania
 
Stefan stolniceanu spritekit, 2 d or not 2d
Codecamp Romania
 
Under Cocos2D Tree @mdvecon 2013
Maxim Zaks
 
Under Cocos 2 D Tree_mdevcon 2013
Wooga
 
cocos2d for i Phoneの紹介
Jun-ichi Shinde
 
Introduction to CocosSharp
James Montemagno
 
2 d gameplaytutorial
unityshare
 
Unity workshop
fsxflyer789Productio
 
Bringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
Unity Technologies
 
Box2D and libGDX
Jussi Pohjolainen
 
2 Dimensions Of Awesome: Advanced ActionScript For Platform Games by Iain Lobb
mochimedia
 
Basics cocos2d
sagaroceanic11
 
Tools for developing Android Games
Platty Soft
 
The not so short
AXM
 
CoreOS at Carnival
Toby Cox
 
CoreOS at Carnival
Toby Cox
 
CATiled Layer - Melbourne Cocoheads February 2012
Jesse Collis
 
Implementing CATiledLayer
Jesse Collis
 

Recently uploaded (20)

PDF
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PPTX
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PPTX
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
PDF
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
ScyllaDB
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
The Growing Value and Application of FME & GenAI
Safe Software
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
Database Benchmarking for Performance Masterclass: Session 1 - Benchmarking F...
ScyllaDB
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Kubernetes - Architecture & Components.pdf
geethak285
 
The Growing Value and Application of FME & GenAI
Safe Software
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
Ad

Creating physics game in 1 hour

  • 1. COCOS2D + BOX2D Creating physics game in 1 hour
  • 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!
  • 7. GETTING COCOS2D - PART 2 1. Clone the Git Repository 2. Install the Templates
  • 8. XCODE TEMPLATES • New Project -> Cocos2D Templates
  • 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 ...
  • 11. LAYERS AND SCENES CCLayer CCScene Gameplay Gameplay CCSprite(s) Scene CCLayer Background
  • 12. LAYERS AND TOUCH Accelerometer CCLayer CCScene CCLayer Gameplay Scene CCLayer Touch
  • 13. MULTIPLE SCENES CCLayer CCScene CCLayer Gameplay CCScene Scene CCLayer Director CCLayer CCScene Level CCLayer Completed Scene CCLayer
  • 14. COCOS2D ACTIONS • Actionsare 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++ • 2Drigid 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!
  • 16. A GAME IN 7 STEPS • Let’s begin
  • 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
  • 18. STEP 2 • init() • createPhysicsWorld • debug draw • ground body
  • 19. 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; }
  • 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);
  • 21. STEP 3 • Create the dynamic blocks
  • 22. CODE+DEMO Physics World + Dynamic Blocks
  • 25. STEP 5 • Time to add graphics!
  • 26. GRAPHICS DETAILS • Background Layer • Sprites for the static shapes • Sprites for the snowballs • Penguin animation • Instructions Layer
  • 27. STEP 6 Let it snow! - Fun with Particle Systems
  • 28. STEP 7 Pump up the volume!
  • 29. CLOSING THOUGHTS • Sample Code != Production Code • Cocos2d Website: http://www.cocos2d-iphone.org/ Look at the sample tests included with Cocos2D!
  • 30. THANK YOU • [email protected] twitter.com/rodstrougo • www.prop.gr • www.cocos2dbook.com
  • 31. EXTRAS • SpriteSheets • Creating Physics Models • Collision detection • Accelerometer Filters • Multi-touch handling
  • 32. HOW-TO PHYSICS MODELS • VertexHelper & Mekanimo http://github.com/jfahrenkrug/VertexHelper http://www.mekanimo.net/ • Ricardo’s LevelSVG http://www.sapusmedia.com/levelsvg/