1

我有一个小样本模拟,把它想象成把球扔到空中。我希望能够“加速”模拟,因此它将在更少的循环中完成,但“球”仍然会像以正常速度(1.0f)一样高地飞到空中。

现在,模拟在更少的迭代中完成,但是球的坐标要么太高要么太低。这里有什么问题?

static void Test()
{
    float scale = 2.0f;
    float mom = 100 * scale;
    float grav = 0.01f * scale;
    float pos = 0.0f;

    int i;
    for (i = 0; i < 10000; i++)
    {
        if (i == (int)(5000 / scale)) // Random sampling of a point in time
            printf("Pos is %f\n", pos);

        mom -= grav;
        pos += mom;
    }
}
4

1 回答 1

1

'scale' 是您试图用来更改时间步长大小的变量吗?

如果是这样,它应该会影响 mom 和 pos 的更新方式。所以你可以从更换开始

mom -= grav;
pos += mom;

mom -= grav*scale;
pos += mom*scale;

也许这点伪代码有帮助..

const float timestep = 0.01; // this is how much time passes every iteration
                             // if it is too high, your simulation
                             // may be inaccurate! If it is too low, 
                             // your simulation will run unnecessarily
                             // slow!

float x=0; //this is a variable that changes over time during your sim.
float t=0.0; // this is the current time in your simulation

for(t=0;t<length_of_simulation;t+=timestep) {
    x += [[insert how x changes here]] * timestep;
}
于 2013-10-14T20:17:09.373 回答