[英]Properly scale simulation by time
I have a small sample simulation, think of it like tossing a ball into the air. 我有一个小样本模拟,可以想像是将球抛向空中。 I want to be able to 'speed up' the simulation, so it will complete in a fewer # of loops, but the 'ball' will still go just as high in the air as if it is at normal speed (1.0f).
我希望能够“加速”仿真,因此它将以较少的循环次数完成,但“球”仍会像在正常速度(1.0f)时一样高高地飞行。
Right now, the simulation completes in a fewer # of iterations, but the coordinates of the ball are either too high or too low. 目前,模拟以较少的迭代次数完成,但是球的坐标太高或太低。 What's wrong here?
怎么了
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;
}
}
Is 'scale' the variable you are trying to use to change the time-step size? “ scale”是您要用来更改时间步长的变量吗?
If so, it should affect how mom and pos are updated. 如果是这样,它将影响对mom和pos进行更新的方式。 So you might start by replacing
所以您可以先更换
mom -= grav;
pos += mom;
with 同
mom -= grav*scale;
pos += mom*scale;
Maybe this bit of pseudocode helps.. 也许这部分伪代码会有所帮助。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.