2010-04-08 169 views
1

在C++ OpenGL中製作一個小型Pong遊戲時,我決定當東西彈跳時創建弧線(半圓形)會很有趣。我決定暫時跳過貝塞爾曲線,直接用直的代數去走,但是我沒有走得很遠。我的代數遵循一個簡單的二次函數(y = + - sqrt(mx + c))。如何在OpenGL中繪製弧線

這個小節選僅僅是一個我尚未完全參數化的例子,我只是想看看它的外觀。然而,當我畫這個時,它給了我一條垂直線,直線的切線接近-1.0/1.0。

這是GL_LINE_STRIP樣式的限制還是有更簡單的方法繪製半圓/弧?還是我完全錯過了一些明顯的東西?

void Ball::drawBounce() 
{ float piecesToDraw = 100.0f; 
    float arcWidth = 10.0f; 
    float arcAngle = 4.0f; 

    glBegin(GL_LINE_STRIP); 
     for (float i = 0.0f; i < piecesToDraw; i += 1.0f) // Positive Half 
     { float currentX = (i/piecesToDraw) * arcWidth; 
      glVertex2f(currentX, sqrtf((-currentX * arcAngle)+ arcWidth)); 
     } 
     for (float j = piecesToDraw; j > 0.0f; j -= 1.0f) // Negative half (go backwards in X direction now) 
     { float currentX = (j/piecesToDraw) * arcWidth; 
      glVertex2f(currentX, -sqrtf((-currentX * arcAngle) + arcWidth)); 
     } 
    glEnd(); 
} 

在此先感謝。

回答

2

什麼是sqrtf(( - currentX * arcAngle)+ arcWidth)?當我> 25時,那個表達就變成想象。這樣做的正確方法是使用sin()/ cos()爲您的問題中所述的半圓生成X和Y座標。如果你想用一個拋物線來代替,清潔方法是計算Y = H-H(X/W)^ 2

+0

呀,有個白癡。不知道爲什麼我決定使用拋物線,而不是回想起記住我的單位圈子。 我想我掛斷了爲什麼有一條垂直線穿過我的線條畫。 謝謝 – rpgFANATIC 2010-04-08 22:40:04