cocos2d-x 3.x 开启debugdraw

()导入Box2DDebugDraw文件在自己cocos2d-x class文件夹下,


1Box2DDebugDraw.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);

};



2Box2DDebugDraw.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需要添加如下

1helloworldscene.h加入成员变量

    //物理drawnode

    DrawNode* _graphics;


2helloworldscene.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如何添加使用。。。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值