【cocos2dx】box2d使用

本文介绍了在Cocos2d-x项目中启用Box2D物理引擎的步骤,包括在Xcode中设置CC_ENABLE_BOX2D_INTEGRATION,删除climuk,以及重新生成库文件。同时,讲解了Box2D的Shape类型、Debug模式的设置,以及碰撞坐标转换和Body类型的使用。
摘要由CSDN通过智能技术生成

刚开始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,V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值