In OpenGL, baue ich ein Fußballspiel, mit dem Sie einen Ball schießen können, indem Sie zuerst Höhenanzeiger nach links und rechts bewegen, bevor Sie basierend auf den Anzeigen, wenn eine Taste gedrückt wird, schießen. Hier ist, wie es aussieht:OpenGL - Warum bewegt sich mein Ball nicht?
Wenn diese Indikatoren bewegen, mein Ball muss auf der Höhe der vertikalen Anzeige (y), und nach links oder rechts Richtung, wenn die vertikale (x) reisen.
Erstens, hier ist der Code, der meine Indikatoren bewegt (die in meiner RenderScene()
Funktion gerade gezogen Texturen werden)
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;
}
}
Berechnungen für meinen Fußball
Jetzt zu bewegen meinen Fußball zu erhalten, bewegen, nachdem die Indikatoren bewegt wurden, muss ich die folgenden Berechnungen auf die x
, y
und z
Achse des Balls anwenden:
x = sin (theta) * cos (phi) y = cos (theta) * sin (phi) z = cos (theta)
wobei Theta = Winkel in zx und phi = Winkel in zy
Also mit diesem, habe ich versucht, zuerst die Werte beider theta und phi-Winkel zu bekommen, indem man sie einfach erhöht wird, je nachdem, welche Höhe Indikatoren, die Sie in der SpecialKeys()
Funktion gedrückt haben:
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
}
}
Jetzt, Ich habe die Winkel, ich will das berechnen einstecken d-Werte in die drawFootball() Parameter, die durch die Art und Weise zunächst in meiner RenderScene Funktion als ...
drawFootBall(0, 40, 500, 50); // x,.y, z, r
genannt wird ... und hier sind, wie ich bin versucht, den Ball mit den Berechnungen zu starten oben:
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);
}
}
Aber wenn ich gehen die Start-Taste F1 klicken, passiert nichts bei allen. Wo habe ich versagt?
EDIT:
Wenn es hilft, ist hier meine drawFootball()
Funktion:
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();
}
Es wird sehr schwer sein, Ihnen zu helfen, ohne zu sehen, was drawFootBall() tut. – Frank
Hallo @Frank, Ich habe meine Frage für Sie mit meiner Funktion drawFootball() bearbeitet. –
Ihre Winkel sollten im Bogenmaß und nicht in Grad angegeben werden. Fünf Radianten sind fast 360 Grad. – molbdnilo