-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
5072 lines (4270 loc) · 171 KB
/
Copy pathmain.cpp
File metadata and controls
5072 lines (4270 loc) · 171 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <windows.h>
#include <GL/glut.h>
#include <ctime>
#include <vector>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;
#define PI 3.14159265358979323846
bool isSoundPlaying = true;
bool gameOverTriggered=false;
bool timer2= false;
bool timer3= false;
bool timer4= false;
bool timer5= false;
void playSound() {
PlaySound("background.wav", NULL, SND_ASYNC | SND_FILENAME | SND_LOOP);
}
void stopSound() {
PlaySound(NULL, 0, 0); // Stops the sound
}
void toggleSound() {
if (isSoundPlaying) {
stopSound();
isSoundPlaying = false;
} else {
playSound();
isSoundPlaying = true;
}
}
// State to determine which screen to render
int currentScreen = 0; // 0 = menu, 1 = info, 2 = levelChoose
// Struct to represent 2D points (vertices)
struct Point {
float x, y;
Point(float x, float y) {
this->x = x;
this->y = y;
}
};
// Function definition for rendering text
void renderText(float x, float y, const char* text, void* font) {
glRasterPos2f(x, y); // Set position for the text
while (*text) {
glutBitmapCharacter(font, *text); // Render each character
text++;
}
}
namespace backStory{
// Plane position and animation variables
float planeOffsetY = 40.0f;
float planeSpeedY = 0.001f;
// Star variables
struct Star {
float x, y;
};
vector<Star> stars;
const int numStars = 80;
float starSpeed = 0.02f;
// Backstory variables
vector<string> backstory = {
"In the not-so-distant future, humanity's skies are no longer a place of serenity",
"but a proving ground for the most skilled pilots.",
"As the world grows chaotic with storms of debris and rogue drones,",
"a secret initiative known as The PathWay was created.",
"To find the next generation of aerial legends",
"You are a daring pilot thrust into this high-stakes simulation.",
"It is designed to test your reflexes, precision, and endurance.",
"Six perilous levels await, each with unpredictable challenges.",
"Will you navigate the pathway to victory,",
"or will the skies claim another challenger?"
};
size_t lineIndex = 0;
string currentText = "";
float lastUpdateTime = 0;
// Plane structure and colors
struct Point {
float x, y;
};
vector<vector<Point>> plane = {
{{0.0f, -58.0f}, {0.3f, -59.0f}, {-0.3f, -59.0f}},
{{0.3f, -59.0f}, {-0.3f, -59.0f}, {-0.8f, -59.4f}, {-0.8f, -59.6f}, {0.8f, -59.6f}, {0.8f, -59.4f}},
{{-0.2f, -59.6f}, {0.2f, -59.6f}, {0.2f, -59.8f}, {-0.2f, -59.8f}},
{{-0.2f, -59.8f}, {-0.4f, -60.0f}, {-0.2f, -60.0f}, {-0.15f, -59.95f}},
{{-0.2f, -59.8f}, {-0.15f, -59.95f}, {-0.1f, -60.0f}, {0.1f, -60.0f}, {0.15f, -59.95f}, {0.2f, -59.8f}},
{{0.2f, -59.8f}, {0.15f, -59.95f}, {0.2f, -60.0f}, {0.4f, -60.0f}},
{{-0.8f, -59.6f}, {-0.8f, -59.2f}},
{{0.8f, -59.6f}, {0.8f, -59.2f}}
};
vector<vector<float>> planeColors = {
{0.698f, 0.745f, 0.710f}, {0.827f, 0.827f, 0.827f}, {0.698f, 0.745f, 0.710f},
{0.827f, 0.827f, 0.827f}, {0.827f, 0.827f, 0.827f}, {0.827f, 0.827f, 0.827f},
{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}
};
float randomXPosition();
// Initialize star positions
void initStars() {
for (int i = 0; i < numStars; ++i) {
stars.push_back({randomXPosition(), static_cast<float>(rand() % 40 - 20)});
}
}
float randomXPosition() {
return static_cast<float>(rand() % 30 - 15);
}
// Draw stars moving downward
void drawStars() {
for (auto& star : stars) {
star.y -= starSpeed;
if (star.y < -20.0f) {
star.y = 20.0f;
star.x = randomXPosition();
}
glPointSize(2.0f);
glBegin(GL_POINTS);
glColor3f(1.0f, 1.0f, 1.0f); // White stars
glVertex2f(star.x, star.y);
glEnd();
}
}
// Draw the plane
void drawPlane(const vector<vector<Point>>& plane, const vector<vector<float>>& colors) {
glPushMatrix();
glTranslatef(0.0f, planeOffsetY, 0.0f);
for (size_t i = 0; i < plane.size(); ++i) {
const auto& vertices = plane[i];
const auto& color = colors[i];
if (vertices.size() > 2) {
glBegin(GL_POLYGON);
glColor3f(color[0], color[1], color[2]);
for (const auto& vertex : vertices) {
glVertex2f(vertex.x, vertex.y);
}
glEnd();
} else if (vertices.size() == 2) {
glLineWidth(1.5f);
glBegin(GL_LINES);
glColor3f(color[0], color[1], color[2]);
for (const auto& vertex : vertices) {
glVertex2f(vertex.x, vertex.y);
}
glEnd();
}
}
glPopMatrix();
}
// Display backstory with typewriter effect
void displayBackstory() {
float currentTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
if (lineIndex < backstory.size()) {
if (currentText.length() < backstory[lineIndex].length()) {
if (currentTime - lastUpdateTime > 0.05f) {
currentText += backstory[lineIndex][currentText.length()];
lastUpdateTime = currentTime;
}
} else if (currentTime - lastUpdateTime > 1.0f) {
currentText = "";
lineIndex++;
lastUpdateTime = currentTime;
}
}
/*if(currentTime - lastUpdateTime > 3.0f){
// home button.
}*/
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f(-8.0f, 0.0f); // Adjusted position
for (char c : currentText) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c);
}
}
// Animate the plane upward
void animatePlane() {
if (planeOffsetY < 80.0f) {
planeOffsetY += planeSpeedY;
}
}
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT);
// from I1->J1->K1->L1
glBegin(GL_POLYGON);
glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(-1.5f, -8.0f);
glVertex2f(1.5f, -8.0f);
glVertex2f(1.5f, -9.0f);
glVertex2f(-1.5f, -9.0f);
glEnd();
// Render text inside the polygon
glColor3f(1.0f, 1.0f, 1.0f);
renderText(-0.4f, -8.5f, "Back", GLUT_BITMAP_9_BY_15);
drawStars();
drawPlane(plane, planeColors);
displayBackstory();
animatePlane();
glutSwapBuffers();
glutPostRedisplay();
}
}
namespace gameOver{
void display(){
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
// Projection setup
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
// from G1->H1->F1->E1
glBegin(GL_POLYGON);
glColor3f(0.514f, 0.871f, 0.925f);
glVertex2f(-10.0f, 10.0f);
glVertex2f(10.0f, 10.0f);
glVertex2f(10.0f, -10.0f);
glVertex2f(-10.0f, -10.0f);
glEnd();
// from A->B->D->C
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-4.0f, 6.0f);
glVertex2f(4.0f, 6.0f);
glVertex2f(4.0f, 0.0f);
glVertex2f(-4.0f, 0.0f);
glEnd();
// from I->J->K->L
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-2.0f, -3.0f);
glVertex2f(2.0f, -3.0f);
glVertex2f(2.0f, -4.0f);
glVertex2f(-2.0f, -4.0f);
glEnd();
// from E2->F2->H2->G2
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-2.0f, -6.0f);
glVertex2f(2.0f, -6.0f);
glVertex2f(2.0f, -7.0f);
glVertex2f(-2.0f, -7.0f);
glEnd();
// for N
GLfloat x2= -9.5f;
GLfloat y2= 9.5f;
GLfloat radius = 0.5f;
int triangleAmount = 100; //# of lines used to draw circle
//GLfloat radius = 0.8f; //radius
GLfloat twicePi = 2.0f * PI;
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x2, y2); // center of circle
for(int i = 0; i <= triangleAmount;i++) {
glVertex2f( x2 + (radius * cos(i * twicePi / triangleAmount)),
y2 + (radius * sin(i * twicePi / triangleAmount)) );
}
glEnd();
// for D1
GLfloat x3= -9.75f;
GLfloat y3= 9.25f;
GLfloat rad = 0.1f;
glColor3f(0.514f, 0.871f, 0.925f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x3, y3); // center of circle
for(int i = 0; i <= triangleAmount;i++) {
glVertex2f( x3 + (rad * cos(i * twicePi / triangleAmount)),
y3 + (rad * sin(i * twicePi / triangleAmount)) );
}
glEnd();
// for D1
GLfloat x4= -9.35f;
GLfloat y4= 9.35f;
glColor3f(0.514f, 0.871f, 0.925f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x4, y4); // center of circle
for(int i = 0; i <= triangleAmount;i++) {
glVertex2f( x4 + (rad * cos(i * twicePi / triangleAmount)),
y4 + (rad * sin(i * twicePi / triangleAmount)) );
}
glEnd();
glLineWidth(2.0f);
glBegin(GL_LINES);
glColor3f(0.514f, 0.871f, 0.925f);
glVertex2f(-9.7f, 9.3f);
glVertex2f(-9.7f, 9.6);
glEnd();
glLineWidth(2.0f);
glBegin(GL_LINES);
glColor3f(0.514f, 0.871f, 0.925f);
glVertex2f(-9.7f, 9.6);
glVertex2f(-9.3f, 9.7f);
glEnd();
glLineWidth(2.0f);
glBegin(GL_LINES);
glColor3f(0.514f, 0.871f, 0.925f);
glVertex2f(-9.3f, 9.7f);
glVertex2f(-9.3f, 9.4f);
glEnd();
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-1.5f, 7.5f, "Game Over", GLUT_BITMAP_TIMES_ROMAN_24);
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-3.0f, 4.0f, "Your Score: ", GLUT_BITMAP_9_BY_15);
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-3.0f, 2.0f, "Higest Score: ", GLUT_BITMAP_9_BY_15);
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-1.0f, -3.5f, "Try Again", GLUT_BITMAP_9_BY_15);
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-0.5f, -6.5f, "Home", GLUT_BITMAP_9_BY_15);
glFlush();
glutPostRedisplay();
}
}
namespace levelComplete{
void display(){
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer (background)
// Projection setup
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0f, 10.0f, -10.0f, 10.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
// from G1->H1->F1->E1
glBegin(GL_POLYGON);
glColor3f(0.514f, 0.871f, 0.925f);
glVertex2f(-10.0f, 10.0f);
glVertex2f(10.0f, 10.0f);
glVertex2f(10.0f, -10.0f);
glVertex2f(-10.0f, -10.0f);
glEnd();
// from A2->B2->D2->C2
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-4.0f, 6.0f);
glVertex2f(4.0f, 6.0f);
glVertex2f(4.0f, 0.0f);
glVertex2f(-4.0f, 0.0f);
glEnd();
// from I1->J1->K1->L1
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-2.0f, -3.0f);
glVertex2f(2.0f, -3.0f);
glVertex2f(2.0f, -4.0f);
glVertex2f(-2.0f, -4.0f);
glEnd();
// from E3->F3->H3->G3
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(-2.0f, -6.0f);
glVertex2f(2.0f, -6.0f);
glVertex2f(2.0f, -7.0f);
glVertex2f(-2.0f, -7.0f);
glEnd();
// for N
GLfloat x2= -9.5f;
GLfloat y2= 9.5f;
GLfloat radius = 0.5f;
int triangleAmount = 100; //# of lines used to draw circle
//GLfloat radius = 0.8f; //radius
GLfloat twicePi = 2.0f * PI;
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x2, y2); // center of circle
for(int i = 0; i <= triangleAmount;i++) {
glVertex2f( x2 + (radius * cos(i * twicePi / triangleAmount)),
y2 + (radius * sin(i * twicePi / triangleAmount)) );
}
glEnd();
// for D1
GLfloat x3= -9.75f;
GLfloat y3= 9.25f;
GLfloat rad = 0.1f;
glColor3f(0.514f, 0.871f, 0.925f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x3, y3); // center of circle
for(int i = 0; i <= triangleAmount;i++) {
glVertex2f( x3 + (rad * cos(i * twicePi / triangleAmount)),
y3 + (rad * sin(i * twicePi / triangleAmount)) );
}
glEnd();
// for D1
GLfloat x4= -9.35f;
GLfloat y4= 9.35f;
glColor3f(0.514f, 0.871f, 0.925f);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x4, y4); // center of circle
for(int i = 0; i <= triangleAmount;i++) {
glVertex2f( x4 + (rad * cos(i * twicePi / triangleAmount)),
y4 + (rad * sin(i * twicePi / triangleAmount)) );
}
glEnd();
glLineWidth(2.0f);
glBegin(GL_LINES);
glColor3f(0.514f, 0.871f, 0.925f);
glVertex2f(-9.7f, 9.3f);
glVertex2f(-9.7f, 9.6);
glEnd();
glLineWidth(2.0f);
glBegin(GL_LINES);
glColor3f(0.514f, 0.871f, 0.925f);
glVertex2f(-9.7f, 9.6);
glVertex2f(-9.3f, 9.7f);
glEnd();
glLineWidth(2.0f);
glBegin(GL_LINES);
glColor3f(0.514f, 0.871f, 0.925f);
glVertex2f(-9.3f, 9.7f);
glVertex2f(-9.3f, 9.4f);
glEnd();
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-1.5f, 7.5f, "Level Completed", GLUT_BITMAP_TIMES_ROMAN_24);
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-3.0f, 4.0f, "Your Score: ", GLUT_BITMAP_9_BY_15);
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-3.0f, 2.0f, "Higest Score: ", GLUT_BITMAP_9_BY_15);
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-1.0f, -3.5f, "Try Again", GLUT_BITMAP_9_BY_15);
// Render text inside the polygon
glColor3f(0.0f, 0.0f, 0.0f);
renderText(-0.5f, -6.5f, "Levels", GLUT_BITMAP_9_BY_15);
glFlush();
glutSwapBuffers();
}
}
namespace levelTwo{
int counter = 0; // Integer to display
float elapsedTime= 0.0f;
// Rotation variable
float rotationAngle = 0.0f;
// Initial y-axis bounds for the visible region
float yMin = -60.0f, yMax = -40.0f;
float step = 0.05;
int scrollTime = 16;
// Plane movement variables
float planeOffsetX = 0.0f; // Offset for the plane to sync with scrolling
float planeOffsetY = 0.0f; // Offset for polygon movement in the x-axis
float planeRotation = 0.0f; // Track the rotation of the plane
bool isKeyPressedA = false;
bool isKeyPressedD = false;
float velocityX = 0.0f;
float targetRotation = 0.0f;
const float moveSpeed = 0.005f; // Speed for smooth movement
const float rotationSpeed = 2.0f; // Speed for smooth rotation
// Variables to handle translation
float xOffset1 = 0.0f; // Offset for translation
float xOffset2 = 0.0f; // Offset for translation
float xOffset3 = 0.0f; // Offset for translation
bool startTranslation1 = false; // Flag to start translation
bool startTranslation2 = false;
bool startTranslation3 = false;
float speed = 0.005f; // Speed of translation
vector<pair<float, float>> translationOffsets = {
{xOffset1, 0.0f},
{xOffset2, 0.0f},
{-xOffset3, 0.0f},
{xOffset3, 0.0f}
}; // Corresponding offsets for each polygon
clock_t startTime; // Timer to check if certain seconds have passed
void init() {
startTime = clock(); // Record the start time
}
vector<vector<Point>> transformedRotationPolygons;
vector<vector<Point>> transformedTranslationPolygons;
//store generated circle vertices
vector<vector<Point>> circles{};
vector<vector<Point>> rotationPolygons = {
// from Z->L1->S1->R1
{{8.0f, -18.0f}, {12.0f, -18.0f}, {12.0f, -26.0f}, {8.0f, -26.0f}},
// from Z2->W2->V2->U2
{{-15.0f, -8.0f}, {-10.0f, -8.0f}, {-10.0f, -6.0f}, {-15.0f, -6.0f}},
// from T2->S2->Q2->R2
{{15.0f, -8.0f}, {10.0f, -8.0f}, {10.0f, -6.0f}, {15.0f, -6.0f}},
// from M2->N2->P2->O2
{{-4.0f, 2.0f}, {4.0f, 2.0f}, {4.0f, -2.0f}, {-4.0f, -2.0f}},
// from F2->G2->L2->H2
{{-4.0f, 10.0f}, {4.0f, 10.0f}, {4.0f, 6.0f}, {-4.0f, 6.0f}},
// from H3->G3->E3->F3
{{15.0f, 12.0f}, {10.0f, 12.0f}, {10.0f, 14.0f}, {15.0f, 14.0f}},
// from D3->A3->B3->C3
{{-15.0f, 14.0f}, {-10.0f, 14.0f}, {-10.0f, 12.0f}, {-15.0f, 12.0f}}
};
vector<vector<Point>> translationPolygons = {
// from A4->B4->W3->Z3
{{15.0f, 22.0f}, {6.0f, 22.0f}, {6.0f, 24.0f}, {15.0f, 24.0f}},
// from C4->D4->E4->F4
{{-15.0f, 28.0f}, {-6.0f, 28.0f}, {-6.0f, 30.0f}, {-15.0f, 30.0f}},
// from N4->K4->L4->M4
{{-15.0f, 36.0f}, {-6.0f, 36.0f}, {-6.0f, 38.0f}, {-15.0f, 38.0f}},
// from J4->G4->H4->I4
{{15.0f, 36.0f}, {6.0f, 36.0f}, {6.0f, 38.0f}, {15.0f, 38.0f}},
};
vector<Point> applyRotation(const vector<Point>& polygon, float angle) {
vector<Point> transformedPolygon;
float radians = angle * M_PI / 180.0f;
// Calculate the center of the rectangle
float minX = polygon[0].x, maxX = polygon[0].x;
float minY = polygon[0].y, maxY = polygon[0].y;
for (const auto& vertex : polygon) { //////check this logic
minX = min(minX, vertex.x);
maxX = max(maxX, vertex.x);
minY = min(minY, vertex.y);
maxY = max(maxY, vertex.y);
}
float centerX = (minX + maxX) / 2.0f;
float centerY = (minY + maxY) / 2.0f;
// Apply rotation around the center
for (const auto& vertex : polygon) {
float x = centerX + (vertex.x - centerX) * cos(radians) - (vertex.y - centerY) * sin(radians);
float y = centerY + (vertex.x - centerX) * sin(radians) + (vertex.y - centerY) * cos(radians);
transformedPolygon.push_back({x, y});
}
return transformedPolygon;
}
vector<Point> applyTranslation(const vector<Point>& polygon, float offsetX, float offsetY) {
vector<Point> transformedPolygon;
for (const auto& vertex : polygon) {
transformedPolygon.push_back({vertex.x + offsetX, vertex.y + offsetY});
}
return transformedPolygon;
}
void applyMovement(){
// Clear the transformed polygons
transformedRotationPolygons.clear();
transformedTranslationPolygons.clear();
translationOffsets = {{xOffset1, 0.0f}, {xOffset2, 0.0f},{-xOffset3, 0.0f},{xOffset3, 0.0f}};
for (size_t i = 0; i < translationPolygons.size(); ++i) {
transformedTranslationPolygons.push_back(
applyTranslation(translationPolygons[i], translationOffsets[i].first, translationOffsets[i].second)
);
}
for(const auto& polygon : rotationPolygons) {
transformedRotationPolygons.push_back(
applyRotation(polygon, rotationAngle)
);
}
}
/* void drawPlane() {
glPushMatrix();
glTranslatef(planeOffsetX, planeOffsetY, 0.0f);
glRotatef(planeRotation, 0.0f, 1.0f, 0.0f); // Apply rotation
// from B1->D1->C1
glBegin(GL_POLYGON);
glColor3f(0.698f, 0.745f, 0.710f);
glVertex2f(0.0f, -58.0f);
glVertex2f(0.3f, -59.0f);
glVertex2f(-0.3f, -59.0f);
glEnd();
// from D1->C1->E1->H1->K1->I1
glBegin(GL_POLYGON);
glColor3f(0.827f, 0.827f, 0.827f);
glVertex2f(0.3f, -59.0f);
glVertex2f(-0.3f, -59.0f);
glVertex2f(-0.8f, -59.4f);
glVertex2f(-0.8f, -59.6f);
glVertex2f(0.8f, -59.6f);
glVertex2f(0.8f, -59.4f);
glEnd();
// from M1->U4->T4->K2
glBegin(GL_POLYGON);
glColor3f(0.698f, 0.745f, 0.710f);
glVertex2f(-0.2f, -59.6f);
glVertex2f(0.2f, -59.6f);
glVertex2f(0.2f, -59.8f);
glVertex2f(-0.2f, -59.8f);
glEnd();
// from K2->V4->Z4->A
glBegin(GL_POLYGON);
glColor3f(0.827f, 0.827f, 0.827f);
glVertex2f(-0.2f, -59.8f);
glVertex2f(-0.4f, -60.0f);
glVertex2f(-0.2f, -60.0f);
glVertex2f(-0.15f, -59.95f);
glEnd();
// from K2->A->A5->B5->A1->T4
glBegin(GL_POLYGON);
glColor3f(0.827f, 0.827f, 0.827f);
glVertex2f(-0.2f, -59.8f);
glVertex2f(-0.15f, -59.95f);
glVertex2f(-0.1f, -60.0f);
glVertex2f(0.1f, -60.0f);
glVertex2f(0.15f, -59.95f);
glVertex2f(0.2f, -59.8f);
glEnd();
// from T4->A1->C5->W4
glBegin(GL_POLYGON);
glColor3f(0.827f, 0.827f, 0.827f);
glVertex2f(0.2f, -59.8f);
glVertex2f(0.15f, -59.95f);
glVertex2f(0.2f, -60.0f);
glVertex2f(0.4f, -60.0f);
glEnd();
// from E1->G1
glLineWidth(1.5f);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-0.8f, -59.6f);
glVertex2f(-0.8f, -59.2f);
glEnd();
// from I1->J1
glLineWidth(1.5f);
glBegin(GL_LINES);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(0.8f, -59.6f);
glVertex2f(0.8f, -59.2f);
glEnd();
glPopMatrix();
}
*/
vector<vector<float>> planeColors = {
// Color for the first polygon
{0.698f, 0.745f, 0.710f},
// Color for the second polygon
{0.827f, 0.827f, 0.827f},
// Color for the third polygon
{0.698f, 0.745f, 0.710f},
// Color for the fourth polygon
{0.827f, 0.827f, 0.827f},
// Color for the fifth polygon
{0.827f, 0.827f, 0.827f},
// Color for the sixth polygon
{0.827f, 0.827f, 0.827f},
// Color for the first line
{0.0f, 0.0f, 0.0f},
// Color for the second line
{0.0f, 0.0f, 0.0f}
};
vector<vector<Point>> plane = {
// First polygon
{
{0.0f, -58.0f}, {0.3f, -59.0f}, {-0.3f, -59.0f}
},
// Second polygon
{
{0.3f, -59.0f}, {-0.3f, -59.0f}, {-0.8f, -59.4f},
{-0.8f, -59.6f}, {0.8f, -59.6f}, {0.8f, -59.4f}
},
// Third polygon
{
{-0.2f, -59.6f}, {0.2f, -59.6f}, {0.2f, -59.8f}, {-0.2f, -59.8f}
},
// Fourth polygon
{
{-0.2f, -59.8f}, {-0.4f, -60.0f}, {-0.2f, -60.0f}, {-0.15f, -59.95f}
},
// Fifth polygon
{
{-0.2f, -59.8f}, {-0.15f, -59.95f}, {-0.1f, -60.0f},
{0.1f, -60.0f}, {0.15f, -59.95f}, {0.2f, -59.8f}
},
// Sixth polygon
{
{0.2f, -59.8f}, {0.15f, -59.95f}, {0.2f, -60.0f}, {0.4f, -60.0f}
},
// First line
{
{-0.8f, -59.6f}, {-0.8f, -59.2f}
},
// Second line
{
{0.8f, -59.6f}, {0.8f, -59.2f}
}
};
// Function to draw a chequered bar
void drawChequeredBar(float x1, float y1, float x2, float y2, float ySubdivision) {
float width = x2 - x1; // Total width of the bar
int numDivisions = static_cast<int>(width); // Number of subdivisions along x-axis
// Top rectangle: From y1 to ySubdivision
for (int i = 0; i < numDivisions; i++) {
float xStart = x1 + i;
float xEnd = x1 + i + 1;
if (i % 2 == 0) {
glColor3f(0.0, 0.0, 0.0); // Black
} else {
glColor3f(1.0, 1.0, 1.0); // White
}
glBegin(GL_POLYGON);
glVertex2f(xStart, y1);
glVertex2f(xEnd, y1);
glVertex2f(xEnd, ySubdivision);
glVertex2f(xStart, ySubdivision);
glEnd();
}
// Bottom rectangle: From ySubdivision to y2
for (int i = 0; i < numDivisions; i++) {
float xStart = x1 + i;
float xEnd = x1 + i + 1;
if (i % 2 != 0) { // Alternate pattern
glColor3f(0.0, 0.0, 0.0); // Black
} else {
glColor3f(1.0, 1.0, 1.0); // White
}
glBegin(GL_POLYGON);
glVertex2f(xStart, ySubdivision);
glVertex2f(xEnd, ySubdivision);
glVertex2f(xEnd, y2);
glVertex2f(xStart, y2);
glEnd();
}
// Draw the top x-axis line
glColor3f(0.0, 0.0, 0.0); // Black color for lines
glBegin(GL_LINES);
glVertex2f(x1, y1); // Start of the top edge
glVertex2f(x2, y1); // End of the top edge
glEnd();
// Draw the bottom x-axis line
glBegin(GL_LINES);
glVertex2f(x1, y2); // Start of the bottom edge
glVertex2f(x2, y2); // End of the bottom edge
glEnd();
}
void drawPlane(const vector<vector<Point>>& plane, const vector<vector<float>>& colors) {
glPushMatrix();
glTranslatef(planeOffsetX, planeOffsetY, 0.0f);
glRotatef(planeRotation, 0.0f, 1.0f, 0.0f); // Apply rotation
for (size_t i = 0; i < plane.size(); ++i) {
const auto& vertices = plane[i];
const auto& color = colors[i];
if (vertices.size() > 2) {
// Draw polygons
glBegin(GL_POLYGON);
glColor3f(color[0], color[1], color[2]);
for (const auto& vertex : vertices) {
glVertex2f(vertex.x, vertex.y);
}
glEnd();
} else if (vertices.size() == 2) {
// Draw lines
glLineWidth(1.5f);
glBegin(GL_LINES);
glColor3f(color[0], color[1], color[2]);
for (const auto& vertex : vertices) {
glVertex2f(vertex.x, vertex.y);
}
glEnd();
}
}
glPopMatrix();
}
vector<vector<Point>> obstacles = {
// First obstacle (C->E->F->H)
{
{5.0f, -45.0f}, {-5.0f, -45.0f}, {-5.0f, -40.0f}, {5.0f, -40.0f}
},
// Second obstacle (G->I->J->M)
{
{-15.0f, -35.0f}, {-5.0f, -35.0f}, {-5.0f, -30.0f}, {-15.0f, -30.0f}
},
// Obstacle (T->U->W->V)
{
{-8.0f, -20.0f}, {3.0f, -20.0f}, {3.0f, -24.0f}, {-8.0f, -24.0f}
},
// Obstacle (O4->R->T1->W1)
{
{-15.0f, -15.0f}, {0.0f, -15.0f}, {0.0f, -10.0f}, {-15.0f, -10.0f}
},
// Obstacle (P4->S->U1->V1)
{
{15.0f, -15.0f}, {10.0f, -15.0f}, {10.0f, -10.0f}, {15.0f, -10.0f}
},
// Obstacle (Z1->B2->D2->J3)
{
{-15.0f, 15.0f}, {-5.0f, 15.0f}, {-5.0f, 20.0f}, {-15.0f, 20.0f}
},
// Obstacle (A2->C2->E2->I3)
{
{15.0f, 15.0f}, {5.0f, 15.0f}, {5.0f, 20.0f}, {15.0f, 20.0f}
},
// Obstacle (K3->M3->O3->R4)
{
{-15.0f, 40.0f}, {-5.0f, 40.0f}, {-5.0f, 45.0f}, {-15.0f, 45.0f}
},
// Obstacle (L3->N3->P3->Q3->Q4)
{
{15.0f, 40.0f}, {5.0f, 40.0f}, {5.0f, 45.0f}, {10.0f, 50.0f}, {15.0f, 50.0f}
},
// Obstacle (R4->O3->R3->S3->S4)
{
{-15.0f, 45.0f}, {-5.0f, 45.0f}, {0.0f, 50.0f}, {-5.0f, 55.0f}, {-15.0f, 55.0f}
},
// Obstacle (Q4->Q3->T3->V3->J2)
{
{15.0f, 50.0f}, {10.0f, 50.0f}, {5.0f, 55.0f}, {5.0f, 60.0f}, {15.0f, 60.0f}
},
// Obstacle (S4->S3->U3->I2)
{
{-15.0f, 55.0f}, {-5.0f, 55.0f}, {-5.0f, 60.0f}, {-15.0f, 60.0f}
},
// from N1->P1->B->D
{
{-15.0f, -60.0f}, {-5.0f, -60.0f}, {-5.0f, -50.0f}, {-15.0f, -50.0f}
},
// from O1->Q1->K->L
{
{15.0f, -60.0f}, {5.0f, -60.0f}, {5.0f, -30.0f}, {15.0f, -30.0f}
},
// from O1->J2
{
{{15.0f, -60.0f}, {15.0f, 60.0f}}
},
// from N1->I2
{
{{-15.0f, -60.0f}, {-15.0f, 60.0f}}
},
// from M->O->P->O4
{
{-15.0f, -30.0f}, {-14.0f, -30.0f}, {-14.0f, -15.0f}, {-15.0f, -15.0f}
},
// from L->N->Q->P4
{
{15.0f, -30.0f}, {14.0f, -30.0f}, {14.0f, -15.0f}, {15.0f, -15.0f}
}
};
// Collision detection Start
// Function to compute the dot product of two vectors
float dotProduct(const Point& a, const Point& b) {
return a.x * b.x + a.y * b.y;
}
// Function to calculate axes perpendicular to edges of a polygon
vector<Point> getAxes(const vector<Point>& polygon) {
vector<Point> axes;
for (size_t i = 0; i < polygon.size(); ++i) {
Point edge = {
polygon[(i + 1) % polygon.size()].x - polygon[i].x,
polygon[(i + 1) % polygon.size()].y - polygon[i].y
};
axes.push_back({-edge.y, edge.x}); // Perpendicular axis
}
return axes;
}
// Function to project a polygon onto an axis
void projectOntoAxis(const vector<Point>& polygon, const Point& axis, float& minVal, float& maxVal) {
minVal = maxVal = dotProduct(polygon[0], axis);
for (const auto& vertex : polygon) {
float projection = dotProduct(vertex, axis);
if (projection < minVal) minVal = projection;
if (projection > maxVal) maxVal = projection;
}
}
// SAT-based collision detection between two polygons
bool polygonsCollide(const vector<Point>& poly1, const vector<Point>& poly2) {
vector<Point> axes1 = getAxes(poly1);
vector<Point> axes2 = getAxes(poly2);
for (const auto& axis : axes1) {
float min1, max1, min2, max2;
projectOntoAxis(poly1, axis, min1, max1);
projectOntoAxis(poly2, axis, min2, max2);
if (max1 < min2 || max2 < min1) {
return false; // No overlap, polygons don't collide
}
}
for (const auto& axis : axes2) {