Rotating a menu

I want a menu that bounces onto the screen and thought using box2d to handle the movement might work well.

Setting the position of the menu works ok, but if I apply the rotation as well it gradually walks off the point and ends up nowhere near the b2body it’s supposed to be following.

I assume it’s something to do with the anchor point, but can’t figure out how to fix it. I’ve explicitly set the anchor point as 0.5,0.5, but it made no difference.

Here’s a code example. It just two bodies attached with a distance joint and the body representing the menu location is given a little angular impulse. The menu starts tied to the menuBody, but then ends up like this;

bool HelloWorld::init()
{
    bool bRet = false;

    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        ptm_ratio = CCEGLView::sharedOpenGLView()->getFrameSize().width / 8.81;
        world = new b2World(b2Vec2(0.0,-10));
        world->SetAllowSleeping(true);
        world->SetContinuousPhysics(true);
        m_debugDraw = new GLESDebugDraw( ptm_ratio );
        world->SetDebugDraw(m_debugDraw);

        uint32 flags = 0;
        flags += b2Draw::e_shapeBit;
        flags += b2Draw::e_jointBit;
        m_debugDraw->SetFlags(flags);

        b2BodyDef myBodyDef;
        myBodyDef.type =  b2BodyType::b2_dynamicBody;
        myBodyDef.position.Set(4.4f, 4.6f); 

        b2CircleShape circleShape;
        circleShape.m_p.Set(0, 0);
        circleShape.m_radius = 0.1f;

        b2FixtureDef myFixtureDef;
        myFixtureDef.shape = &circleShape;
        myFixtureDef.density = 1;

        myFixtureDef.filter.maskBits = 0;
        menuBody = world->CreateBody(&myBodyDef);
        menuBody->CreateFixture(&myFixtureDef);
        myBodyDef.position.Set(4.4f, 2.5f); 
        myBodyDef.type = b2BodyType::b2_staticBody;
        b2Body* centerBody = world->CreateBody(&myBodyDef);
        centerBody->CreateFixture(&myFixtureDef);

        b2DistanceJointDef* def = new b2DistanceJointDef();
        def->bodyA = menuBody;
        def->bodyB = centerBody;
        def->collideConnected = false;
        def->length = 0.0;
        def->frequencyHz = 1.0;
        def->dampingRatio = 0.2;
        menuBody->SetGravityScale(0);
        world->CreateJoint(def);

        menu = CCMenu::create();

        menu->addChild(CCMenuItemFont::create("<", this, menu_selector(HelloWorld::menuCloseCallback)));
        menu->addChild(CCMenuItemFont::create("@", this, menu_selector(HelloWorld::menuCloseCallback)));
        menu->addChild(CCMenuItemFont::create(">", this, menu_selector(HelloWorld::menuCloseCallback)));

        menu->alignItemsHorizontallyWithPadding(2); 
        this->addChild(menu);

        menuBody->ApplyAngularImpulse(1);
        scheduleUpdate();
        bRet = true;
    } while (0);

    return bRet;
}

void HelloWorld::update(float dt)
{
    world->Step(dt, 8, 1);
    menu->setRotation(menuBody->GetAngle());
    menu->setPosition(menuBody->GetPosition().x * ptm_ratio, menuBody->GetPosition().y * ptm_ratio);
}   

Angle is in radians, while rotation is in degrees. You’ll need to convert it first into degrees.

What Terry said.

menu->setRotation( -1 * CC_RADIANS_TO_DEGREES(menuBody->GetAngle()));