lundi 23 novembre 2020

rotate 2d drawing car by pressing keyboard input

I have finally got my 2d car moving using the up arrow key, down arrow key, right arrow key and left arrow key, now I am trying to rotate my 2d car by using the keyboard again, for example I want to be able to press the r keyboard and get the car to rotate but I don't know how to. I have tried to look for examples about rotating using keyboard, it will be helpful if you can show me which examples is more suitable for me.

int Wwidth;
int Wheight;

int xOffset = 0, yOffset = 0;   //initializes the amount to move the car at 0
float angle = 0.0;      //the angle that the car will be rotated

bool goingUp = false;
bool goingDown = false;
bool goingLeft = false;
bool goingRight = false;

void drawCar(float length, float x, float y, int xOffset, int yOffset, float angle)
{

glPushMatrix();
glLoadIdentity();
glTranslatef(xOffset, yOffset, 0);
glTranslatef(carX_CENTRE, carY_CENTRE, 0.0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-carX_CENTRE, -carY_CENTRE, 0.0);
glBegin(GL_POLYGON);
glColor3f(0.8, 1.0, 0.8);
glVertex2f(carX_CENTRE - LENGTH / 2, carY_CENTRE - LENGTH / 2);   //these 4 points draw the middle of the car
glVertex2f(carX_CENTRE - LENGTH / 2, carY_CENTRE + LENGTH / 2);  //these 4 points draw the middle of the car
glVertex2f(carX_CENTRE + 20, carY_CENTRE + LENGTH / 2);  //these 4 points draw the middle of the car
glVertex2f(carX_CENTRE + 20, carY_CENTRE - LENGTH / 2);  //these 4 points draw the middle of the car
glEnd();
glPopMatrix();
glFlush();

glPushMatrix();
glLoadIdentity();
glTranslatef(xOffset, yOffset, 0);
glTranslatef(carX_CENTRE, carY_CENTRE, 0.0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-carX_CENTRE, -carY_CENTRE, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(carX_CENTRE + 20, carY_CENTRE - LENGTH / 2);  //these 4 points draw the front of the car
glVertex2f(carX_CENTRE + 20, carY_CENTRE + LENGTH / 2);  //these 4 points draw the front of the car
glVertex2f(carX_CENTRE + 30, carY_CENTRE + LENGTH / 2);  //these 4 points draw the front of the car
glVertex2f(carX_CENTRE + 30, carY_CENTRE - LENGTH / 2);  //these 4 points draw the front of the car
glEnd();
glPopMatrix();
glFlush();

glPushMatrix();
glLoadIdentity();
glTranslatef(xOffset, yOffset, 0);
glTranslatef(carX_CENTRE, carY_CENTRE, 0.0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-carX_CENTRE, -carY_CENTRE, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(carX_CENTRE - 20, carY_CENTRE - LENGTH / 2);  //these 4 points draw the back of the car
glVertex2f(carX_CENTRE - 20, carY_CENTRE + LENGTH / 2);  //these 4 points draw the back of the car
glVertex2f(carX_CENTRE - LENGTH / 2, carY_CENTRE + LENGTH / 2);  //these 4 points draw the back of the car
glVertex2f(carX_CENTRE - LENGTH / 2, carY_CENTRE - LENGTH / 2);  //these 4 points draw the back of the car
glEnd();
glPopMatrix();


glFlush();

glPushMatrix();
glLoadIdentity();
glTranslatef(xOffset, yOffset, 0);
glTranslatef(carX_CENTRE, carY_CENTRE, 0.0);
glRotatef(angle, 0, 0, 1);
glTranslatef(-carX_CENTRE, -carY_CENTRE, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(carX_CENTRE - 28, carY_CENTRE - LENGTH / 2);  //these 4 points draw the exhaust
glVertex2f(carX_CENTRE - 28, carY_CENTRE - LENGTH / 2 + 3);  //these 4 points draw the exhaust
glVertex2f(carX_CENTRE - 20, carY_CENTRE - LENGTH / 2 + 3);  //these 4 points draw the exhaust
glVertex2f(carX_CENTRE - 20, carY_CENTRE - LENGTH / 2);  //these 4 points draw the exhaust
glEnd();
glPopMatrix();
glFlush();

struct collisionBox car;
car.x = carX_CENTRE + 30;
car.y = carY_CENTRE + LENGTH / 2;
car.w = 20;
car.h = 50;

}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);   /* clear window */
glLoadIdentity();

drawBoundary(170, 0, 0);

drawRedGate();

drawGreenGate();

drawCar(50, 0, 0, xOffset, yOffset, angle);

//drawParticles();


drawObstacle1();
drawObstacle2();
drawObstacle3();
drawObstacle4();
drawObstacle5();


glutSwapBuffers();


}

void specialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP: //when the up key is pressed
    yOffset = yOffset + 1;
    cout << y << endl;
    glutPostRedisplay();
    break;
case GLUT_KEY_DOWN: //when the down arrow key is pressed
    yOffset--;
    cout << y << endl;
    glutPostRedisplay();
    break;

case GLUT_KEY_LEFT: //when the left arrow key is pressed
    xOffset--;
    cout << x << endl;
    glutPostRedisplay();
    break;


case GLUT_KEY_RIGHT: //when the right arrow key is pressed
    xOffset++;
    cout << x << endl;
    glutPostRedisplay();
    break;

default:
    break;
}

}

GLvoid IdleFunc(GLvoid)
{
glutPostRedisplay();
}

/* graphics initialisation */
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);   /* window will be cleared to black */
}


int main(int argc, char* argv[])//standard c entry signature
{
cout << "Player Controls:\n Up Key - move forward\n Down Key - move backward\n Left/Right key - move left or right";
/* window management code ... */
/* initialises GLUT and processes any command line arguments */
glutInit(&argc, argv);
/* use double-buffered window and RGBA colour model */
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
/* window width = 400 pixels, height = 400 pixels */
glutInitWindowSize(500, 500);
/* window upper left corner at (100, 100) */
glutInitWindowPosition(100, 100);
/* creates an OpenGL window and set its title bar*/
glutCreateWindow("Coursework 1");
init();

glutDisplayFunc(display);


glutSpecialFunc(specialKeys);
//  glutSpecialUpFunc(keyGoneUp);

glutReshapeFunc(reshape);
//  SetupRC();
glutIdleFunc(IdleFunc);
glutMainLoop();



return 0;


}

Aucun commentaire:

Enregistrer un commentaire