【cocos2dx】box2d使用

来源:互联网 发布:根据ip查询域名 编辑:程序博客网 时间:2024/06/06 18:09

刚开始xcode莫名其妙的报错误,提示box2d没有开启,主要有两个地方需要设置,在targets->buidseting->preprocessing中设置CC_ENABLE_BOX2D_INTEGRATION=1删除climuk,project->buidseting->preprocessing同样但是对于使用,a的同志们来说还是找不到,那就必须从新生成cocos2dlib.a文件,生成之前一定设置以上地方的CC_ENABLE_BOX2D_INTEGRATION=1删除climuk后在生成,然后换上项目中的.a就没问题了 

Leave2.h


#ifndef Leave2_hpp#define Leave2_hpp#include "GLES-Render.h"#include <cstdlib>#include <stdio.h>#include "ShyRole.h"#include "Roulette.h"#include "cocos2d.h"#include "VisibleRect.h"#include <Box2D/Box2D.h>using namespace cocos2d;class Leave2 : public Scene{public:    Leave2();    ~Leave2();    virtual void draw(cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t flags) override;    virtual bool init() override;    CREATE_FUNC(Leave2);private:    b2World* world;    void update(float dt) override;    GLESDebugDraw m_debugDraw; };#endif /* Leave2_hpp */


Leave2.cpp


#define PTM_RATIO 32#include "Leave2.h"#include <Box2D/Box2D.h>#include "extensions/cocos-ext.h"#include "renderer/CCRenderer.h"#include "renderer/CCCustomCommand.h"Leave2::Leave2(){    init();}Leave2::~Leave2(){    CC_SAFE_DELETE(world);}bool Leave2::init(){    if(!Scene::init()){        return false;    }    //重力参数    b2Vec2 gravity;    gravity.Set(0.0f, -10.0f);        //物理世界    world = new b2World(gravity);    //是否可以休眠    world->SetAllowSleeping(true);    //开启连续物理测试(避免物体速度快物理穿透)    world->SetContinuousPhysics(true);        //设施debug模式(绘制刚体)    GLESDebugDraw* m_debugDraw = new GLESDebugDraw(PTM_RATIO);    world->SetDebugDraw(m_debugDraw);    uint32 flags = 0;    flags += b2Draw::e_shapeBit; //有很多种绘制方式 可以查看源代码    m_debugDraw->SetFlags(flags);        //定义刚体属性(位置类型等b2BodyDef需要添加到b2Body上)    b2BodyDef groundBodyDef;    groundBodyDef.position.Set(0, 0);    //刚体身体    b2Body* groundBody = world->CreateBody(&groundBodyDef);    //定义一个有边的形状    b2EdgeShape groundBox;        groundBox.Set(b2Vec2(VisibleRect::leftBottom().x/PTM_RATIO,VisibleRect::leftBottom().y/PTM_RATIO), b2Vec2(VisibleRect::rightBottom().x/PTM_RATIO,VisibleRect::rightBottom().y/PTM_RATIO));    groundBody->CreateFixture(&groundBox,0);        groundBox.Set(b2Vec2(VisibleRect::leftTop().x/PTM_RATIO,VisibleRect::leftTop().y/PTM_RATIO), b2Vec2(VisibleRect::rightTop().x/PTM_RATIO,VisibleRect::rightTop().y/PTM_RATIO));    groundBody->CreateFixture(&groundBox,0);     groundBox.Set(b2Vec2(VisibleRect::leftTop().x/PTM_RATIO,VisibleRect::leftTop().y/PTM_RATIO), b2Vec2(VisibleRect::leftBottom().x/PTM_RATIO,VisibleRect::leftBottom().y/PTM_RATIO));    groundBody->CreateFixture(&groundBox,0);        groundBox.Set(b2Vec2(VisibleRect::rightBottom().x/PTM_RATIO,VisibleRect::rightBottom().y/PTM_RATIO), b2Vec2(VisibleRect::rightTop().x/PTM_RATIO,VisibleRect::rightTop().y/PTM_RATIO));    groundBody->CreateFixture(&groundBox,0);          b2BodyDef bodyDef;    bodyDef.type = b2_dynamicBody;    b2Body *body;    body = world->CreateBody(&bodyDef);        //碰撞体定义(和b2BodyDef类似,主要定义密度,摩擦系数等物理属性)    b2FixtureDef fixtureDef;    //设置密度    fixtureDef.density = 1.0f;    //设置摩擦系数    fixtureDef.friction = 0.3f;        //设置碰撞体的形状(b2..Shape可以直接绑定到bebody上也可以设置b2FixtureDef后在绑定到b2body上)    b2PolygonShape dynamicBox;    dynamicBox.SetAsBox(0.5 , 0.5);    fixtureDef.shape = &dynamicBox;    body->CreateFixture(&fixtureDef);        //创建物理引擎精灵对象    cocos2d::Texture2D * spriteTexture = Director::getInstance()->getTextureCache()->addImage("CloseNormal.png");    auto sprite = cocos2d::extension::PhysicsSprite::createWithTexture(spriteTexture);    dynamicBox.SetAsBox(90/PTM_RATIO , 90/PTM_RATIO );    this->addChild(sprite);    sprite->setB2Body(body);    sprite->setPTMRatio(PTM_RATIO);     sprite->setPosition(Vec2(100,100));        scheduleUpdate();        return true;}void Leave2::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags){    Scene::draw(renderer, transform, _transformUpdated);    world->DrawDebugData();} void Leave2::update(float dt){    int velocityIterations = 8;    int positionIterations = 1;    world->Step(dt, velocityIterations, positionIterations);}


