简体   繁体   中英

OpenGL - Why doesn't my ball move?

In OpenGL, I'm building a football game that allows you to shoot a ball by first moving height indicators left and right, before shooting based on the indicators when a button is pressed. Here's what it looks like:

Football Game Visual

When these indicators are moved, my ball needs to travel at the height of the vertical indicator (y), and left or right direction if the vertical one (x).

Firstly, here's the code that moves my indicators (which are just textures being drawn in my RenderScene() function)

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;
    }
}

Calculations for my football to move

Now to get my football to move after the indicators have be moved, I need to apply the following calculations to the x , y and z axis of the ball:

x = sin(theta) * cos (phi) y = cos(theta) * sin(phi) z = cos(theta)

where theta = angle in zx, and phi = angle in zy

So with this, I have attempted to get the values of both theta and phi angles first, by simply incrementing them depending on what height indicators you have pressed in the SpecialKeys() function:

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
    }
}

Now that I have the angles, I want to plug in the calculated values into the drawFootball() parameters, which by the way is initially called in my RenderScene function as...

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

...and here's how I'm attempting to launch the ball with the calculations above:

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);
    }
}

But when I go to click the launch button F1 , nothing happens at all . Where have I messed up?

EDIT:

If it helps, here's my drawFootball() function:

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();
}

Firstly make sure your theta and phi are in the right units ie Radians. If in degrees convert them to radians by using sin(theta * PI/180.0f) and so on, assuming PI is defined.

I believe what you are computing there is a direction vector for the Ball. The d(x,y,z) is the direction in which the ball should travel, (assuming there is no gravity or other forces). Its probably the direction in which the ball is kicked.

I think if you simply wanted to move your ball, you need to multiply this direction with a length. Since your ball has a radius of 50 units, try translating to 2 times this radius.

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

This will move the ball to 2 times its radius in your desired direction. However you probably want to have some physics to have a more realistic movement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM