b2body未在b2world-Box2D中显示

时间:2013-12-13 11:39:16

标签: android box2d cocos2d-x

b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody;
myBodyDef.position.Set(_screenSize.width / 3, _screenSize.height / 3);
myBodyDef.angle = 0; 
b2Body* dynamicBody = m_world->CreateBody(&myBodyDef);
b2PolygonShape boxShape;
boxShape.SetAsBox(10, 10);

PTM_RATIO = 40.0f;
m_debugDraw = new GLESDebugDraw(PTM_RATIO);
m_world->SetDebugDraw(m_debugDraw);

b2FixtureDef boxFixtureDef;
boxFixtureDef.shape = &boxShape;
boxFixtureDef.density = 1;
dynamicBody->CreateFixture(&boxFixtureDef);

我在cocos2d-x 2.2.1中使用box2d

使用这个简单的代码在屏幕上显示b2body但是使用

m_debugDraw = new GLESDebugDraw(PTM_RATIO);

我正在

Classes/HelloWorldScene.cpp:164: error: undefined reference to 'GLESDebugDraw::GLESDebugDraw(float)'

我正在使用他们的参考项目,但我无法找到为什么该对象没有在屏幕上显示。

如果我没有声明PTM_RATIO的值,则会给出错误

PTM_RATIO value was not declared in this scope.

更新:1:

通过在我的android.mk文件中包含GLES_Render.cpp,清除了与PTM_RATIO相关的错误。

更新:2:

这是我的绘制方法代码

void HelloWorld::draw() {
//
// IMPORTANT:
// This is only for debug purposes
// It is recommend to disable it
//
CCLayer::draw();

ccGLEnableVertexAttribs (kCCVertexAttribFlag_Position);

kmGLPushMatrix();

m_world->DrawDebugData();

kmGLPopMatrix();
}

更新:3:

我的最终代码:

我的android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := game_shared

LOCAL_MODULE_FILENAME := libgame

LOCAL_SRC_FILES := hellocpp/main.cpp \
               ../../Classes/AppDelegate.cpp \
               ../../Classes/HelloWorldScene.cpp \
               ../../Classes/GLES_Render.cpp

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
                /home/user/adt/cocos2dx/external/Box2D \
                /home/user/adt/cocos2dx/external


LOCAL_WHOLE_STATIC_LIBRARIES := cocos_testcpp_common
LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static
LOCAL_WHOLE_STATIC_LIBRARIES += chipmunk_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cocos2dx)
$(call import-module,cocos2dx/platform/third_party/android/prebuilt/libcurl)
$(call import-module,CocosDenshion/android)
$(call import-module,extensions)
$(call import-module,external/Box2D)
$(call import-module,external/chipmunk)

这就是我定义b2world

的方式
m_world = new b2World(gravity);
m_world->SetAllowSleeping(true);
m_world->SetContinuousPhysics(true);
m_debugDraw = new GLESDebugDraw(PTM_RATIO);
m_world->SetDebugDraw(m_debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
//  flags += b2Draw::e_jointBit;
/*flags += b2Draw::e_aabbBit;
 flags += b2Draw::e_pairBit;
 flags += b2Draw::e_centerOfMassBit;*/
m_debugDraw->SetFlags(flags);

然后在菜单精灵点击回调方法我用它来显示一个b2body

b2BodyDef myBodyDef;
myBodyDef.type = b2_dynamicBody; //this will be a dynamic body
myBodyDef.position.Set(_screenSize.width / 3, _screenSize.height / 3);
//set the starting position
myBodyDef.angle = 0; //set the starting angle
b2Body* dynamicBody = m_world->CreateBody(&myBodyDef);
dynamicBody->SetType(b2_staticBody);
b2PolygonShape boxShape;
boxShape.SetAsBox(200, 200);

b2FixtureDef boxFixtureDef;
boxFixtureDef.shape = &boxShape;
boxFixtureDef.density = 1;
dynamicBody->CreateFixture(&boxFixtureDef);

请建议我在哪里做错了。我找不到它。感谢

3 个答案:

答案 0 :(得分:2)

android.mk中,您必须在.cpp文件中添加GLESDebugDraw.cpp并添加GLESDebugDraw.h

如果你想调试draw,那么你必须在draw方法中只写这些行

CCLayer::draw();
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );

kmGLPushMatrix();

world->DrawDebugData();

kmGLPopMatrix();

在init方法或构造函数中

debugDraw = new GLESDebugDraw(PTM_RATIO);
world->SetDebugDraw(debugDraw);

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);

试试这个,这是我的初始化方法............

    bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }



    this->setTouchEnabled(true);

    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    array2->createWithCapacity(4);

    b2Vec2 gravity;
    gravity.Set(0.0f, -10.0f);
    _world = new b2World(gravity);
    _world->SetContactListener(this);

       // Do we want to let bodies sleep?
    _world->SetAllowSleeping(false);

    _world->SetContinuousPhysics(true);

    _debugDraw = new GLESDebugDraw(PTM_RATIO);
    _world->SetDebugDraw(_debugDraw);



      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);


    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));

    pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label

    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

    // position the label on the center of the screen
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
//    this->addChild(pSprite, 0);

    plbdy();

    schedule(SEL_SCHEDULE(&HelloWorld::update), 0);
    return true;
}

这是我的plbdy()

plbdy()
{
b2BodyDef bdf ;

    bdf.type = b2_dynamicBody;

    b2PolygonShape shape;
    shape.SetAsBox(1,1);
    b2FixtureDef fd;
    fd.shape = &shape;
    fd.density = 1;

b2Body hero = _world->CreateBody(&bdf2);
    hero->CreateFixture(&fd);
}

这是我更新世界的Box2d更新方法

update(float dt)
{
int velocityIterations = 8;
    int positionIterations = 1;
_world->Step(dt, velocityIterations, positionIterations);
}

'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''” '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''

Try these make a simple body nothing more and then check body is created or not

答案 1 :(得分:0)

您遇到的错误是:

Classes/HelloWorldScene.cpp:164: error: undefined reference to 'GLESDebugDraw::GLESDebugDraw(float)'

看起来您的GLESDebugDraw未定义。你是否包含了类头文件?

答案 2 :(得分:0)

需要将GLES_Render.cpp添加到android.mk文件中。我已添加到mk文件,它可以使用完整的debugdraw。以下是我在我的hello world中包含PTM_RATIO的地方

    #ifndef __HELLOWORLD_SCENE_H__
    #define __HELLOWORLD_SCENE_H__

    #include "cocos2d.h"
    #include "Box2D/Box2D.h"
    #include "ContactListener.h"
    #include "GLES-Render.h"


    #define PTM_RATIO 32

    class HelloWorld : public cocos2d::CCLayer
    {
           public: