繁体   English   中英

OpenGL-为什么我的球不动?

[英]OpenGL - Why doesn't my ball move?

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

我的足球运动的计算

现在,要让我的足球在指示器移动之后移动,我需要对球的xyz轴应用以下计算:

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

其中theta = zx的角度,phi = zy的角度

因此,我尝试首先根据您在SpecialKeys()函数中按下的高度指示符简单地增加它们的角度,从而首先获取theta和phi角度的值:

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

现在我有了角度,我想将计算出的值插入到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();
}

首先确保您的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倍。 但是,您可能需要一些物理学来使运动更逼真。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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