()Shape有三种

1 b2CircleShape定义圆

2 b2PolygonShape定义多边形

3 b2EdgeShape 定义边界

()设置debug模式就是能看到设置的刚体外围边界绘制方式有5种不设置

  1. e_shapeBit  有淡淡的颜色表示碰撞体
  2. e_jointBit  碰撞边框
  3. e_aabbBit  碰撞变宽+图形边框
  4. e_pairBit   碰撞变宽+图形边框+中心
  5.  e_centerOfMassBit
  6. 6m_drawFlags默认什么都不显示

(三)关于debug加入以下方法,cocos2dx 带的例子太乱网上找的也过时

draw(Renderer *renderer,constMat4 &transform,uint32_t flags)

{

    Scene::draw(renderer, transform,_transformUpdated);

    world->DrawDebugData();

}

(四)碰撞坐标系和坐标之间有一个32的转换如设置边界时候会用到

#define PTM_RATIO 32

 b2EdgeShape groundBox;

    

    groundBox.Set(b2Vec2(VisibleRect::leftBottom().x/PTM_RATIO,VisibleRect::leftBottom().y/PTM_RATIO),b2Vec2(VisibleRect::rightBottom().x/PTM_RATIO,VisibleRect::rightBottom().y/PTM_RATIO));

    groundBody->CreateFixture(&groundBox,0);

    

    groundBox.Set(b2Vec2(VisibleRect::leftTop().x/PTM_RATIO,VisibleRect::leftTop().y/PTM_RATIO),b2Vec2(VisibleRect::rightTop().x/PTM_RATIO,VisibleRect::rightTop().y/PTM_RATIO));

    groundBody->CreateFixture(&groundBox,0);

 

    groundBox.Set(b2Vec2(VisibleRect::leftTop().x/PTM_RATIO,VisibleRect::leftTop().y/PTM_RATIO),b2Vec2(VisibleRect::leftBottom().x/PTM_RATIO,VisibleRect::leftBottom().y/PTM_RATIO));

    groundBody->CreateFixture(&groundBox,0);

    

    groundBox.Set(b2Vec2(VisibleRect::rightBottom().x/PTM_RATIO,VisibleRect::rightBottom().y/PTM_RATIO),b2Vec2(VisibleRect::rightTop().x/PTM_RATIO,VisibleRect::rightTop().y/PTM_RATIO));

    groundBody->CreateFixture(&groundBox,0);

()设置碰撞体大小

b2PolygonShape dynamicBox;

//设置0.5恰好包围住边界

    dynamicBox.SetAsBox(0.5 ,0.5);

(六)b2BodyType类型

 enum b2BodyType

    {

        b2_staticBody = 0,  //没有速度,不受力,可以碰撞

        b2_kinematicBody, //有速度,受力,可以碰撞

        b2_dynamicBody  //有速度,不受力,可以碰撞

    };



1 0