2015-11-13 33 views
1

當一個點到達底部時,我試圖將其反彈。不幸的是,它只是不斷下降。這裏是代碼:在動畫中需要幫助Opengl c/C++

void Timer(int value) { 
    if (value) { 
     if(initMpoints[1][1] <= 500 && initMpoints[0][1] >= 0) { 
      txForC -= 0; 
      tyForC -= 5;   //Move C downward. 
      txForM -= 0; 
      tyForM -= 5; 
      if (initMpoints[0][1] == 0) { 
       txForC += 0; 
       tyForC += 5;   //Move C downward. 
       txForM += 0; 
       tyForM += 5; 
      } 
     } 

    } 
    glutPostRedisplay(); 
    glutTimerFunc(50, Timer, value); 
} 

我該如何解決這個問題?

+0

這裏的代碼沒有完全做到。請告訴我,如果我從一開始就做了什麼。提前致謝。 –

+3

看起來是一個很好的機會[學習如何使用調試器](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –

+1

你能發佈一個更有意義的代碼片段嗎?爲什麼在initMpoints [0] [1] == 0時撤消對'txForC'等的修改,而不是簡單地從第一個'if()'語句中排除這個條件?這些變量代表什麼?你的代碼是如何知道一個點到底的?你是否檢查過了底部位置會發生什麼? –

回答

0

反彈胺化的很好和更自然的例子可以通過使用abs(sin(x))函數完成。

enter image description here

你的情況

我會重寫你的函數是這樣的。

// Speed of bouncing 
float fSpeed = 0.01f; 
float fPI = 3.1415926f; 

// Height of bouncing 
float fHeight = 100.0f; 

void Timer(int iValue) 
{ 
    static float fAngle = 0.0; 

    // if animation enabled 
    if (iValue) 
    { 
     tyForC = fHeight * abs(sin(fAngle)); 
     tyForM = fHeight * abs(sin(fAngle)); 

     // Go to next position 
     fAngle += fSpeed; 
    } 

    // Restore previous value to avoid bitgrid overflow 
    if (fAngle >= (4.0f * fPI - fSpeed/2.0)) 
    { 
     fAngle = 0.0f; 
    } 

    glutPostRedisplay(); 
    glutTimerFunc(50, Timer, iValue); 
} 

像你的情況動畫牙齒的代碼會像

float fHeight = 100.0f; 

void Timer(int iValue) 
{ 
    static float deltaC = 5.0f; 
    static float deltaM = 5.0f; 

    if (iValue) 
    { 
     tyForC += deltaC; 
     tyForM += deltaM; 

     if (tyForC <= 0 || tyForC >= fHeight) 
      deltaC *= -1.0f; 

     if (tyForM <= 0 || tyForM >= fHeight) 
      deltaM *= -1.0f; 
    } 

    glutPostRedisplay(); 
    glutTimerFunc(50, Timer, iValue); 
} 
+0

@MC Mad Moefat:我回答你的問題了嗎? – Mykola

+0

謝謝你真的幫助了...如果我的編程不好,我很抱歉。但仍在研究。謝謝。 –