SlideShare ist ein Scribd-Unternehmen logo
1 von 31
Downloaden Sie, um offline zu lesen
cocos2dとBox2Dの何か
第8回 Sendai WorldWide Developer Conference - @tototti

Date 2012/08/25
自己紹介
自己紹介




✤   twitter : @tototti
✤   iOSとかjQueryで何かをカチャカチャ...ッターンと作っ
    てます。
宣伝
宣伝


✤   SocialBeer by AMBER RONDO
    ✤   仙台オクトーバーフェスト2012 対応版が出ます
        ✤   ビール図鑑に、13種追加されます

    ✤   2012年9月14日(金)∼23日(日)
        ✤   毎日行って、一日1.3杯飲めばコンプリート!
cocos2d

✤   ゲーム用のフレームワーク
    ✤   ゲーム以外にも使えるよ
    ✤   OpenGLを使いやすくしているよ
    ✤   名前の通り2次元だよ

✤   簡単に高速な描画を実現
    ✤   そう、cocos2dならね
Box2D


✤   物理エンジン
    ✤   ぶつかったり・跳ね返ったりをシミュレーション
    ✤   加速度、摩擦、衝突などを計算してくれるよ

✤   簡単に物理シミュレーションを実現
    ✤   そう、Box2Dならね
参考図書

✤   cocos2dで作る iPhone&iPadゲームプロ
    グラミング
    ✤   3,990円
その1・インストール
インストール


✤   cocos2d公式サイトより
    ✤   http://www.cocos2d-iphone.org/download
    ✤   ver1.0.1 をダウンロード
    ✤   install-templates.sh を実行して、プロジェクトテンプ
        レートもインストールしよう
新規プロジェクト作成
新規プロジェクト実行

✤   何やらそれっぽいものが動きます
その2:Box2D基礎
Box2D


✤   C++で記述されているライブラリ
    ✤   Objective-Cではないので汎用的
    ✤   iPhone以外でも使用可能
    ✤   拡張子を .mm にする必要あり
特徴

✤   縦横のスケールはピクセルではない
    ✤   なぜかメートル単位
    ✤   defaultでは 32pixel = 1m に定義されている

✤   物理演算機能のみ。描画は行わない。
    ✤   今回は、描画はcocos2dで行う
    ✤   言い換えれば、描画は任意のフレームワークで可能
フレームごとの処理

      1/60 sec毎の処理

    Box2Dで物体の座標を演算



     cocos2dで各物体を描画
その3:Box2D解析
templateファイル
2種類の物体


✤   静的ボディ
    ✤   座標が固定の物体。動かない物体。

✤   動的ボディ
    ✤   他の物理影響を受けて、動く物体。
物体の性質

✤   密度(density) = 重さ
    ✤   動的ボディの場合、落下速度などに影響

✤   摩擦(friction)
    ✤   表面を移動するときなどに影響

✤   反発(restitution)
    ✤   衝突した際の、跳ね返りなどに影響
b2Worldクラス

すべての物体を管理するコンテナ。The	 World。

//	 宣言
@interface	 HelloWorldLayer	 :	 CCLayer
{
	    b2World*	 world;
	    GLESDebugDraw	 *m_debugDraw;
}


//	 初期化
//	 gravity	 =	 重力
//	 doSleep	 =	 他と接していない物体の物理演算をする?フラグ
world	 =	 new	 b2World(gravity,	 doSleep);
ボディの形状(shape)


✤   b2PolygonShape
    ✤   SetAsEdge(x, y)
    ✤   SetAsBox(width, height)
    ✤   Set(array, array)

✤   b2CircleShape
4辺に静的ボディが配置されてる

//	 Define	 the	 ground	 body.
b2BodyDef	 groundBodyDef;
groundBodyDef.position.Set(0,	 0);	 //	 bottom-left	 corner
	     	 
b2Body*	 groundBody	 =	 world->CreateBody(&groundBodyDef);	  	 
b2PolygonShape	 groundBox;	  	 
	     	 
