使用C ++中的GetTickCount()更新时间和位置

时间:2014-03-17 11:27:24

标签: c++ winapi

问题:我正在尝试使用getTickCount更新时间而无济于事。 情况:我目前使用getTickCount获取时差并将其传递给世界更新方法参数。但是在我更新位置的更新方法中,传入一个较大的值(即使我除以1000),因此位置会向位置向量添加一个奇数4000.

以下代码:

Simulation.CPP:

    int Simulation::simControlLogic(HWND hWnd, keyEvent event)
{
    /* TO DO: add relevant code */


    if (event != QUIT)
    {
        previousTime = 0;

        frameStartTime = GetTickCount();

        if (previousTime == 0)
            previousTime = frameStartTime;

        timeDifference = (frameStartTime - previousTime)  / 1000.0f; // this generates the difference between the last and current time
        world.update(event, &graphics, timeDifference);                 // update parameters of virtual world

        gameLoopDelay(frameStartTime);

        simDisplayFrame(hWnd);          // display frame

        previousTime = frameStartTime;  //the current time is set to the previous time for the next loop step

    }

   return 1;
}

调查世界更新方法:

int WorldData::update(keyEvent kEvent, GraphicsM * pGraphicsModule, float timeStep)
    {

    //updates the particle
    for (int i = 0; i < 3; i++)
    {
        particles[i].Update(kEvent, pGraphicsModule, timeStep);
    }

    return 1;
    }

研究粒子更新方法:

void ParticleModel::Update(keyEvent kEvent, GraphicsM * pGraphicsModule, float timeStep)
{
    move(timeStep);
}

调查移动方法:

void ParticleModel::move(float timeStep)
{

    velocity.y = 0.5F;
    velocity.x = 0.5F;
    acceleration.x = 0.0F;
    acceleration.y = 0.0F;

    pos.x += velocity.x * timeStep; //here is the problem. I get a large value e.g 79637.1788 causing pos.x to be ridiculously large
    pos.y += velocity.y * timeStep; //here is the problem. I get a large value e.g 79637.1788 causing pos.y to be ridiculously large


}

1 个答案:

答案 0 :(得分:0)

将previousTime移动到模拟器构造函数将其初始化为0后,它就可以了。感谢您的回复