2017-01-02 31 views
0

在OpenGL中,我正在構建一個足球遊戲,通過在按下按鈕時基於指標進行拍攝之前,通過左右第一個移動高度指示器拍攝球。這裏是什麼樣子:OpenGL - 爲什麼我的球沒有移動?

Football Game Visual

當這些指標被移動,我的球需要在垂直指標(Y)的高度去旅遊了,向左或向右的方向,如果縱向一(X)。

首先,這裏有一個移動我的指標(這只是紋理正在我RenderScene()功能繪製)

void SpecialKeys(int key, int x, int y){ 

if (key == GLUT_KEY_RIGHT) {  // moves the bottom indicator RIGHT 
     horizontalBarX += 5.0f; 
    } 

    if (key == GLUT_KEY_LEFT) { 
     horizontalBarX -= 5.0f; // moves the bottom indicator LEFT 
    } 

    if (key == GLUT_KEY_UP) { // moves the top indicator UP 
     verticalBarY += 5.0f; 
     verticalBarX += 1.0f; 
    } 

    if (key == GLUT_KEY_DOWN) { // moves the top indicator DOWN 
     verticalBarY -= 5.0f; 
     verticalBarX -= 1.0f; 
    } 
} 

計算爲我的足球運動

現在讓我的足球代碼移動指標後,我需要將以下計算應用於x,yz軸的球:

X = SIN(THETA)* COS(PHI)Y = COS(THETA)* SIN(PHI)Z = COS(THETA)

其中θ=在ZX角度,和phi =在ZY

所以用這個,我試圖首先得到兩theta和披角的值,通過簡單地增加他們根據您在SpecialKeys()功能時按什麼高度指標:

void SpecialKeys(int key, int x, int y){ 

if (key == GLUT_KEY_RIGHT) {  // moves the bottom indicator RIGHT 
     horizontalBarX += 5.0f; 
     theta += 5; // Increase theta angle by 5 
    } 

    if (key == GLUT_KEY_LEFT) { 
     horizontalBarX -= 5.0f; // moves the bottom indicator LEFT 
     theta -= 5; // Decrease theta angle by 5 
    } 

    if (key == GLUT_KEY_UP) { // moves the top indicator UP 
     verticalBarY += 5.0f; 
     verticalBarX += 1.0f; 
     phi += 5; // Increase theta angle by 5 
    } 

    if (key == GLUT_KEY_DOWN) { // moves the top indicator DOWN 
     verticalBarY -= 5.0f; 
     verticalBarX -= 1.0f; 
     phi -= 5; // Decrease phi angle by 5 
    } 
} 

現在,我有角度,我想插入計算d值代入drawFootball()的參數,它的方式是最初叫我RenderScene功能...

drawFootBall(0, 40, 500, 50); // x,.y, z, r 

...這是我怎樣,我試圖與上述計算啓動球:

void SpecialKeys(int key, int x, int y){ 

    // indicator if statements just above this line 

    if (key == GLUT_KEY_F1) { 
     drawFootBall(sin(theta)*cos(phi), cos(theta)*sin(phi), cos(theta), 50); 
    } 
} 

但是當我去點擊啓動按鈕F1什麼都不會發生在所有。我在哪裏搞砸了?

編輯:

如果有幫助,這是我drawFootball()功能:

void drawFootBall(GLfloat x, GLfloat y, GLfloat z, GLfloat r) 
{ 
    glPushMatrix(); 
    glFrontFace(GL_CCW); 
    glTranslatef(x,y,z); 
    //create ball texture 
    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_BALL]); 
    //glDisable(GL_LIGHTING); 
    glColor3f(0.5,0.5,0.5); 
    quadricFootball = gluNewQuadric(); 
    gluQuadricDrawStyle(quadricFootball, GLU_FILL); 
    gluQuadricNormals(quadricFootball, GLU_SMOOTH); 
    gluQuadricOrientation(quadricFootball, GLU_OUTSIDE); 
    gluQuadricTexture(quadricFootball, GL_TRUE); 
    gluSphere(quadricFootball, r, 85, 50); 
    glDisable(GL_TEXTURE_2D); 
    glPopMatrix(); 
} 
+0

在看不到drawFootBall()所做的事情的情況下,很難爲您提供幫助。 – Frank

+0

嗨@Frank,我爲你編輯了我的問題,展示了我的drawFootball()函數。 –

+0

你的角度應該是弧度,而不是度數。五弧度幾乎是360度。 – molbdnilo

回答

0

首先確保你的thetaphi是在正確的單位,即弧度。如果以度爲單位將它們轉換爲弧度,則使用sin(theta * PI/180.0f)等,假設PI已定義。

我相信你在計算有一個球的方向矢量。 d(x,y,z)是球行進的方向,(假設沒有重力或其他力)。它可能是球被踢的方向。

我想如果你只是想移動你的球,你需要乘以這個方向的長度。由於您的球的半徑爲50個單位,請嘗試將該半徑翻譯爲2倍。

glTranslatef(2.0f*r*x,2.0f*r*y,2.0f*r*z); 

這會將球移動到其所需方向的半徑的2倍。 但是,你可能想要讓一些物理學有一個更現實的運動。

+0

感謝您的回答,這是有道理的。只是幾個簡單的問題。有了這個glTranslate,我在哪個函數中放置了這個(我假設我的RenderScene函數,但只是想確定)。另外,我是否基本上將每次計算都插入一個x,y,z變量並在glTranslate中使用它們? –

+0

另外,只有在按F1鍵的情況下,我怎樣才能使這種翻譯發生? –

+0

理想情況下,您可以在每幀畫出球而不是僅在按F1時畫出球。只需將翻譯存儲在一個變量中並相應增加或使用標誌並僅在檢查標誌時進行翻譯。 – Harish

相關問題