Box2d チュートリアル2個目。
今度こそは1週間以内をノルマでやってみよう…と ^^;
How To Create A Breakout Game with Box2D and Cocos2D 2.X Tutorial
とりあえず、枠とボールを作るところまでを準備 ^^;
//HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Box2D.h"
#include "GLES-Render.h"
#define PTM_RATIO (32.0f)
class HelloWorld : public cocos2d::CCLayer
{
public:
static cocos2d::CCScene* scene();
CREATE_FUNC(HelloWorld);
protected:
virtual bool init();
protected:
HelloWorld();
virtual ~HelloWorld();
protected:
b2World * _world;
GLESDebugDraw * _debugDraw;
b2Body * _groundBody;
b2Fixture * _bottomFixture;
b2Fixture * _ballFixture;
protected:
void initPhysics();
protected:
void update(float delta);
void draw();
};
#endif
//HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
#pragma mark -
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
bool result = false;
do {
const CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
const CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
const CCPoint center = ccpMult(ccpFromSize(visibleSize), 0.5f);
this->initPhysics();
//枠
{
{
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set(0, 0);
_groundBody = _world->CreateBody(&bodyDef);
}
if (_groundBody)
{
b2EdgeShape shape;
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
//地面
shape.Set(b2Vec2(0, 0),
b2Vec2(visibleSize.width / PTM_RATIO, 0.0f));
_bottomFixture = _groundBody->CreateFixture(&fixtureDef);
shape.Set(b2Vec2(0, 0),
b2Vec2(0, visibleSize.height / PTM_RATIO));
_groundBody->CreateFixture(&fixtureDef);
shape.Set(b2Vec2(visibleSize.width, visibleSize.height),
b2Vec2(0.0f, visibleSize.height));
_groundBody->CreateFixture(&fixtureDef);
shape.Set(b2Vec2(visibleSize.width, visibleSize.height),
b2Vec2(visibleSize.width, 0.0f));
_groundBody->CreateFixture(&fixtureDef);
}
}
//ボール
{
b2Body * body = NULL;
{
CCPoint pos = center;
CCSprite * sprite = NULL;
{
sprite = CCSprite::create("ball.png");
sprite->setPosition(pos);
const int tagId = 1;
this->addChild(sprite, tagId, tagId);
}
{
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.userData = sprite;
bodyDef.position.Set(pos.x / PTM_RATIO, pos.y / PTM_RATIO);
body = _world->CreateBody(&bodyDef);
}
{
b2CircleShape shape;
shape.m_radius = (sprite->boundingBox().size.width * 0.5f / PTM_RATIO);
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 1.0f;
_ballFixture = body->CreateFixture(&fixtureDef);
}
}
}
this->scheduleUpdate();
result = true;
} while (0);
return result;
}
#pragma mark -
HelloWorld::HelloWorld()
{
_world = NULL;
_debugDraw = NULL;
}
HelloWorld::~HelloWorld()
{
CC_SAFE_DELETE(_debugDraw);
_debugDraw = NULL;
CC_SAFE_DELETE(_world);
_world = NULL;
}
#pragma mark -
void HelloWorld::initPhysics()
{
{
b2Vec2 gravity = b2Vec2(0.0f, -9.8f);
_world = new b2World(gravity);
_debugDraw = new GLESDebugDraw(PTM_RATIO);
{
uint32 flags = 0;
{
flags += b2Draw::e_shapeBit;
}
_debugDraw->SetFlags(flags);
}
_world->SetDebugDraw(_debugDraw);
}
}
#pragma mark -
void HelloWorld::update(float delta)
{
float32 timeStep = (1.0f / 60.0f);
int32 velocityIterations = 10;
int32 positionIterations = 10;
_world->Step(timeStep, velocityIterations, positionIterations);
}
void HelloWorld::draw()
{
CCLayer::draw();
//デバッグのための処理...
{
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
_world->DrawDebugData();
kmGLPopMatrix();
}
}
とりあえずこんな感じからスタート ^^;