//	 bottom
groundBox.SetAsEdge(b2Vec2(0,0),	 b2Vec2(screenSize.width/
PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
	     	 
//	 top
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO),	 
b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
	     	 
//	 left
......
A,B,C,Dの箱は動的ボディ

	    //	 Define	 the	 dynamic	 body.
	    //Set	 up	 a	 1m	 squared	 box	 in	 the	 physics	 world
	    b2BodyDef	 bodyDef;
	    bodyDef.type	 =	 b2_dynamicBody;
	    bodyDef.position.Set(p.x/PTM_RATIO,	 p.y/PTM_RATIO);
	    bodyDef.userData	 =	 sprite;
	    b2Body	 *body	 =	 world->CreateBody(&bodyDef)
	     //	 Define	 another	 box	 shape	 for	 our	 dynamic	 body.
	     b2PolygonShape	 dynamicBox;
	     dynamicBox.SetAsBox(.5f,	 .5f);//These	 are	 mid	 points	 for	 our	 
1m	 box
	 
	     //	 Define	 the	 dynamic	 body	 fixture.
	     b2FixtureDef	 fixtureDef;
	     fixtureDef.shape	 =	 &dynamicBox;
	     fixtureDef.density	 =	 1.0f;
	     fixtureDef.friction	 =	 0.1f;
	     body->CreateFixture(&fixtureDef);
余談



✤   #define RTM_RATIO
    ✤   ディフォルトでは32 だが、 32.0 にした方がいい
    ✤   割り算すると、小数点が消えてしまうため
4辺を若干狭くしてみる

底も傾けて、より物理っぽく。
utility関数

//	 ピクセル単位の座標値を、メートル単位の座標ベクトルに変換
-(b2Vec2)	 toMeterVectorX:(float)x	 Y:(float)y	 {
	 	 	 	 return	 b2Vec2(x	 /	 PTM_RATIO,	 	 y	 /	 PTM_RATIO);
}
4辺の静的オブジェクト座標

//	 bottom
groundBox.SetAsEdge([self	 toMeterVectorX:10	 Y:10],
	 	 	 	 	 	 	 	 	 	 	 	 [self	 toMeterVectorX:(screenSize.width	 -	 10)	 Y:50]);
groundBody->CreateFixture(&groundBox,0);
//	 top
groundBox.SetAsEdge([self	 toMeterVectorX:10	 
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 Y:(screenSize.height-10)],
	 	 	 	 	 	 	 	 	 	 	 	 	 [self	 toMeterVectorX:(screenSize.width	 -	 10)	 
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 Y:(screenSize.height-10)]);
groundBody->CreateFixture(&groundBox,0);
//	 left
groundBox.SetAsEdge([self	 toMeterVectorX:10	 
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 Y:(screenSize.height-10)],
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 [self	 toMeterVectorX:10	 Y:10]);
groundBody->CreateFixture(&groundBox,0);
//	 right
groundBox.SetAsEdge([self	 toMeterVectorX:(screenSize.width-10)	 Y:
(screenSize.height-10)],
円形Objectを転がしてみる
円形Objectを転がしてみる

//	 Define	 another	 box	 shape	 for	 our	 dynamic	 body.
b2CircleShape	 circle;
circle.m_radius	 =	 0.5;

//	 Define	 the	 dynamic	 body	 fixture.
b2FixtureDef	 fixtureDef;
fixtureDef.shape	 =	 &circle;
fixtureDef.density	 =	 1.0f;
fixtureDef.friction	 =	 0.3f;
body->CreateFixture(&fixtureDef);
おしまい

Weitere ähnliche Inhalte

Empfohlen

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Empfohlen (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

SWWDC 20120825 [cocos2d and Box2D]

  • 1. cocos2dとBox2Dの何か 第8回 Sendai WorldWide Developer Conference - @tototti Date 2012/08/25
  • 3. 自己紹介 ✤ twitter : @tototti ✤ iOSとかjQueryで何かをカチャカチャ...ッターンと作っ てます。
  • 5. 宣伝 ✤ SocialBeer by AMBER RONDO ✤ 仙台オクトーバーフェスト2012 対応版が出ます ✤ ビール図鑑に、13種追加されます ✤ 2012年9月14日(金)∼23日(日) ✤ 毎日行って、一日1.3杯飲めばコンプリート!
  • 6. cocos2d ✤ ゲーム用のフレームワーク ✤ ゲーム以外にも使えるよ ✤ OpenGLを使いやすくしているよ ✤ 名前の通り2次元だよ ✤ 簡単に高速な描画を実現 ✤ そう、cocos2dならね
  • 7. Box2D ✤ 物理エンジン ✤ ぶつかったり・跳ね返ったりをシミュレーション ✤ 加速度、摩擦、衝突などを計算してくれるよ ✤ 簡単に物理シミュレーションを実現 ✤ そう、Box2Dならね
  • 8. 参考図書 ✤ cocos2dで作る iPhone&iPadゲームプロ グラミング ✤ 3,990円
  • 10. インストール ✤ cocos2d公式サイトより ✤ http://www.cocos2d-iphone.org/download ✤ ver1.0.1 をダウンロード ✤ install-templates.sh を実行して、プロジェクトテンプ レートもインストールしよう
  • 12. 新規プロジェクト実行 ✤ 何やらそれっぽいものが動きます
  • 14. Box2D ✤ C++で記述されているライブラリ ✤ Objective-Cではないので汎用的 ✤ iPhone以外でも使用可能 ✤ 拡張子を .mm にする必要あり
  • 15. 特徴 ✤ 縦横のスケールはピクセルではない ✤ なぜかメートル単位 ✤ defaultでは 32pixel = 1m に定義されている ✤ 物理演算機能のみ。描画は行わない。 ✤ 今回は、描画はcocos2dで行う ✤ 言い換えれば、描画は任意のフレームワークで可能
  • 16. フレームごとの処理 1/60 sec毎の処理 Box2Dで物体の座標を演算 cocos2dで各物体を描画
  • 19. 2種類の物体 ✤ 静的ボディ ✤ 座標が固定の物体。動かない物体。 ✤ 動的ボディ ✤ 他の物理影響を受けて、動く物体。
  • 20. 物体の性質 ✤ 密度(density) = 重さ ✤ 動的ボディの場合、落下速度などに影響 ✤ 摩擦(friction) ✤ 表面を移動するときなどに影響 ✤ 反発(restitution) ✤ 衝突した際の、跳ね返りなどに影響
  • 21. b2Worldクラス すべての物体を管理するコンテナ。The World。 // 宣言 @interface HelloWorldLayer : CCLayer { b2World* world; GLESDebugDraw *m_debugDraw; } // 初期化 // gravity = 重力 // doSleep = 他と接していない物体の物理演算をする?フラグ world = new b2World(gravity, doSleep);
  • 22. ボディの形状(shape) ✤ b2PolygonShape ✤ SetAsEdge(x, y) ✤ SetAsBox(width, height) ✤ Set(array, array) ✤ b2CircleShape
  • 23. 4辺に静的ボディが配置されてる // Define the ground body. b2BodyDef groundBodyDef; groundBodyDef.position.Set(0, 0); // bottom-left corner b2Body* groundBody = world->CreateBody(&groundBodyDef); b2PolygonShape groundBox; // bottom groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/ PTM_RATIO,0)); groundBody->CreateFixture(&groundBox,0); // top groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO)); groundBody->CreateFixture(&groundBox,0); // left ......
  • 24. A,B,C,Dの箱は動的ボディ // Define the dynamic body. //Set up a 1m squared box in the physics world b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO); bodyDef.userData = sprite; b2Body *body = world->CreateBody(&bodyDef) // Define another box shape for our dynamic body. b2PolygonShape dynamicBox; dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box // Define the dynamic body fixture. b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 1.0f; fixtureDef.friction = 0.1f; body->CreateFixture(&fixtureDef);
  • 25. 余談 ✤ #define RTM_RATIO ✤ ディフォルトでは32 だが、 32.0 にした方がいい ✤ 割り算すると、小数点が消えてしまうため
  • 28. 4辺の静的オブジェクト座標 // bottom groundBox.SetAsEdge([self toMeterVectorX:10 Y:10], [self toMeterVectorX:(screenSize.width - 10) Y:50]); groundBody->CreateFixture(&groundBox,0); // top groundBox.SetAsEdge([self toMeterVectorX:10 Y:(screenSize.height-10)], [self toMeterVectorX:(screenSize.width - 10) Y:(screenSize.height-10)]); groundBody->CreateFixture(&groundBox,0); // left groundBox.SetAsEdge([self toMeterVectorX:10 Y:(screenSize.height-10)], [self toMeterVectorX:10 Y:10]); groundBody->CreateFixture(&groundBox,0); // right groundBox.SetAsEdge([self toMeterVectorX:(screenSize.width-10) Y: (screenSize.height-10)],
  • 30. 円形Objectを転がしてみる // Define another box shape for our dynamic body. b2CircleShape circle; circle.m_radius = 0.5; // Define the dynamic body fixture. b2FixtureDef fixtureDef; fixtureDef.shape = &circle; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef);

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n