Balance bar with box2d in cocos2dx

Hello. I'm trying to create a balance bar with box2d but I really can't do it. I'm very new with box2d and I read a lot of tutorials but I can't do it work. Here is what I have:

Image


As you can see, the bar and the triangle are static, they don't move. The square fall down from above. I don't want that, I want a balance. It doesn't matter where the box fall down, the bar doesn't move.

Here is the source:

Code: Select all

////////////////////////////// BOX2D //////////////////////////////////
b2Vec2 gravity = b2Vec2(0.0f, -9.8f);

this->mB2World = new b2World(gravity);
this->mB2World->SetAllowSleeping(false);

b2BodyDef floorDef;
floorDef.position.Set(Director::getInstance()->getVisibleOrigin().x / PTM_RATIO, 300.0f / PTM_RATIO);

b2EdgeShape floorShape;
floorShape.Set(b2Vec2(0.0f, 0.0f), b2Vec2(Director::getInstance()->getWinSize().width / PTM_RATIO, 0.0f));

b2FixtureDef floorFixture;
floorFixture.shape = &floorShape;

b2Body * floorBody = this->mB2World->CreateBody(&floorDef);
floorBody->CreateFixture(&floorFixture);

///////////////////////////////////////////////////////////////
b2BodyDef pivotDef;
pivotDef.position.Set(this->mTriangle->getPositionX() / PTM_RATIO, (this->mTriangle->getPositionY()) / PTM_RATIO);
pivotDef.userData = this->mTriangle;
pivotDef.type = b2_staticBody;

this->mTriangleBody = this->mB2World->CreateBody(&pivotDef);

b2PolygonShape triangleShape;
triangleShape.SetAsBox(2/PTM_RATIO, 2/PTM_RATIO);

b2FixtureDef triangleFixture;
triangleFixture.shape = &circleShape;
triangleFixture.density = 0.0f;
triangleFixture.friction = .1f;
this->mTriangleBody->CreateFixture(&triangleFixture);
///////////////////////////////////////////////////////////////

b2BodyDef groundBodyDef;
groundBodyDef.type = b2_dynamicBody;
groundBodyDef.position.Set(this->mGround->getPositionX() / PTM_RATIO, this->mGround->getPositionY() / PTM_RATIO);
groundBodyDef.userData = this->mGround;
groundBodyDef.fixedRotation = true;

this->mGroundBody = this->mB2World->CreateBody(&groundBodyDef);


b2PolygonShape groundShape;
groundShape.SetAsBox(this->mGround->getContentSize().width / PTM_RATIO / 2, this->mGround->getContentSize().height / PTM_RATIO / 2);

b2FixtureDef groundFixture;
groundFixture.shape = &groundShape;
groundFixture.density = 0.3f;
groundFixture.friction = .0f;
this->mGroundBody->CreateFixture(&groundFixture);


///////////////////////////////////////////////////////////////

b2BodyDef squareBodyDef;
squareBodyDef.type = b2_dynamicBody;
squareBodyDef.position.Set(this->mSquare->getPositionX() / PTM_RATIO, this->mSquare->getPositionY() / PTM_RATIO);
squareBodyDef.userData = this->mSquare;
squareBodyDef.fixedRotation = true;

this->mSquareBody = this->mB2World->CreateBody(&squareBodyDef);


b2PolygonShape squareShape;
squareShape.SetAsBox(this->mSquare->getContentSize().width / PTM_RATIO / 2, this->mSquare->getContentSize().height / PTM_RATIO / 2);

b2FixtureDef squareFixture;
squareFixture.shape = &squareShape;
squareFixture.density = 1.0f;
squareFixture.friction = 0.2f;
this->mSquareBody->CreateFixture(&squareFixture);
///////////////////////////////////////////////////////////////////

b2RevoluteJointDef balance;
balance.Initialize(this->mGroundBody, this->mTriangleBody, this->mGroundBody->GetWorldCenter());
balance.enableMotor = true;
this->mBalanceJoin = (b2RevoluteJoint*)this->mB2World->CreateJoint(&balance);

///////////////////////////////////////////////////////////////


If you can help me, I'll be very grateful. Thanks.
Ok, I finally could resolved it. It was the fixrotation property on the ground body. It should was on false.

Original thread: https://box2d.org/forum/viewtopic.php?t=10221