(一)导入Box2DDebugDraw文件在自己cocos2d-x class文件夹下,
1)Box2DDebugDraw.h如下
#include "cocos2d.h"
#include "external/Box2D/Box2D.h"
class Box2DDebugDraw:public b2Draw
{
protected:
float32 _ratio;
cocos2d::DrawNode* _graphics;
public:
Box2DDebugDraw(cocos2d::DrawNode* graphics);
Box2DDebugDraw(cocos2d::DrawNode* graphics,float32 ratio);
/// Draw a closed polygon provided in CCW order.
virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
/// Draw a solid closed polygon provided in CCW order.
virtual void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);
/// Draw a circle.
virtual void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);
/// Draw a solid circle.
virtual void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);
/// Draw a line segment.
virtual void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);
/// Draw a transform. Choose your own length scale.
/// @param xf a transform.
virtual void DrawTransform(const b2Transform& xf);
};
2)Box2DDebugDraw.cpp如下
#include "Box2DDebugDraw.h"
USING_NS_CC;
Box2DDebugDraw::Box2DDebugDraw(cocos2d::DrawNode* graphics)
: _ratio( 1.0f ),_graphics(graphics)
{
}
Box2DDebugDraw::Box2DDebugDraw( cocos2d::DrawNode* graphics,float32 ratio)
: _ratio( ratio ),_graphics(graphics)
{
}
void Box2DDebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
//绘制多边形
Vec2* newVertices=new Vec2[vertexCount];
for( int i=0;i<vertexCount;i++)
{
newVertices[i].set(vertices[i].x, vertices[i].y);
newVertices[i]*=_ratio;
}
_graphics->drawPolygon(newVertices, vertexCount, Color4F(color.r,color.g,color.b,0.25f), 0, Color4F());
delete[] newVertices;
}
void Box2DDebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
//绘制带描边的多边形
Vec2* newVertices=new Vec2[vertexCount];
for( int i=0;i<vertexCount;i++)
{
newVertices[i].set(vertices[i].x, vertices[i].y);
newVertices[i]*=_ratio;
}
_graphics->drawPolygon(newVertices, vertexCount, Color4F(color.r*0.5f,color.g*0.5f,color.b*0.5f,0.5f), 1, Color4F(color.r,color.g,color.b,1.0f));
delete [] newVertices;
}
void Box2DDebugDraw::DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{
//绘制圆
Vec2 newCenter;
newCenter.set(center.x*_ratio, center.y*_ratio);
_graphics->drawSolidCircle(newCenter, radius*_ratio, 0, 16, Color4F(color.r,color.g,color.b,0.25f));
}
void Box2DDebugDraw::DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{
Vec2 newCenter;
newCenter.set(center.x*_ratio, center.y*_ratio);
//绘制圆
_graphics->drawSolidCircle(newCenter, radius*_ratio, 0, 16, Color4F(color.r*0.5f,color.g*0.5f,color.b*0.5f,0.5f));
//绘制边
_graphics->drawCircle(newCenter, radius*_ratio, 0, 16, false, Color4F(color.r,color.g,color.b,1.0f));
}
void Box2DDebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
//绘制线段
Vec2 newP1,newP2;
newP1.set(p1.x*_ratio, p1.y*_ratio);
newP2.set(p2.x*_ratio, p2.y*_ratio);
_graphics->drawSegment(newP1,newP2,1.0f,Color4F(color.r,color.g,color.b,1.0f));
}
void Box2DDebugDraw::DrawTransform(const b2Transform& xf)
{
//绘制坐标轴变换
Vec2 p1, p2;
p1.set(xf.p.x, xf.p.y);
const float32 scale = 0.4f;//轴的长度缩放
p2.set(xf.q.GetXAxis().x, xf.q.GetXAxis().y);
p2 = p1 + scale * p2;
_graphics->drawSegment(p1*_ratio,p2*_ratio,1,Color4F(1.0f,0,0,1.0f));
p2.set(xf.q.GetYAxis().x, xf.q.GetYAxis().y);
p2 = p1 + scale * p2;
_graphics->drawSegment(p1*_ratio,p2*_ratio,1,Color4F(0,1.0f,0,1.0f));
}
(2)HelloWorldScene需要添加如下
1)helloworldscene.h加入成员变量
//物理drawnode
DrawNode* _graphics;
2)helloworldscene.h加入成员变量
#include "Box2DDebugDraw.h"
auto debugdraw=new Box2DDebugDraw(_graphics,PTM_RATIO);
world->SetDebugDraw(debugdraw);
PTM_RATIO的定义如下: #define PTM_RATIO 32
PTM_RATIO用于定义32个像素在Box2D世界中等同于1米。一个有32像素宽和高的
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
// flags += b2Draw::e_jointBit;
// flags += b2Draw::e_aabbBit;
// flags += b2Draw::e_pairBit;
// flags += b2Draw::e_centerOfMassBit;
debugdraw->SetFlags(flags);
开启你想要draw的,我只需要shape,所以注释掉了其他的。
然后只需要在helloworldscene
void HelloWorldScene::update(float delta){
_graphics->clear();
world->DrawDebugData();
}
开启就能显示效果了。。记得自己开启侦听。。。
//开启移动跳跃 侦听
scheduleUpdate();
看看效果吧。。。。。省略box2d如何添加使用。。。