2014-01-18 41 views
0

cocos2dx + box2d: 你好。我其實有一個問題。 我做了一點繩子,她的行爲正常。但是如果物體系上兩根繩子,它會突然停下來。任何人都可以理解爲什麼發生這種情況,並可以幫助嗎我用兩根繩索錄製了一段視頻,這表明該對象尖銳地ostanavliaetsya。並用一根繩索,在那裏一切都很好視頻:cocos2dx + box2d:對象突然停下來,繫上兩根繩索

一繩:http://youtu.be/Yh4x8qrgmgo

兩根繩子: http://youtu.be/iRPVPLrC9ZQ 因爲什麼對象可以如此突然停止?

加繩:

GameScene::createRope(blk1,groundBody,blk1->GetLocalCenter(), cc_to_b2Vec(580, screen.height/1) ,1.0); 
    GameScene::createRope(groundBody,blk1,cc_to_b2Vec(380, screen.height/2), blk1->GetLocalCenter() ,1.1); 

創建繩:

void GameScene::createRope(b2Body *bodyA, b2Body *bodyB, b2Vec2 anchorA, b2Vec2 anchorB, float sag) 
{ 
    b2RopeJointDef jd; 
    jd.bodyA = bodyA; 
    jd.bodyB = bodyB; 

    jd.localAnchorA = anchorA; 
    jd.localAnchorB = anchorB; 
    jd.collideConnected = true; 

    // Max length of joint = current distance between bodies * sag 
    float32 ropeLength = (bodyA->GetWorldPoint(anchorA)- bodyB->GetWorldPoint(anchorB)).Length()*sag; 
    jd.maxLength = ropeLength; 

    // Create joint 
    b2RopeJoint *ropeJoint = (b2RopeJoint *)world->CreateJoint(&jd); 

    VRope *newRope = new VRope(ropeJoint, _ropeSpriteSheet); 
    _ropes.push_back(newRope); 
} 

http://youtu.be/Lchc1II3cjY - 繩索被安排在同一高度。基本上相同的物體應該上下襬動

實際上,繩子緊繃,但精靈看起來並不舒展,因此看起來物體應該擺動。我不知道如何解決它... 創建繩:

void VRope::createRope(const CCPoint& pointA, const CCPoint& pointB,float distance) 
{ 
    // 16; //12; //increase value to have less segments per rope, decrease to have more segments 
    int segmentFactor = 20; 
    numPoints = (int) distance/segmentFactor; 

    CCPoint diffVector = ccpSub(pointB,pointA); 
    float multiplier = distance/(numPoints-1); 
    antiSagHack = 0.1f; //HACK: scale down rope points to cheat sag. set to 0 to disable, max suggested value 0.1 
    for(int i=0;i<numPoints;i++) { 
     CCPoint tmpVector = ccpAdd(pointA, ccpMult(ccpNormalize(diffVector), multiplier*i*(1-antiSagHack)));   
     VPoint *tmpPoint = new VPoint(); 
     tmpPoint->setPos(tmpVector.x, tmpVector.y); 
     vPoints.push_back(tmpPoint); 
    } 
    for(int i=0;i<numPoints-1;i++) { 
     VStick* tmpStick = new VStick(vPoints[i], vPoints[i+1]); 
     vSticks.push_back(tmpStick); 
    } 

    if(spriteSheet) { 
     for(int i=0;i<numPoints-1;i++) { 
      VPoint* point1 = vSticks[i]->getPointA(); 
      VPoint* point2 = vSticks[i]->getPointB(); 
      CCPoint stickVector = ccpSub(ccp(point1->x,point1->y),ccp(point2->x,point2->y)); 
      float stickAngle = ccpToAngle(stickVector); 

      float f = spriteSheet->getTextureAtlas()->getTexture()->getPixelsHigh()/CC_CONTENT_SCALE_FACTOR(); 
      CCRect r = CCRectMake(0, 0, multiplier, f); 
      CCSprite* tmpSprite = CCSprite::createWithTexture(spriteSheet->getTexture(), r); 

      ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT}; 
      tmpSprite->getTexture()->setTexParameters(&params); 
      tmpSprite->setPosition(ccpMidpoint(ccp(point1->x, point1->y), ccp(point2->x, point2->y))); 
      tmpSprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(stickAngle)); 
      spriteSheet->addChild(tmpSprite); 
      ropeSprites.push_back(tmpSprite); 
     } 
    } 
} 

如果我增加的繩子會被拉伸的段數,而不是看上去很美。是的,我砍關節,所以如果增加的段數(數量減少點和繩子看起來像一根棍子)

http://youtu.be/hn8ALt2P8pE

如果切割不準確....

你增加了細分的數量(減少點數和繩子看起來像一根棍子),如果你減少驚人的數量段,它不是真的太下垂。我究竟做錯了什麼?在繩索上的所有代碼

