PhysicsWorld is not working

Hi everyone! I’m trying to use physics in cocos2d-x but it doesn’t work. I want to reproduce part of example from manual. Here is my init() code:

    if (!Scene::initWithPhysics())
        return false;

    getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

    auto visibleSize { Director::getInstance()->getVisibleSize() };
    Point screenCenter { visibleSize.width/2, visibleSize.height/2 };

    auto square { Sprite::create("square.png") };
    auto physicsBody { PhysicsBody::createBox(square->getContentSize(), PhysicsMaterial(0.1f, 1.0f, 0.0f)) };

    physicsBody->setGravityEnable(false);
    physicsBody->setVelocity(Vec2(cocos2d::random(-500,500), cocos2d::random(-500,500)));
    physicsBody->setTag(10);

    square->setPosition({screenCenter.x, screenCenter.y});
    square->addComponent(physicsBody);
    addChild(square);

    return true;

However, I get a static object as if I were creating a normal sprite. It doesn’t even show a red debug mark around it. getPhysicsWorld()->getAllBodies().size() gives me 0. Also I tried to create EdgeBox but got same problem.


I’m using Linux, compile with gcc 10.2.0, cocos2d-x 4.0 (also tried with 3.17.2). This init() code were made in automatically created HelloWorld scene in new project. I had problems with chipmunk (undefined reference to `__powf_finite') so I built libchipmunk with -fno-builtin flag and replace prebuilt lib with currently built. Demos in libchipmunk working without problems with new buildDemos in libchipmunk working without problems with new build, but not my code with cocos :frowning:

Hope this helps someone else who face the same problem. It seems that cocos2d cannot be built with gcc > 7.5.0. I have compiled gcc 9, 8 and 7 and tried to compile the code above. Only on the 7.5.0 version of gcc physics works. In versions newer than 7, the cocos2d physical cpp-tests do not work and throw assertion errors or just create static sprites even though the library chipmunk tests works fine.

Interesting I am still using 7.5. I don’t upgrade my installation much.

What Linux distro are you using?

I’m using Arch Linux. As far as I know, gcc 7.5 is installed by default on Ubuntu 18.04.

I’ve got vms of 18.04, 19.04, 20.04 but they all are on gcc 7.5 iirc

Apparently this is not very relevant and no one except me needs to use new versions of the compiler. However, here is the solution to this problem.

To use gcc > 7.5 you should to download chipmunk sources (I used this one) and build it. For example:

mkdir build
cmake ..
make

After that copy headers and static library to the project directory.

rm <path-to-project>/cocos2d/external/chipmunk/include/chipmunk/*
cp <path-to-src>/include/chipmunk/* <path-to-project>/cocos2d/external/chipmunk/include/chipmunk/
cp <path-to-build>/src/libchipmunk.a <path-to-project>/cocos2d/external/chipmunk/prebuilt/linux/64-bit/
chmod 755 <path-to-project>/cocos2d/external/chipmunk/prebuilt/linux/64-bit/libchipmunk.a

where <path-to-project> is path to your project, <path-to-build> is path to chipmunk build and <path-to-src> is path to chipmunk sources.

Then:

  • include chipmunk/cpHastySpace.h in cocos2d/cocos/physics/CCPhysicsWorld.cpp;
  • change line 77 of cocos2d/extensions/physics-nodes/CCPhysicsDebugNode.cpp from switch (shape->CP_PRIVATE(klass)->type) to switch (shape->type).
  • add extern C to cocos2d/external/chipmunk/include/chipmunk/cpHastySpace.h. Your cpHastySpace.h should look smth like
#ifdef __cplusplus
extern "C" {
#endif

....

#ifdef __cplusplus
}
#endif

If you want to change c++ version, change 11 on line 63 in set(CMAKE_CXX_STANDARD 11) in cmake/Modules/CocosConfigDefine.cmake to version you need.

1 Like

What is your use case for a newer compiler? Perhaps it’s time we switch to a newer version.

I use Arch Linux on both my home and work system, so I have the latest version of the compiler. And I am using my own “library” which contains some C++17 and C++20 features not implemented in gcc 7

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.