Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用opengl模拟一辆汽车超越另一辆汽车_C++_Animation - Fatal编程技术网

C++ 用opengl模拟一辆汽车超越另一辆汽车

C++ 用opengl模拟一辆汽车超越另一辆汽车,c++,animation,C++,Animation,我想用opengl模拟两辆汽车在街上行驶。为此,我选择将每辆车表示为一个矩形(蓝色-慢速车,红色-超越另一辆车) 我有以下代码: 主要功能 { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(800, 600); glutCreateWindow("

我想用opengl模拟两辆汽车在街上行驶。为此,我选择将每辆车表示为一个矩形(蓝色-慢速车,红色-超越另一辆车)

我有以下代码:

主要功能

{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(800, 600);
    glutCreateWindow("cars");
    glewInit();
    Initialize();
    glutDisplayFunc(RenderFunction);
    glutIdleFunc(idle); 
    glutCloseFunc(Cleanup);
    glutMainLoop();
}
void RenderFunction(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    // slow rectangle
    {
        myMatrix = glm::mat4(1.f); // identity matrix 4x4
        matrTransl1 = glm::translate(glm::mat4(1.0f), glm::vec3(x1, 0.0f, 0.0f)); //translate along x1
        myMatrix = matrTransl1 * glm::mat4(1.f);
        myMatrixLocation = glGetUniformLocation(ProgramId, "myMatrix");
        glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &myMatrix[0][0]);
        codCol = 0;
        codColLocation = glGetUniformLocation(ProgramId, "codCol");
        glUniform1i(codColLocation, codCol);
        glDrawArrays(GL_QUADS, 0, 4);
    }
    
    // "faster" rectangle
    {
        matrTransl1 = glm::translate(glm::mat4(1.0f), glm::vec3(x2, yy, 0.0f));
        myMatrix = matrTransl1 * glm::mat4(1.f);
        myMatrixLocation = glGetUniformLocation(ProgramId, "myMatrix");
        glUniformMatrix4fv(myMatrixLocation, 1, GL_FALSE, &myMatrix[0][0]);
        codCol = 1;
        codColLocation = glGetUniformLocation(ProgramId, "codCol");
        glUniform1i(codColLocation, codCol);
        glDrawArrays(GL_QUADS, 0, 4);
    }
glFlush();
}
空闲功能

float x1 = -1., x2 = -2., yy = 0.; 
//x1 is the x coordinate of the slow car, x2 and yy for the overtaking car
void idle()
{
    if (x1 <= 2) //stil in the window, slowly moving the rectangle
    {
        x1 += 0.0001;
        if (x1 < -0.25 && x1 > 0.25)
            yy = 0;
        //here i wanted the overtaking to take place, when the x of the slow car is in [-0.25; 0.25]
        else if (x1 > -0.25 && x1 < 0.25) 
            yy = 0.5; // i make the 2nd car go up
    }
    else //go back to window
        x1 = -1.5;

    if (x2 <= 2)
        x2 += 0.0004;
    else
        x2 = -1.5;

    glutPostRedisplay();
}

我的主要问题是我真的不明白红色矩形何时/如何上升,一旦上升就永远不会下降。。关于我应该看什么有什么提示吗?

这是因为你的条件是错误的:
如果(x1<-0.25&&x1>0.25)
是逻辑错误的陈述。它永远无法评估为真

您可以在此处将
&
替换为
|
,但这样做会在逻辑中为
x1
的值创建一个间隙,该值正好是-0.25或0.25。因为这两条语句都不能处理这个问题,但可能会给人这样做的印象,所以这种方法有一天会给您带来一个bug

更不用说,这样做也会复制常量,使代码更难阅读和维护

以下任何一项都可以:

if (x1 > -0.25 && x1 < 0.25) 
    yy = 0.5;
else
    yy = 0.0;
if(x1>-0.25&&x1<0.25)
yy=0.5;
其他的
yy=0.0;
或:

yy=0.0;
如果(x1>0.25&&x1<0.25)
yy=0.5;

非常感谢!它的行为更接近我想要做的事情。你能告诉我如何让超车“真实”吗?我的猜测是,我必须找出何时我必须更改yy值,而不是x1在[-0.25;0.25]时,而是x1大于/小于x2时?尽量避免提出新问题作为对原始问题答案的评论。为了让你放松一下,你需要选择汽车从一个位置移动到下一个位置的时间间隔。现在,你的车在一瞬间移动得非常快。相反,想想“我的传球动作需要多长时间?”然后从那里开始。您可能希望通过逐步计算进行更改,或者您甚至可以选择将移动表示为该范围内
x1
的数学函数(例如二次插值)。最好是尝试一下,看看会发生什么。我会记住的!
yy = 0.0;
if (x1 > -0.25 && x1 < 0.25) 
    yy = 0.5;