cocos2d-x 物理引擎Box2D 連接器之旋轉連接器

void WorldLayerSence::createBodyJointTo(
    b2World* world,const char* name)
{
    CCSize size =
        CCDirector::sharedDirector()->getVisibleSize();
    float x = rand()*100%(int)size.width/2;
    float y = rand()*100%(int)size.height/2;
    //1、創建精靈
    CCSprite* spritTrang =
        CCSprite::create(name);
    spritTrang->setPosition(ccp(size.width/1.2
                                -spritTrang->getContentSize().width/2/PTM_RATIO,
                                160/PTM_RATIO));
    spritTrang->setTag(2);
    addChild(spritTrang);
    //1創建剛體定義
    b2BodyDef trangBodyDef;
    trangBodyDef.userData =
        spritTrang;//剛體關聯精靈
    //剛體定義位置
    trangBodyDef.position.Set((size.width/1.2
                               -spritTrang->getContentSize().width/2)/PTM_RATIO,
                              160/PTM_RATIO);
    trangBodyDef.type=
        b2_dynamicBody;//剛體種類,動態剛體
    //2根據剛體定義產生剛體
    b2Body* trangBody = world->CreateBody(
                            &trangBodyDef);
    //3、賦予形狀
    b2PolygonShape shape;
    shape.SetAsBox(
        spritTrang->getContentSize().width/2/PTM_RATIO,
        spritTrang->getContentSize().height/2/PTM_RATIO);
    //4、定義關聯
    b2FixtureDef def;
    def.shape = &shape; //關聯形狀
    def.density = 3.5; //關聯密度
    def.restitution = 1; //關聯反彈係數
    def.friction = 1; //關聯摩擦力
    //設置關聯
    trangBody->CreateFixture(&def);
    //連接一個靜態剛體
    CCSprite* upSpr = CCSprite::create("up_bar.png");
    upSpr->setPosition(ccp(size.width/2,0));
    addChild(upSpr);
    trangBodyDef.type = b2_staticBody;
    trangBodyDef.position.Set(
        size.width/2/PTM_RATIO,
        0);//關聯位置
    //定義形狀
    b2PolygonShape upShape;
    upShape.SetAsBox(
        upSpr->getContentSize().width/2/PTM_RATIO,
        upSpr->getContentSize().height/2/PTM_RATIO);
    //定義關聯
    b2FixtureDef upFixDef;
    upFixDef.shape=&upShape;
    upFixDef.restitution = 1;//反彈係數
    b2Body* upBody = m_world->CreateBody(
                         &trangBodyDef);
    upBody->CreateFixture(&upFixDef);
    //1、生成一個連接定義
    //定義一個連接剛體
    b2RevoluteJointDef jointDef;
    //jointDef.Initialize(upBody, trangBody,
    //                    b2Vec2(size.width/1.2/PTM_RATIO,
    //                           160/PTM_RATIO));
    jointDef.bodyA = upBody; //靜態剛體
    //關聯的剛體A
    jointDef.bodyB =  trangBody;  //非靜態剛體
    //關聯的剛體B
    jointDef.collideConnected =  false;
    jointDef.localAnchorB.Set(0,0);
    jointDef.localAnchorA.Set(0,4);
    //不會發生碰撞
    jointDef.enableLimit = false;
    //關閉角度限制
    jointDef.lowerAngle =  CC_DEGREES_TO_RADIANS(
                               -360);
    //角度最低限制(由於關閉了角度限制,所以不起作用)
    jointDef.upperAngle =   CC_DEGREES_TO_RADIANS(
                                360);
    //角度最高限制(由於關閉了角度限制,所以不起作用)
    jointDef.enableMotor = true;
    //開啓連接器馬達
    jointDef.motorSpeed =  100;
    //連接器馬達的目標速度
    jointDef.maxMotorTorque =  100;
    //馬達的最大扭矩
    //旋轉速度
    jointDef.maxMotorTorque = 10.0f;
    //靈敏度
    jointDef.motorSpeed = 40.0f;
    //創建連接(關節)
    b2Joint* joint =
        trangBody->GetWorld()->CreateJoint(&jointDef);
    //使用旋轉連接器創建鏈條(下一條實現)
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章