float carX = 0.0;
float carY = 0.0;
float carSpeed = 1.0;
float direction = 0.0;
bool exhaust;
void reshape(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 10.0, 0, 10.0, 0.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
void keys(int key, int x, int y){
if(key == GLUT_KEY_UP){
carY = carY + carSpeed;
direction = 0.0;
glutPostRedisplay();
}
if(key == GLUT_KEY_DOWN){
carY = carY - carSpeed;
direction = 90.0;
glutPostRedisplay();
}
if(key == GLUT_KEY_LEFT){
carX = carX - carSpeed;
direction = -90.0;
glutPostRedisplay();
}
if(key == GLUT_KEY_RIGHT){
carX = carX + carSpeed;
direction = 180.0;
glutPostRedisplay();
}}
void renderScene(void){
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
drawPlayground();
glPushMatrix();
glRotatef(direction, 0.0, 0.0, 1.0);
glTranslatef(carX, carY,0.0);
drawcar();
glPopMatrix();
glFlush();}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (400, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow("courseWork_2DGame");
reshape();
glutDisplayFunc(renderScene);
glutSpecialFunc(keys);
glutMainLoop();
return 0;}
这是我的openGL代码,我画了汽车移动的操场。小车将被移动但箭头,并且随着它移动的方向是根据按下的箭头键小车的旋转。但是,当按下按键时,汽车可以正常平移,但旋转不正确,我认为是因为它没有在其中心旋转,而是在不同的点上旋转。