C++ Box2D:使实体以随机间隔下落

C++ Box2D:使实体以随机间隔下落,c++,opengl,timer,2d,box2d,C++,Opengl,Timer,2d,Box2d,我正在尝试开发一个游戏,其中我有几个b2PolygonShape身体,它们应该从顶部掉落。但我想要的是,我希望它们从随机位置落下,并有一定的延迟。到目前为止,我所做的并没有让我完成这项工作,即物体确实会掉落,但它们会一起掉落。我不知道如何延迟调用该函数!我甚至不能从display函数调用它。而init函数只被调用一次。 这就是我迄今为止所尝试的: aadBrick的功能实际上是针对应该坠落的身体 b2Body* addBrick(int x,int y,int w,int h,bool dyn=

我正在尝试开发一个游戏,其中我有几个
b2PolygonShape
身体,它们应该从顶部掉落。但我想要的是,我希望它们从随机位置落下,并有一定的延迟。到目前为止,我所做的并没有让我完成这项工作,即物体确实会掉落,但它们会一起掉落。我不知道如何延迟调用该函数!我甚至不能从
display
函数调用它。而
init
函数只被调用一次。 这就是我迄今为止所尝试的:

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);
}

我建议下一个解决方案:

    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);
    }
int mCounter=0;
#定义最大延迟60
无效计时器(int t)
{
如果(M计数步骤(1.0/30.0,8,3);
再发现();
glutTimerFunc(1000/30,定时器,1);
}

你正在同时做两个addBrick。你只需要等待,然后再做第二个。如何设置“等待”的东西?这就是我要问的问题我如何才能实现某种延迟?酷!谢谢你让我开心!!