1、绘制飞机方法draw_plane()
void draw_plane()
{
float scale = 1.0f;
glLineWidth(6.0f);
glColor3f(1.0, 0.0, 1.0);
glPushMatrix();
glScaled(0.75f * scale, 0.75f * scale, 0.75f * scale);
glPushMatrix();
glRotated(90.0f, 0.0f, 1.0f, 0.0f);
glScaled(1.0f, 1.0f, 1.3f);
glutSolidSphere(0.25f, 5, 5); // 机身
glPopMatrix();
glColor3f(1.0, 0.0, 0.0);
glPushMatrix();
glTranslated(0.0f, -0.24f, 0.0f);
glRotated(90.0f, 0.0f, 1.0f, 0.0f);
glScaled(1.0f, 1.0f, 5.0f);
glTranslated(0.2f, 0.0f, 0.0f);
glutSolidSphere(0.07f, 3, 3); // 脚架
glTranslated(-0.4f, 0.0f, 0.0f);
glutSolidSphere(0.07f, 3, 3);
glPopMatrix();
glColor3f(0.0, 0.0, 1.0);
glPushMatrix();
glTranslated(0.0f, 0.2f, 0.0f);
glRotated(-90.0f, 1.0f, 0.0f, 0.0f);
glutSolidCone(0.04, 0.16, 4, 1); // 旋翼轴
glPopMatrix();
glPushMatrix();
glTranslated(0.0f, 0.4f, 0.0f);
glRotated(90.0f, 1.0f, 0.0f, 0.0f);
glutSolidCone(0.4, 0.05, 16, 1); // 旋翼
glPopMatrix();
glColor3f(1.0, 1.0, 0.0);
glPushMatrix();
glRotated(-90.0f, 0.0f, 1.0f, 0.0f);
glTranslated(0.0f, 0.05f, 0.29f);
glRotated(-15.0f, 1.0f, 0.0f, 0.0f);
glutSolidCone(0.05, 0.3, 4, 1); // 机尾
glColor3f(0.0, 0.0, 1.0);
glTranslated(0.03f, 0.0f, 0.3f);
glRotated(90.0f, 0.0f, 1.0f, 0.0f);
glutSolidCone(0.15, 0.0, 8, 1); // 尾旋翼
glPopMatrix();
glPopMatrix();
}
2、更新飞机位置和方向方法update_plane()
全局变量:
float angle = -90;//角度
int i = 0;//修正飞机方向
bool isPause = false;//暂停标志
void update_plane()
{
glPushMatrix();
glTranslatef(1.5*cos(angle), 0, 1.5*sin(angle));
//printf("%f,%d;", -atan((1.5*sin(angle)) / (1.5*cos(angle))) * 180 / 3.141592654,i);
if (!isPause)
{
if (-atan((1.5*sin(angle)) / (1.5*cos(angle))) * 180 / 3.141592654 < -89.95)
i++;
if (i % 2 == 0)
glRotated(-atan((1.5*sin(angle)) / (1.5*cos(angle))) * 180 / 3.141592654 - 90 + 180, 0.0f, 1.0f, 0.0f);
else
glRotated(-atan((1.5*sin(angle)) / (1.5*cos(angle))) * 180 / 3.141592654 - 90, 0.0f, 1.0f, 0.0f);
angle = angle + 0.001;
}
else
{
if (i % 2 == 0)
glRotated(-atan((1.5*sin(angle)) / (1.5*cos(angle))) * 180 / 3.141592654 - 90 + 180, 0.0f, 1.0f, 0.0f);
else
glRotated(-atan((1.5*sin(angle)) / (1.5*cos(angle))) * 180 / 3.141592654 - 90, 0.0f, 1.0f, 0.0f);
}
draw_plane();
glPopMatrix();
}
3、另一种方案采用向量法,待定。