C++ 在C+;中使用GetTickCount()更新时间和位置+;
问题:我正在尝试使用getTickCount更新时间,但无效。 情况:我当前使用getTickCount获取时差,并将其传递到world update方法参数中。但是,在更新位置的更新方法中,传入了一个大值(即使我已除以1000),因此位置向位置向量添加了一个奇数4000 代码如下: Simulation.CPP:C++ 在C+;中使用GetTickCount()更新时间和位置+;,c++,winapi,C++,Winapi,问题:我正在尝试使用getTickCount更新时间,但无效。 情况:我当前使用getTickCount获取时差,并将其传递到world update方法参数中。但是,在更新位置的更新方法中,传入了一个大值(即使我已除以1000),因此位置向位置向量添加了一个奇数4000 代码如下: Simulation.CPP: int Simulation::simControlLogic(HWND hWnd, keyEvent event) { /* TO DO: add relevant
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
}
将之前的时间移动到模拟器构造函数并将其初始化为0后,它就工作了。感谢您的回复时差、帧开始时间和上一个时间成员/变量的类型是什么?初始化的时间、地点和时间是什么?
previousTime
在第一帧中正确初始化勾号计数吗?如果不是,它可能被设置为零,导致在第一次迭代中出现较大的时间步长。顺便说一句:这与您的问题没有直接关系:时间差的单位似乎是1秒,因为GetTickCount
返回以毫秒为单位的运行时间,然后将timeDifference
除以1000。您将previousTime
设置为0,然后将其设置为frameStartTime
,因此时差
为0…将其初始化为GetTickCount()
更简单。