http://github.com/Banck/Rope

請幫助我

+0

你可以添加你的代碼到帖子? – FuzzyBunnySlippers

+0

從視頻看來,你的第二根繩子太短了。您正在使用(可能是一條三角繩)來模擬繩索的連接,但是您可以添加box2d調試圖來顯示您正在使用的實際繩索連接嗎? – FuzzyBunnySlippers

+0

是的,我用的是Verlet Rope。第二根繩子確實很短,但實際上物體應該從一側到另一側略微擺動。 – user3079929

回答

1

我懷疑你所遇到的問題是在設置你的第二個繩的長度。如何設置它會使物體擺動更多/更少。

我採用了默認的cocos2d-x box2d模板並對其進行了修改,使其成爲「擺動盒子」演示。這裏是什麼樣子的屏幕截圖:

enter image description here

有創建時連接到每個箱子3股。

第一根繩子將場景頂部(相對於地面框)的一點連接到該塊。如果自由擺動,盒子會像鐘擺一樣來回擺動。

第二根繩子從左側(從盒子開始的高度)連接,其長度等於從頂部關節的x位置到盒子創建的x位置的距離。這可以防止盒子向右「擺動太遠」。

第三個關節就像第二根繩子一樣在附件上。它可以防止盒子向左擺動太遠。

擺動看起來很自然,但如果它被另一個箱子碰撞,擺動不會超過原來的左右距離。如果將它們縮短,那麼該框只會在縮短的距離範圍內擺動。

這裏是我用來創建關節代碼:如果修改的distToCenter的值,例如乘以0.5就

// Calculate the local position of the 
    // top of screen in the local space 
    // of the ground box. 
    CCSize scrSize = CCDirector::sharedDirector()->getWinSize(); 
    b2Vec2 groundWorldPos = b2Vec2((scrSize.width/2)/PTM_RATIO,(scrSize.height)/PTM_RATIO); 
    b2Vec2 groundLocalPos = m_pGround->GetLocalPoint(groundWorldPos); 

    // Now create the main swinging joint. 
    b2RopeJointDef jointDef; 
    jointDef.bodyA = m_pGround; 
    jointDef.bodyB = body; 
    jointDef.localAnchorA = groundLocalPos; 
    jointDef.localAnchorB = b2Vec2(0.0f,0.0f); 
    jointDef.maxLength = (groundWorldPos-body->GetWorldCenter()).Length(); 
    jointDef.collideConnected = true; 
    world->CreateJoint(&jointDef); 

    // Now create a second/third rope to "constrain" the swing. 
    // These one will be attached to the side of the screen. 
    b2Vec2 groundWorldPos2 = b2Vec2(0,body->GetWorldCenter().y); 
    b2Vec2 groundLocalPos2 = m_pGround->GetLocalPoint(groundWorldPos2); 

    jointDef.localAnchorA = groundLocalPos2; 
    // Setting the length of the side rope... 
    // What we want to do here is use the distance from the center as 
    // the length of the rope. Then add length to it so that the box 
    // can have at least one full swing which ever side it is on (left or 
    // right half of the scene). 
    float32 distToCenter = (fabs(scrSize.width/2 - (body->GetWorldCenter().x)*PTM_RATIO))/PTM_RATIO; 
    jointDef.maxLength = distToCenter + (scrSize.width/2)/PTM_RATIO; 
    world->CreateJoint(&jointDef); 

    // Now for the last rope, other side of the scene. 
    // Now create a second/third rope to "constrain" the swing. 
    // These one will be attached to the side of the screen. 
    b2Vec2 groundWorldPos3 = b2Vec2((scrSize.width)/PTM_RATIO,body->GetWorldCenter().y); 
    b2Vec2 groundLocalPos3 = m_pGround->GetLocalPoint(groundWorldPos3); 
    jointDef.localAnchorA = groundLocalPos3; 
    world->CreateJoint(&jointDef); 

,你會看到箱子很快就會「啪」的縮短擺動,然後在完成擺動時對其他繩索長度「反彈」。總的來說,物理看起來是正確的。你甚至可以通過點擊和看到整個事物的反彈來添加更多的盒子(比如牛頓擺錘)。

你可以不是連接任一繩索和框將能夠在該方向上進一步擺動。

整個代碼解決方案is posted on github here

對您有幫助嗎?

+0

非常感謝,我意識到我只是串起一根繩子,需要增加它們的長度。我可以做。但是,我沒有糾正過的繩索本身並不是這個精靈本身被拉伸,而是一種應該擺動的感覺。也許你可以幫我解決這個問題?已經花了兩天時間來實施,但不是很有意思( 你能說你的電子郵件地址,所以我可以與你聯繫, – user3079929

+0

我的vrope - https://github.com/Banck/Rope我可以讓那個錯誤 – user3079929

+0

我更新了我的帖子,請看這 – user3079929