gpt4 book ai didi

c - 按时间适当缩放模拟

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:53 25 4
gpt4 key购买 nike

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

最佳答案

“比例”是您尝试用来更改时间步长的变量吗?

如果是这样,它应该会影响 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;
}

关于c - 按时间适当缩放模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19368376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com