【问题标题】:Properly scale simulation by time按时间适当缩放模拟
【发布时间】:2013-10-14 20:05:59
【问题描述】:

我有一个小样本模拟,把它想象成向空中抛球。我希望能够“加速”模拟,因此它将在更少的循环中完成,但“球”仍然会像以正常速度(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;
    }
}

【问题讨论】:

    标签: c simulation


    【解决方案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;
    }
    

    【讨论】:

    • 如果我做出您建议的更改(* grav 和 mom 的比例),如果我使用 1.0 的比例,我会为 pos 打印不同的值,我使用 2.0 的比例。不仅略微偏离(这是意料之中的),而且 pos 翻了一番。
    • 啊哈!我不必缩放初始值,只需在循环内添加。谢谢!
    猜你喜欢
    • 2019-09-05
    • 2020-03-27
    • 2021-09-29
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多