とりあえずチュートリアルを試してみたいと思います…
http://www.raywenderlich.com/28602/intro-to-box2d-with-cocos2d-2-x-tutorial-bouncing-balls
今は Cocos2d-x ver.2.2.2 を試しているので、create_project.py でとりあえずプロジェクトを作成。
TestCpp/Classes/Box2DTestBed にある GLES-Render.cpp/h をコピーしてくる。
Box2d の基本を記述。
HelloWorldScene.h
//HelloWorldScene.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
{
protected:
b2World * _world;
GLESDebugDraw * _debugDraw;
protected:
HelloWorld();
virtual ~HelloWorld();
public:
static cocos2d::CCScene * scene();
CREATE_FUNC(HelloWorld);
protected:
virtual bool init();
void initPhysic();
protected:
virtual void update(float delta);
virtual void draw();
};
#endif
HelloWorldScene.cpp
//HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
HelloWorld::HelloWorld()
{
}
HelloWorld::~HelloWorld()
{
}
CCScene * HelloWorld::scene()
{
CCScene * scene = CCScene::create();
HelloWorld * layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
bool result = false;
do
{
if ( !CCLayer::init() )
{
break;
}
this->initPhysic();
{
}
result = true;
} while (0);
return result;
}
void HelloWorld::initPhysic()
{
b2Vec2 gravity = b2Vec2(0.0f, -9.8f);
_world = new b2World(gravity);
_debugDraw = new GLESDebugDraw(PTM_RATIO);
_world->SetDebugDraw(_debugDraw);
{
uint32 flags = 0;
{
flags += b2Draw::e_shapeBit;
}
_debugDraw->SetFlags(flags);
}
}
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();
}
}
とりあえず準備まで…。^^;