### Problem with basic function

Hello, what i want to do is to make rotate a car and then accelerate to the direction he's facing after rotate.

What i'm doing is calculate what % of rotation it has (90 deg is 100
%, 0 deg is 0%, 45 is 50%) and then multiplicate that per 0.1 and per velocity.

MAIN
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125`` ``````#include #include #include #include "polygons.h" using namespace std; float selection = 2.0f, cameraheight = 0.0f, accept = 0.0f, Advance = 0.0f, angle = 0.0f, velocitat = 0.0f; void Exit_Func(){ if (selection == -2.0f){ if(accept == 1.0f){ exit(0); } } } void handleKeypress(unsigned char key, int x, int y) { switch (key) { case '1': cameraheight += 1.0f; break; case '2': cameraheight -= 1.0f; break; case 'w': selection += 2.0f; velocitat += 1.0f; break; case 'W': selection += 2.0f; break; velocitat += 1.0f; break; case 's': selection -= 2.0f; break; velocitat -= 1.0f; break; case 'S': selection -= 2.0f; break; velocitat -= 1.0f; break; case 'a': accept += 1.0f; angle += 1.0f; Exit_Func(); break; case 'A': accept += 1.0f; angle += 1.0f; Exit_Func(); break; case 'd': accept -= 1.0f; angle -= 1.0f; break; case 'D': accept -= 1.0f; angle -= 1.0f; break; } } //Initializes 3D rendering void initRendering() { glEnable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); } //Called when the window is resized void handleResize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (double)w / (double)h, 0.00001, 200000.0); } float direction = 7.0f; //Draws the 3D scene void drawScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //Ajusta la posició de la camera glTranslatef(accept * direction, 0.0f, -20.0f/*-7.5f*/ + cameraheight); glPushMatrix(); glRotatef(angle, 0.0f, 0.0f, 1.0f); glTranslatef(((90 * (angle * 100)) * velocitat), ((90 * (angle * 100)) * velocitat), 0.0f); Car_1(); glPopMatrix(); glutSwapBuffers(); } void update(int value) { //Selection loop in Menu_1 if (selection > 2) { selection = -2; } if (selection < -2) { selection = 2; } if (angle > 90) { angle == 0; } if (angle < 90) { angle == 90; } glutPostRedisplay(); glutTimerFunc(25, update, 0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(400, 400); glutCreateWindow("Treball de Recerca - Marc Colomé 2013"); initRendering(); glutDisplayFunc(drawScene); glutKeyboardFunc(handleKeypress); glutReshapeFunc(handleResize); glutTimerFunc(25, update, 0); glutMainLoop(); return 0; }``````

POLYGONS.H

 ``123456789101112`` ``````#include "glut.h" //Represents a Ferrari F40 void Car_1(void){ glBegin(GL_QUADS); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(2.25f, 1.0f, 0.0f); glVertex3f(2.25f, -1.0f, 0.0f); glVertex3f(-2.25f, -1.0f, 0.0f); glVertex3f(-2.25f, 1.0f, 0.0f); glEnd(); }``````

But it not works. How i could fix it?

I also pulled of the menu thing, i only have the car

Marc Colomé
Car has a position pos0 and direction dir0. Next moment, it has turned and then moved to new direction by X. New direction is dir1 and position is pos1.

Lets say that X0 is as long as X, but along x-axis.
X == dir1*X0.
pos1 == pos0 + X == pos0 + dir1*X0

That aside, do note that `glRotatef(a); glTranslatef(b);` is quite different from `glTranslatef(b); glRotatef(a);`. Furthermore, camera, world, and object coordinates are separate. There are plenty of ways to do the stuff in OpenGL. I've always got them wrong.
How you can implement then the Y-Axis?
Ahh, I was not precise.

