Adding physics world to my game

Hello I am following this tutorial, but the code is a little different from the code a new cocos2d-x project creates they have
GameScene.h

class GameScene : public cocos2d::Scene
{
public:
 static cocos2d::Scene* createScene();

   virtual bool init();
    // implement the "static create()" method manually
    CREATE_FUNC(GameScene);
    
private:
    void setPhysicsWorld( cocos2d::PhysicsWorld *world) { sceneWorld = world; }
  cocos2d::PhysicsWorld *sceneWorld;
};

GameScene.cpp

Scene* GameScene::createScene()
{
 // 'scene' is an autorelease object 
 auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask( PhysicsWorld::DEBUGDRAW_ALL );

// 'layer' is an autorelease object
auto layer = GameScene::create();
layer->setPhysicsWorld( scene->getPhysicsWorld() );
scene->addChild(layer);

return scene

}

but the new cocos2d-x code doesnt have layer in the createScene method ? would this be the right way to implement the equivalent code to the top ? do i need to make a layer object to add setPhsyicsWorld to like above ?

 Scene* HelloWorld::createScene()
 {
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

     
     return scene;
 }

I noticed in alot of other tutorials and books i’ve been reading they add the layer code the createScene method ?
also what is the difference between createWithPhysics and initWithPhysics ??

The only thing you need to do is in your scene init method. Where it says,

if (!Scene::init()){
    return false;
}

to this,

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

Everything else is same as in HelloWorld Example.

and this will replace the need for the setPhsyicsWorld method? , and “auto scene = Scene::createWithPhysics();”? also if thats all I change to a newly created cocos2d-x project to add physicsWorld what object do I call ->setDebugDrawMask( PhysicsWorld::DEBUGDRAW_ALL ) on ??

yes, Here is my create scene method,

Scene* GamePlayScene::createScene()
{
	auto scene = GamePlayScene::create();
	scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
	return scene;
}
1 Like

ok so all these books I have are outdated then from 2015, they all say I need to add layer inside the create scene method like I put in the above code, this is no longer necessary ??

yes, Your image answers your question.

1 Like

do you know which version of cocos2d-x initWithPyhsics was introduced in ? im wondering why they didn’t use it in this book, and classes derived from layer in this book to instead of scene

I am not sure. I am also new to this community. I started using it from 3.16.

1 Like

Sorry. What’s the actual problem? Just create your scene and add your pieces.

1 Like

I did not know how to add physics world in the new cocos2d-x code because all the books and old tutorials im following go by this
Scene* GameScene::createScene() {
// ‘scene’ is an autorelease object
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask( PhysicsWorld::DEBUGDRAW_ALL );

// ‘layer’ is an autorelease object auto layer = GameScene::create();
layer->setPhysicsWorld( scene->getPhysicsWorld() );
scene->addChild(layer);
return scene }

and in the new cocos2d-x projects they dont have any mention of the ‘layer’ object in createScene method so I just wanted to know how to implement physics in cocos2d-x 3.17 pretty much

i think i got it now though ! @tink3r_t told me I just needed to add initWithPhysics and all the code about ‘layer’ can be removed

We also talk about it http://docs.cocos2d-x.org/cocos2d-x/en/physics/getting_started.html

1 Like

there is no docs about calling “setDebugDrawMask” ??
also one question if anybody knows the answer im trying to figure out why on the tutorials im following they call both of these in HelloWorld::createScene() method
auto scene = Scene::create();
auto layer = HelloWorld::create();
and now in new cocos2d-x Projects they only call

return HelloWorld::create();
and they scene::create() isnt being called ?

Because HelloWorld is already a Scene?

I’ll add info about debug draw

1 Like

Oh ok that makes sense it’s a scene because it’s derived from scene , so they only use to call it before because HelloWorld use to be derived from layer ?

1 Like

True. Also, when I was looking at the Physics docs, we talk about debug draw already: http://docs.cocos2d-x.org/cocos2d-x/en/physics/debugging.html

1 Like