2013-10-14 143 views
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; 
    } 
} 

回答

1

'scale'是您試圖用來更改時間步長大小的變量嗎?

如果是這樣,它應該影響如何更新媽媽和pos。所以,你可能用

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

更換

mom -= grav; 
pos += mom; 

如果我做你的建議的更改開始也許僞代碼的此位可以幫助..

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; 
} 
+0

(*爲規模GRAV都和媽媽) ,如果我使用1.0的縮放比例,我會得到一個不同的pos值,而我使用2.0的縮放比例。不僅稍微偏離(這是預期的),但是pos增加了一倍。 – user1054922

+0

啊哈!我不需要縮放初始值,只需在循環內添加即可。謝謝! – user1054922