X0 is a vector (x,0). "dir1" in the equation is rotation. It rotates vectors around the Z-axis. Your glRotate() is such rotation. X is a vector too (x',y).

The stuff that you do with angles in the glTranslate() is erroneous. X0 and X are 2D-vectors. "dir1" must be a 2x2 matrix. Look up wikipedia for rotations, or figure out which order of glRotate()s and glTranslate()s achieve the same, i.e. how glTranslate could remain (velocity, 0.0, 0.0).
I updated like this: Power is now a 2d vector (or array) and does the function of turning the tyres (actually it should be a "while left/right arrow is presed ... ")

It's more or less what you said but it not works. Can you see anything bad?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173`` ``````#include #include #include #include "polygons.h" using namespace std; float selection = 2.0f; float cameraheight = 0.0f; float accept = 0.0f; float Advance = 0.0f; float angle_1 = 0.0f; float velocitat = 0.0f; float xpos = 0.0f; float ypos = 0.0f; float power[2] = {0,0}; //2d vector for the car direction void Exit_Func(){ if (selection == -2.0f){ if(accept == 1.0f){ exit(0); } } } void handleKeypress(unsigned char key, int x, int y) { switch (key) { case '3': cameraheight += 1.0f; break; case '4': cameraheight -= 1.0f; break; case 'w': selection += 2.0f; velocitat += 1.0f; break; case 'W': selection += 2.0f; break; velocitat += 1.0f; break; case 's': selection -= 2.0f; break; velocitat -= 1.0f; break; case 'S': selection -= 2.0f; break; velocitat -= 1.0f; break; case 'a': accept += 1.0f; angle_1 += 1.0f; Exit_Func(); break; case 'A': accept += 1.0f; angle_1 += 1.0f; Exit_Func(); break; case 'd': accept -= 1.0f; angle_1 -= 1.0f; break; case 'D': accept -= 1.0f; angle_1 -= 1.0f; break; case '1': power[0] + 0.1f; //Turning right power[1] - 0.1f; break; case '2': power[1] + 0.1f; //Turning left power[0] - 0.1f; break; } } //Initializes 3D rendering void initRendering() { glEnable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); } //Called when the window is resized void handleResize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (double)w / (double)h, 0.00001, 200000.0); } //Draws the 3D scene void drawScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //Ajusta la posició de la camera glTranslatef(0.0f, 0.0f, -20.0f/*-7.5f*/ + cameraheight); glTranslatef(power[0], power[1], 0.0f); Car_1(); /*MENU glPushMatrix(); Menu_1(); glPopMatrix(); glPushMatrix(); Menu_2(); glPopMatrix(); glPushMatrix(); Menu_3(); glPopMatrix(); glPushMatrix(); Menu_4(); glPopMatrix(); glPushMatrix(); glTranslatef(0.0f, selection, 0.0f); Menu_Selection(); glPopMatrix(); MENU*/ glutSwapBuffers(); } void update(int value) { //Selection loop in Menu_1 if (selection > 2) { selection = -2; } if (selection < -2) { selection = 2; } if (angle_1 > 90) { angle_1 == 0; } if (angle_1 < 90) { angle_1 == 90; } if (power[1] < 0) { power[1] == 1; } if (power[1] > 1) { power[1] == 0; } if (power[2] < 0) { power[2] == 1; } if (power[2] > 1) { power[2] == 0; } glutPostRedisplay(); glutTimerFunc(25, update, 0); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(400, 400); glutCreateWindow("Treball de Recerca - Marc Colomé 2013"); initRendering(); glutDisplayFunc(drawScene); glutKeyboardFunc(handleKeypress); glutReshapeFunc(handleResize); glutTimerFunc(25, update, 0); glutMainLoop(); return 0; } ``````
Last edited on
Anyone?
bump!
Now i think this is the final work but it not works for some reason

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117`` ``````#include #include #include using namespace std; float _angle = 0.0f;//For rotating the car float xpos = 0.0f;//For moving the car in the X axis float ypos = 0.0f;//For moving the car in the Y axis float cameraheight = -20.0f;//For zoom or unzoom the camera //Called when a key is pressed void handleKeypress(unsigned char key, int x, int y) { switch (key) { case 27: //ESC exit(0); break; case 49: //Number 1 _angle += 1.0f; break; case 50: //Number 2 _angle -= 1.0f; break; case 51: //Number 3 cameraheight -= 5.0f; break; case 52: //Number 4 cameraheight += 5.0f; break; case 53: //Number 5 xpos += 1.0f; break; case 54: //Number 6 ypos += 1.0f; break; } } //Initializes 3D rendering void initRendering() { glEnable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); //Enable color glClearColor(0.2f, 0.2f, 0.2f, 1.0f); //Background color is sky blue } //Called when the window is resized void handleResize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, (double)w / (double)h, 0.01/*Min render distance*/, 1000.0/*Max distance*/);//Meters } //Draws the 3D scene void drawScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, cameraheight);//I have moved the circuit and the car 20 meters under, so now //the camera is "set" at 20 meters high than the car and the circuit //CAR glPushMatrix(); //Save the transformations performed thus far glTranslatef(xpos*sinf(_angle), ypos*cosf(_angle), 0.0f); glRotatef(_angle, 0.0f, 0.0f, 1.0f); glBegin(GL_QUADS); glColor3f(1.0f, 0.0f, 0.0f); //Red Ferrari glVertex3f(-2.25f, 1.0f, 0.0f); //Meters (4,5m long per 2,25m wide) glVertex3f(2.25f, 1.0f, 0.0f); glVertex3f(2.25f, -1.0f, 0.0f); glVertex3f(-2.25f, -1.0f, 0.0f); glEnd(); glPopMatrix(); //Undo the move of the car glutSwapBuffers(); } void update(int value) { if (_angle > 360) { _angle -= 360; } glutPostRedisplay(); glutTimerFunc(16, update, 0); } int main(int argc, char** argv) { //Initialize GLUT glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(320, 240); //Create the window glutCreateWindow("Test"); initRendering(); //Set handler functions glutDisplayFunc(drawScene); glutKeyboardFunc(handleKeypress); glutReshapeFunc(handleResize); glutTimerFunc(16, update, 0); //Add a timer glutMainLoop(); return 0; } ``````

Is the idea from here http://www.passyworld.com/passyPDFs/FlashTrigCarHowTo.pdf

Anyone sees the problem? Thank you
Topic archived. No new replies allowed.