2013-11-20 45 views
0

我想開發一個遊戲,其中有幾個b2PolygonShape機構,它們應該從頂部下降。但是我想要的是我希望他們從一些隨機的位置上掉下來,並且有些拖延。到目前爲止,我所做的並沒有讓我這樣做,即身體會墜落,但他們會一起墜落。我不知道這個功能有些遲鈍,我不知道這個功能。我甚至不能從display函數中調用它。並且init函數只被調用一次。 這是我到目前爲止已經試過:Box2D:使機構隨機下降

aadBrick功能,這實際上是對身體應屬於

b2Body* addBrick(int x,int y,int w,int h,bool dyn=true) 
{ 
    b2BodyDef bodydef; 
    bodydef.position.Set(x*P2M,y*P2M); //Setting body position 
    if(dyn) 
    { 
      bodydef.type=b2_dynamicBody; // dynamic body means body will move 

    } 

    brick=world->CreateBody(&bodydef);  //Creating box2D body 

    b2PolygonShape shape;   //Creating shape object 
    shape.SetAsBox(P2M*w,P2M*h); 

    ////////////// Adding Fixtures(mass, density etc) ////////////// 


    brickFixture.shape=&shape; 
    brickFixture.density=1.0; 
    circleFixture.restitution = 0.7; 
    brick->CreateFixture(&brickFixture); 
    return brick; 
} 

這是init功能

void init() 
{ 
    glMatrixMode(GL_PROJECTION); 
    glOrtho(0,WIDTH,HEIGHT,0,-1,1); 
    glMatrixMode(GL_MODELVIEW); 
    glClearColor(0,0,0,1); 

    world=new b2World(b2Vec2(0.0,5.8)); 

    addGround(WIDTH/2,HEIGHT-80,WIDTH,10,false); 

    addBrick(80,0,10,10);// these bricks should fall with some delay not together 
    addBrick(100,0,10,10); 

    actor=addActor(80,460,50,70,false); // static body 

} 

這是定時器功能,如果這與延遲有關!

void Timer(int t) 
{ 
world->Step(1.0/30.0,8,3); 

glutPostRedisplay(); 
glutTimerFunc(1000/30,Timer,1); 
} 
+0

您在同一時間做兩個addBrick。在做第二個之前,你只需等待。 – iforce2d

+0

如何設置「等待」的東西?那就是我要求的 – Vector

+0

我該如何實現某種延遲? – Vector

回答

2

我建議未來的解決方案:

int mCounter = 0; 

    #define MAX_DELAY 60 

    void Timer(int t) 
    { 
     if (mCounter <= 0) 
     { 
      // rand() % 100 - random value in range 0 - 99 
      addBrick(rand() % 100, 0,10,10); 

      mCounter = rand() % MAX_DELAY; 
     } 
     mCounter -= t; 

     world->Step(1.0/30.0,8,3); 

     glutPostRedisplay(); 
     glutTimerFunc(1000/30,Timer,1); 
    } 
+0

酷!謝謝你讓我的一天! – Vector