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
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.
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