box2d objects do not appear on screen?

Hi!

I have initialized the box2d physics and a shape here in init()

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
	float PTM_RATIO = 32.0;
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

	CCLOG("123123123");
	b2Vec2 gravity;
	gravity.Set(0.0f, -10.0f);
	world = new b2World(gravity);
	world->SetAllowSleeping(true);
	world->SetContinuousPhysics(true);


	this->addChild(B2DebugDrawLayer::create(world, PTM_RATIO), 9999);
	b2BodyDef myBodyDef;
	myBodyDef.type = b2_staticBody; //this will be a dynamic body
	myBodyDef.position.Set(0, 20); //set the starting position
	myBodyDef.angle = 0; //set the starting angle

	b2Body* dynamicBody = world->CreateBody(&myBodyDef);

	b2PolygonShape boxShape;
	boxShape.SetAsBox(1, 1);

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

    
    return true;
}

According to the tutorial I am following I should be able to see this:

But my screen is just black. What can I do to get the same result as in the image above?

Thank you!

are you missing an addChild()?

´How should i add a b2 object using addChild? Do you have to do that and if so, how? And what objects am i supposed to add then, like the world, fixture or what? Thank you

actually it looks like what you are doing is fine. I looked to quickly.

1 Like

Box2d doesn’t automatically draw anything - you need to implement the debug draw functionality yourself.

I’ts not hard - search here
I think


will help you.

Thank you so much. It all works now :slight_smile:

No worries.