Skip to content

Instantly share code, notes, and snippets.

@STAR-ZERO
Created August 20, 2014 02:09
Show Gist options
  • Save STAR-ZERO/f0d35665841292a7aab0 to your computer and use it in GitHub Desktop.
Save STAR-ZERO/f0d35665841292a7aab0 to your computer and use it in GitHub Desktop.
Cocos2d-xでBox2DのDebugDraw
#include "B2DebugDrawLayer.h"
USING_NS_CC;
B2DebugDrawLayer *B2DebugDrawLayer::create(b2World *pB2World, float pPtmRatio)
{
B2DebugDrawLayer *pRet = new B2DebugDrawLayer(pB2World, pPtmRatio);
if (pRet && pRet->init()) {
pRet->autorelease();
return pRet;
} else {
delete pRet;
pRet = NULL;
return NULL;
}
}
B2DebugDrawLayer::B2DebugDrawLayer(b2World *pB2World, float pPtmRatio)
: mB2World(pB2World), mPtmRatio(pPtmRatio)
{
}
bool B2DebugDrawLayer::init()
{
if (!Layer::init()) {
return false;
}
mB2DebugDraw = new GLESDebugDraw(mPtmRatio);
mB2World->SetDebugDraw(mB2DebugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
flags += b2Draw::e_jointBit;
flags += b2Draw::e_aabbBit;
flags += b2Draw::e_pairBit;
flags += b2Draw::e_centerOfMassBit;
mB2DebugDraw->SetFlags(flags);
return true;
}
void B2DebugDrawLayer::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)
{
Layer::draw(renderer, transform, flags);
GL::enableVertexAttribs(cocos2d::GL::VERTEX_ATTRIB_FLAG_POSITION);
Director *director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
_modelViewMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(B2DebugDrawLayer::onDraw, this);
renderer->addCommand(&_customCommand);
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
void B2DebugDrawLayer::onDraw()
{
Director *director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
Mat4 oldMV;
oldMV = director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewMV);
mB2World->DrawDebugData();
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, oldMV);
}
#ifndef __B2_DEBUG_DRAW_LAYER_H__
#define __B2_DEBUG_DRAW_LAYER_H__
#include "cocos2d.h"
#include "Box2D/Box2D.h"
#include "GLES-Render.h"
USING_NS_CC;
class B2DebugDrawLayer : public Layer
{
b2World *mB2World;
GLESDebugDraw *mB2DebugDraw;
const float mPtmRatio;
public:
B2DebugDrawLayer(b2World *pB2World, float pPtmRatio);
static B2DebugDrawLayer *create(b2World *pB2World, float pPtmRatio);
virtual bool init();
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags);
protected:
Mat4 _modelViewMV;
void onDraw();
CustomCommand _customCommand;
};
#endif // __B2_DEBUG_DRAW_LAYER_H__

Cocos2d-xでBox2DのDebugDraw

環境

Cocos2d-x 3.2

概要

物体のシェイプとかジョイントを表示してくれる

使い方

  1. このGistにあるB2DebugDrawLayer.hB2DebugDrawLayer.cppをプロジェクトへ配置
  2. Cocos2d-xのディレクトリのtests/cpp-tests/Classes/Box2DTestBedの中にあるGLES-Render.hGLES-Render.cppをプロジェクトへ配置
  3. 下記のようにb2World作成後にLayer追加(PTM_RATIOはBox2Dで使用する定数なので定義してあるはず)
    b2Vec2 gravity;
    gravity.Set(0.0f, -0.8f);
    world = new b2World(gravity);

    // DebugDraw
    addChild(B2DebugDrawLayer::create(world, PTM_RATIO), 9999);

オプション

B2DebugDrawLayer.cppの下記箇所をコメントアウトしたりすることでデバッグで表示されるものを制御できる

    uint32 flags = 0;
    flags += b2Draw::e_shapeBit;
    flags += b2Draw::e_jointBit;
    flags += b2Draw::e_aabbBit;
    flags += b2Draw::e_pairBit;
    flags += b2Draw::e_centerOfMassBit;
    mB2DebugDraw->SetFlags(flags);

参考

http://discuss.cocos2d-x.org/t/box2d-debug-drawing-for-cocos2d-x-3-0/11912

ここから落とせるものが少し古いのかオーバーライドする関数が違ったので修正した

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment