Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.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
Opengl 将汽车直线移动_Opengl_Graphics_Glut - Fatal编程技术网

Opengl 将汽车直线移动

Opengl 将汽车直线移动,opengl,graphics,glut,Opengl,Graphics,Glut,我正试着让汽车直线行驶。 我得到的是这条线的起点和终点 这就是我到目前为止所尝试的 glTranslated(x, y, z); glRotated(angle * 180 / PI, 0, 1, 0); glColor3d(0.1, 0.1, 0.4); glutSolidCube(40); // imagine that this is a car x += speed * sin(angle + PI/2); z += speed * cos(angle + PI/2); 该角度由

我正试着让汽车直线行驶。 我得到的是这条线的起点和终点

这就是我到目前为止所尝试的

glTranslated(x, y, z);
glRotated(angle * 180 / PI, 0, 1, 0);

glColor3d(0.1, 0.1, 0.4);
glutSolidCube(40); // imagine that this is a car

x += speed * sin(angle + PI/2);
z += speed * cos(angle + PI/2);
该角度由

double dist = sqrt((double)(x2-x1)*(x2-x1) + (double)(y2-y1)*(y2-y1));
int deltaX = abs(x2-x1);

angle = PI - acos(deltaX/dist)
如果点1位于点2的左侧,并且只有一辆车在移动,则此解决方案偶尔有效。 如果我试图同时移动几辆车,其中只有一辆车按预期移动。然而,其他人似乎有自己的意愿

所以

我需要做什么才能让所有的车一起行驶? 我应该如何正确计算角度?不管起点在哪里 重要注意事项:汽车在平坦的xz平面y=0上移动

编辑:

这是我目前的汽车课

#include "glut.h"
#include <math.h>
#include "Car.h"    

Car::Car(double x, double y, double z, double speed, double angle)
{
    this->x = x;
    this->y = y;
    this->z = z;
    this->speed = speed;
    this->angle = angle;
}        

void Car::draw3D()
{
    glTranslated(x, y, z);
    glRotated(angle * 180 / 3.14, 0, 1, 0);

    glColor3d(0.1, 0.1, 0.4);
    glutSolidCube(40);

    x += speed * sin(angle + 1.57);
    z += speed * cos(angle + 1.57);
}
这就是我建造汽车的方法

for(vector<Road*>::iterator it = roads.begin(); it != roads.end(); ++it) {
    int x1 = (*it)->x1;
    int y1 = (*it)->y1;
    int x2 = (*it)->x2;
    int y2 = (*it)->y2;    

    double mangle = -atan2((double)y2-y1, (double)x2-x1);
    cars.push_back(new Car(x1-GRID_SIZE/2, 0, y1-GRID_SIZE/2, 5, mangle));          
}
这就是我画所有汽车的方式

for(vector<Car*>::iterator it = cars.begin(); it != cars.end(); ++it) {
    (*it)->draw3D();
}

对于角度,它可能更容易使用

angle = atan2(y2-y1, x2-x1);
不仅计算向量的反正切,而且在角度不确定的情况下处理恶劣的情况

请记住,余弦/正弦是周期pi的周期,因此仅使用delta值将产生两个可能的角度,即1,1和-1,-1都将产生45°的角度


也许这确实解决了你们的车不在一起开的问题。如果没有,请检查每辆车是否都有自己的角度。

使用此功能,我可以在每个可能的方向上一次移动一辆车,因此非常感谢,但我仍然无法同时移动多辆车。是的,每辆车都有自己的角度。我还必须使用角度=-atan2y2-y1,x2-x1;你为什么不能移动几辆车?当你试着去做的时候会发生什么?你打算怎么做呢。其中一辆车朝着正确的方向行驶,而其他车则朝着不同的方向行驶。2.我有一个car对象的向量,每个对象用我问题中的第一个代码片段绘制自己。你能添加car类的全部代码和绘图函数的示例函数调用吗?我现在可以随便猜一猜。在draw3d的开始部分,放置glPushMatrix;,在最后,单击“矩阵”;。然后再试一次。我也这么想。现在它开始工作了。谢谢你的帮助,很好。最后一件事:GLUT是过时的,你可以考虑使用。