在OpenGL中,我正在構建一個足球遊戲,通過在按下按鈕時基於指標進行拍攝之前,通過左右第一個移動高度指示器拍攝球。這裏是什麼樣子:OpenGL - 爲什麼我的球沒有移動?
當這些指標被移動,我的球需要在垂直指標(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
,y
和z
軸的球:
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();
}
在看不到drawFootBall()所做的事情的情況下,很難爲您提供幫助。 – Frank
嗨@Frank,我爲你編輯了我的問題,展示了我的drawFootball()函數。 –
你的角度應該是弧度,而不是度數。五弧度幾乎是360度。 – molbdnilo