-
Notifications
You must be signed in to change notification settings - Fork 1
/
vfr.cpp
285 lines (225 loc) · 5.2 KB
/
vfr.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define WIDTH 600
#define HEIGHT 400
//-----------------------------------------------
#define MOVE 1
#define QUIET 2
//-----------------------------------------------
void display();;
void keyboard(unsigned char c,int x,int y);
void idle();
//-----------------------------------------------
class particle {
float x,y; //-- Current position
float vx,vy; //-- Velocity vector
int state;
long time_remaining;
public:
particle();
void set_position(int x,int y);
void init_movement(int destination_x,int destination_y,int duration);
void integrate(long t);
void draw();
};
//-----------------------------------------------
particle::particle()
{
state=QUIET;
}
//-----------------------------------------------
void particle::set_position(int x,int y)
{
this->x = x;
this->y = y;
}
//-----------------------------------------------
void particle::init_movement(int destination_x,int destination_y,int duration)
{
vx = (destination_x - x)/duration;
vy = (destination_y - y)/duration;
state=MOVE;
time_remaining=duration;
}
//-----------------------------------------------
void particle::integrate(long t)
{
if(state==MOVE && t<time_remaining)
{
x = x + vx*t;
y = y + vy*t;
time_remaining-=t;
}
else if(state==MOVE && t>=time_remaining)
{
x = x + vx*time_remaining;
y = y + vy*time_remaining;
state=QUIET;
}
}
//-----------------------------------------------
void particle::draw()
{
glColor3f(1,1,1);
glBegin(GL_QUADS);
glVertex2i(x-6,y-6);
glVertex2i(x+6,y-6);
glVertex2i(x+6,y+6);
glVertex2i(x-6,y+6);
glEnd();
}
//-----------------------------------------------
// -- MAIN
//-----------------------------------------------
particle square;
long last_t=0;
int main(int argc,char *argv[])
{
srand(time(NULL));
square.set_position(50,50);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Variable Frame Rate");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,WIDTH-1,0.0,HEIGHT-1);
glutMainLoop();
return 0;
}
//-----------------------------------------------
//-----------------------------------------------
void display()
{
glClearColor(0,0,0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
square.draw();
glutSwapBuffers();
}
//-----------------------------------------------
//-----------------------------------------------
void keyboard(unsigned char c,int x,int y)
{
square.set_position(50,50);
square.init_movement(WIDTH-50,HEIGHT-50,1000);
glutPostRedisplay();
};
//-----------------------------------------------
//-----------------------------------------------
void idle()
{
long t;
t=glutGet(GLUT_ELAPSED_TIME);
if(last_t==0)
last_t=t;
else
{
square.integrate(t-last_t);
last_t=t;
}
glutPostRedisplay();
}
*/
/*
#include <fstream>
#include <vector>
#include <cassert>
using HighScoreEntry = int;
int main()
{
std::vector<HighScoreEntry> highscores;
std::ifstream ifile("example.txt");
HighScoreEntry tentry;
while(ifile) {
ifile >> tentry;
highscores.push_back(tentry);
}
assert(highscores.empty());
assert(!ifile.good());
}
sobre el stick:
class setShotVelocity
{
public:
int last_mx;
int last_my;
int cur_mx;
int cur_my;
int tracking;
int ballID;
glm::vec3 velocity;
glm::vec3 angVel;
void setVelocity();
};
Main antes keyboard:
glutMouseFunc(onMouse);
glutMotionFunc(onMotion);
void processSelection(int x, int y)
{
unsigned char res[4];
renderSelection();
glReadPixels(x, screen_height-y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &res);
myShot.ballID = res[0]-1;
myShot.setVelocity();
if (myShot.ballID != -1)
{
myBallPhy.ballVel[myShot.ballID] = myShot.velocity;
myBallPhy.ballAngVel[myShot.ballID] = myShot.angVel;
}
cout << "myBallPhy.ballVel[myShot.ballID]: " << myShot.velocity[0] << ", " << myShot.velocity[1] << endl;
printf("Ball: %d\n", myShot.ballID);
}
void onMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
myShot.last_mx = x;
myShot.last_my = y;
myShot.tracking = true;
}
else myShot.tracking = false;
// if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
// {
// myQuater.last_mx = myQuater.cur_mx = x;
// myQuater.last_my = myQuater.cur_my = y;
// myQuater.trackball_on = true;
// }
// else myQuater.trackball_on = false;
}
void onMotion(int x, int y)
{
if (myShot.tracking)
{
myShot.cur_mx = x;
myShot.cur_my = y;
processSelection(x,y);
printf("cur_mx: %d, cur_my: %d\n", myShot.cur_mx, myShot.cur_my);
}
// if (myQuater.trackball_on)
// {
// myQuater.cur_mx = x;
// myQuater.cur_my = y;
// }
}
void setShotVelocity::setVelocity()
{
if (ballID >= 0)
{
velocity[0] = cur_my - last_my;
velocity[1] = 0.0;
velocity[2] = -(cur_mx - last_mx);
angVel = glm::cross(glm::vec3(0.0,1.0,0.0),velocity);
}
else
{
velocity = glm::vec3(0.0f);
angVel = glm::vec3(0.0f);
}
}
**/