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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <stdlib.h>
#include <glut.h>
#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

1
2
3
4
5
6
7
8
9
10
11
12
#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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <stdlib.h>
#include <glut.h>
#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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <stdlib.h>
#include <glut.h>

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.