-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision Detection.cpp
1305 lines (1201 loc) · 42.8 KB
/
Collision Detection.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
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
/***********************************************************************************
CTIS164 - Official Homework 3
====================================================================================
STUDENT : Mehmet Bulut 21903372
SECTION : 002
HOMEWORK : Official Homework 3 for 2021 Spring Semester (Submission: 25 April 2021)
====================================================================================
PROBLEMS :
Satellite keep showing after each F1 key press
====================================================================================
ADDITIONAL FEATURES :
1- Tank (firing object) can be rotated with the onMove() function and can be fired with onClick() function
2- Linked list are implemented for multiple bullet firing up to AMMO macro at a time on the scene
2.1- Linked listes traversed for collision detection purposes and those who collide (both object and bullet) is removed
from the object and bullet linked lists.
3.1- Bullet looks primitively like a bullet, there is no basic point bullets
3.2- Tank's angle and position is displayed along with the mouse location for reference purposes
4- Additional third object is used and moved along the sinusodial line with the harmonic motion
5- Additional bullet type is added that perform projectile motion, however angle of the bullet stayed same unfortunately :((
5.2- throwing velocity of the bullet change be either incremented or decremented with (+,-)
6.1- Object that move on the orbit is also rotate itself (similar to earth moving around the sun and rotate by itself)
6.2- Our Fourth object, namely satellite orbit around another object, namely car.
***********************************************************************************/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#define WINDOW_WIDTH 1400
#define WINDOW_HEIGHT 800
#define TIMER_PERIOD 16 // Period for the timer. // 16
#define TIMER_ON true // false:disable timer, true:enable timer
#define D2R 0.0174533
#define MAX_ARR 30
#define AMMO 20
#define MAX_OBJECT 3
#define PI 3.1415
// Global Variables for Template File
bool up, down, right, left;
int winWidth, winHeight; // current Window width and height
// Mehmet Bulut's Global Declarations of Variables and Structures
bool fire, fire2, flow, game_over; // NOT PAUSED
bool first_time = true, bullet_display, object_moves;
int cnt_bullet, cnt_object, cnt_motion_bullet;
double degree, motion_velocity, angle;
//double coefficient[MAX_OBJECT];
//bool allowance;
//double min_distance;
enum type_e{garbage, plane, car, star, satellite};
enum move_type_e{standard, motion};
typedef struct{
double x, y;
} point_t;
typedef struct{
point_t velocity;
double initial_angle;
double degree;
} motion_t;
typedef struct{
point_t pos;
double angle; // in degree form NOT radius
double speed;
type_e type;
double orbit_angle; // if it is a plane
motion_t motion;
point_t arr[MAX_ARR]; // holds the points that will be used for collision detection
point_t center;
int size; // size of the array
} object_t;
typedef struct{
double r, g, b; // between 0 and 1
} color_t;
object_t tank;
point_t p_onClick, p_onMove, p_onMoveDown, centre; // static mouse pointer // dynamic mouse pointer
//******************************LINKED_LIST_LIBRARY****************************************************
typedef object_t LType;
typedef struct node_s
{
LType data;
struct node_s *next;
} node_t;
node_t *getnode(void)
{
node_t *node;
node = (node_t *)malloc(sizeof(node_t));
node->next = NULL;
return (node);
}
void addAfter(node_t *p, LType item)
{
node_t *newp;
newp = getnode();
newp->data = item;
newp->next = p->next;
p->next = newp;
}
node_t *addBeginning(node_t *headp, LType item)
{
node_t *newp;
newp = getnode();
newp->data = item;
newp->next = headp;
return (newp);
}
node_t *deleteFirst(node_t *headp, LType *item)
{
node_t *del = headp;
*item = del->data;
headp = del->next;
free(del);
return (headp);
}
void deleteAfter(node_t *p, LType *item)
{
node_t *del;
del = p->next;
*item = del->data;
p->next = del->next;
free(del);
}
node_t *destroy(node_t *hp)
{
node_t *del;
while( hp != NULL)
{
del = hp;
hp = hp->next;
free(del);
}
return hp;
}
//function that displays the list
void displayList(node_t *hp)
{
hp = hp->next;
if (hp == NULL)
printf("The List is EMPTY !!!\n");
else
{
while (hp != NULL)
{
printf("x: %4.0f y:%4.0f angle: %4.0f speed: %.0f ->\n", hp->data.pos.x, hp->data.pos.y, hp->data.angle, hp->data.speed);
hp = hp->next;
}
printf("NULL\n");
}
}
void displayMotionBulletList(node_t *hp)
{
hp = hp->next;
if (hp == NULL)
printf("The List is EMPTY !!!\n");
else
{
while (hp != NULL)
{
printf("velocity:\tx: %4.0f y:%4.0f angle: %4.0f ->\n", hp->data.motion.velocity.x, hp->data.motion.velocity.y, hp->data.angle);
hp = hp->next;
}
printf("NULL\n");
}
}
void displayListObjects(node_t *hp)
{
//hp = hp->next;
if (hp == NULL)
printf("The List is EMPTY !!!\n");
else
{
while (hp != NULL)
{
switch(hp->data.type)
{
case garbage: printf("garbage\n"); break;
case plane: printf("plane\n"); break;
case car: printf("car\n"); break;
case star: printf("star\n"); break;
case satellite: printf("satellite\n"); break;
}
hp = hp->next;
}
printf("NULL\n");
}
}
//******************************************************************************************************
node_t *bullet_list = NULL, *object_list = NULL, *motion_bullet_list = NULL;
double A, //amplitude
fq, //frequency
C, //horizontal phase shift
B; //vertical phase shift
double f(double x) {
return A * sin((fq * x + C) * D2R) + B;
}
void InitObjects()
{
object_t object;
object = { {-50, 50}, 0, 5, plane, 0};
addAfter(object_list, object);
double coefficient_car = 12;
object = { {-WINDOW_WIDTH/2-14.5*coefficient_car, 100}, 0, 5, car};
addAfter(object_list, object);
object = { {-50, 50}, 0, 5, star, 0};
addAfter(object_list, object);
object = { {0, 0}, 0, 5, satellite, 0};
addAfter(object_list, object);
}
void Init()
{
up = false, down = false, right = false, left = false;
fire = false, fire2 = false, flow = false, game_over = false, bullet_display = true, object_moves = false;
cnt_bullet = 0, cnt_object = 2, cnt_motion_bullet = 0, motion_velocity = 20, degree = 0;
A = 100, //amplitude
fq = 1, //frequency
C = 0, //horizontal phase shift
B = 0, //vertical phase shift
angle = 0;
//min_distance = 150;
tank = {{WINDOW_WIDTH/5, -WINDOW_HEIGHT/2+60}, 360, 5}; // pos, angle, speed
for (int i = 0; i < MAX_ARR; i++)
tank.arr[i] = {0, 0};
object_t object;
if( first_time )
{
// UNUSED GARBAGE BULLET AND OBJECT INSERTED TO THE LIST
bullet_list = addBeginning(bullet_list, tank);
motion_bullet_list = addBeginning(motion_bullet_list, tank);
object = { {-50, 50}, 0, 5, garbage};
object_list = addBeginning(object_list, object);
}
// Smoothing shapes
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void drawCircle( point_t p, double r, color_t color )
{
float angle ;
glColor3d(color.r, color.g, color.b);
glLineWidth(1);
glPointSize(1);
glBegin( GL_POLYGON ) ;
for ( int i = 0 ; i < 100 ; i++ )
{
angle = 2*PI*i/100;
glVertex2f( p.x+r*cos(angle), p.y+r*sin(angle)) ;
}
glEnd();
}
void drawCircleWire(point_t p, double r, color_t color, double thickness)
{
float angle;
glLineWidth(thickness); // change the thickness of circle_wire
glColor3d(color.r, color.g, color.b);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 100; i++){
angle = 2 * PI*i / 100;
glVertex2f(p.x + r*cos(angle), p.y + r*sin(angle));
}
glEnd();
}
void print(int x, int y, const char *string, void *font )
{
int len, i ;
glRasterPos2f( x, y );
len = (int) strlen( string );
for ( i =0; i<len; i++ )
glutBitmapCharacter( font, string[i]);
}
void vprint(int x, int y, void *font, const char *string , ...)
{
// display text with variables.
// vprint(-winWidth / 2 + 10, winHeight / 2 - 20, GLUT_BITMAP_8_BY_13, "ERROR: %d", numClicks);
// OpenGL coordinate system
va_list ap;
va_start ( ap, string );
char str[1024] ;
vsprintf( str, string, ap ) ;
va_end(ap) ;
int len, i ;
glRasterPos2f( x, y );
len = (int) strlen( str );
for ( i =0; i<len; i++ )
glutBitmapCharacter( font, str[i]);
}
void vprint2(int x, int y, float size, const char *string, ...)
{
// vprint2(-50, 0, 0.35, "00:%02d", timeCounter);
va_list ap;
va_start(ap, string);
char str[1024];
vsprintf(str, string, ap);
va_end(ap);
glPushMatrix();
glTranslatef(x, y, 0);
glScalef(size, size, 1);
int len, i;
len = (int)strlen(str);
for (i = 0; i<len; i++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, str[i]);
glPopMatrix();
}
// distance between two points.
double distance( point_t p1, point_t p2 )
{
return sqrtf( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) );
}
// p1 and p2 is a line. Likewise p3 and p4 is a line
bool intersect(point_t p1, point_t p2, point_t p3, point_t p4)
{
double t1_numerator = (p4.x-p3.x)*(p3.y-p1.y) - (p4.y-p3.y) * (p3.x-p1.x);
double t2_numerator = (p2.x-p1.x)*(p3.y-p1.y) - (p2.y-p1.y) * (p3.x-p1.x);
double t_denominator = (p4.x-p3.x)*(p2.y-p1.y) - (p4.y-p3.y) * (p2.x-p1.x);
double t1 = t1_numerator / t_denominator;
double t2 = t2_numerator / t_denominator;
if( 0.0 <= t1 && t1 <= 1.0 && 0.0 <= t2 && t2 <= 1.0 )
return true;
else
return false;
}
// check if two array of points in the form of GL_LINE_LOOP intersect or not
bool collide(point_t arr_i[], point_t center_i, int size_i, point_t arr_k[], point_t center_k, int size_k)
{
for(int i = 0; i < size_i; i++)
for(int k = 0; k < size_k; k++)
if( intersect( arr_i[i%size_i], arr_i[(i+1)%size_i], arr_k[k%size_k], arr_k[(k+1)%size_k] ) )
return true;
return false;
}
// Apply translate (move) and rotation transformations
point_t vertex(point_t P, point_t Tr, double angle)
{
point_t result;
result.x = (P.x * cos(angle) - P.y * sin(angle)) + Tr.x;
result.y = (P.x * sin(angle) + P.y * cos(angle)) + Tr.y;
glVertex2d(result.x, result.y);
return result;
}
// vertex WITHOUT glVertex2d
point_t rotateTransfer(point_t P, point_t Tr, double angle)
{
point_t result;
result.x = (P.x * cos(angle) - P.y * sin(angle)) + Tr.x;
result.y = (P.x * sin(angle) + P.y * cos(angle)) + Tr.y;
return result;
}
void drawEllipse(point_t p, point_t radius, color_t color) // radius = width-height
{
double angle = PI/180;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f( color.r, color.g, color.b, 0.7 );
glLineWidth(3);
glBegin( GL_POLYGON );
for(int t = 0; t <= 360; t+=5)
glVertex2f(radius.x*cos(t*angle) + p.x, radius.y*sin(t*angle) + p.y);
glEnd();
glDisable(GL_BLEND);
}
void drawEllipseWire(point_t p, point_t radius, color_t color) // radius = width-height
{
double angle = PI/180;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f( color.r, color.g, color.b, 0.7 );
glLineWidth(3);
glBegin( GL_LINE_STRIP );
for(int t = 0; t <= 360; t+=5)
glVertex2f(radius.x*cos(t*angle) + p.x, radius.y*sin(t*angle) + p.y);
glEnd();
glDisable(GL_BLEND);
}
void drawTank(object_t tank)
{
double angle = tank.angle * D2R;
double temp_angle = angle;
double coefficient = 7;
point_t pos = tank.pos;
glPointSize(1);
//angle = 0;
// Main body
glColor3f(1, 1, 0);
glBegin(GL_QUADS);
vertex({5*coefficient, 4*coefficient}, pos, angle);
vertex({-5*coefficient, 4*coefficient}, pos, angle);
vertex({-5*coefficient, -5*coefficient}, pos, angle);
vertex({5*coefficient, -5*coefficient}, pos, angle);
glEnd();
// Front
glColor3f(0.5, 0.5, 0.5);
glBegin(GL_QUADS);
vertex({-1*coefficient, 6*coefficient}, pos, angle);
vertex({-4*coefficient, 4*coefficient}, pos, angle);
vertex({4*coefficient, 4*coefficient}, pos, angle);
vertex({1*coefficient, 6*coefficient}, pos, angle);
glEnd();
// Back
glColor3f(0.5, 0.5, 0.5);
glBegin(GL_QUADS);
vertex({-3*coefficient, -5*coefficient}, pos, angle);
vertex({-2*coefficient, -6*coefficient}, pos, angle);
vertex({2*coefficient, -6*coefficient}, pos, angle);
vertex({3*coefficient, -5*coefficient}, pos, angle);
glEnd();
// Wings
// Right Wing
glColor3f(0, 0.5, 0);
glBegin(GL_QUADS);
vertex({-5*coefficient, +8*coefficient}, pos, angle);
vertex({-9*coefficient, +8*coefficient}, pos, angle);
vertex({-9*coefficient, -8*coefficient}, pos, angle);
vertex({-5*coefficient, -8*coefficient}, pos, angle);
glEnd();
// Left Wing
glBegin(GL_QUADS);
vertex({5*coefficient, +8*coefficient}, pos, angle);
vertex({9*coefficient, +8*coefficient}, pos, angle);
vertex({9*coefficient, -8*coefficient}, pos, angle);
vertex({5*coefficient, -8*coefficient}, pos, angle);
glEnd();
angle = temp_angle;
// Firing part of the tank
glColor3f(0, 1, 0);
glBegin(GL_QUADS);
vertex({+1*coefficient, +17*coefficient}, pos, angle);
vertex({-1*coefficient, +17*coefficient}, pos, angle);
vertex({-1*coefficient, +3*coefficient}, pos, angle);
vertex({+1*coefficient, +3*coefficient}, pos, angle);
glEnd();
// Top Turner
glColor3f(0, 0, 1);
glBegin(GL_POLYGON);
vertex({+2*coefficient, +3*coefficient}, pos, angle);
vertex({-2*coefficient, +3*coefficient}, pos, angle);
vertex({-3*coefficient, +2*coefficient}, pos, angle);
vertex({-3*coefficient, -2*coefficient}, pos, angle);
vertex({-2*coefficient, -3*coefficient}, pos, angle);
vertex({+2*coefficient, -3*coefficient}, pos, angle);
vertex({+3*coefficient, -2*coefficient}, pos, angle);
vertex({+3*coefficient, +2*coefficient}, pos, angle);
glEnd();
}
void drawBullet(object_t bullet, point_t arr[], int *size_arr, point_t *center)
{
double angle = bullet.angle * D2R;
double coefficient = 7;
point_t pos = bullet.pos;
glPointSize(1);
glColor3f(1, 0, 1);
glBegin(GL_QUADS);
arr[3] = vertex({0.5*coefficient, -1.5*coefficient}, pos, angle);
arr[4] = vertex({0.5*coefficient, 0.5*coefficient}, pos, angle);
arr[1] = vertex({-0.5*coefficient, 0.5*coefficient}, pos, angle);
arr[2] = vertex({-0.5*coefficient, -1.5*coefficient}, pos, angle);
glEnd();
glColor3f(0, 1, 1);
glBegin(GL_TRIANGLES);
arr[0] = vertex({0*coefficient, 1.5*coefficient}, pos, angle);
arr[1] = vertex({-0.5*coefficient, 0.5*coefficient}, pos, angle);
arr[4] = vertex({0.5*coefficient, 0.5*coefficient}, pos, angle);
glEnd();
// middle
*center = rotateTransfer({0*coefficient, 0*coefficient}, pos, angle);
*size_arr = 5;
}
void drawPlane(object_t plane, point_t arr[], int *size_arr, point_t *center)
{
double angle = plane.angle * D2R;
double coefficient = 7;
point_t pos = plane.pos;
glPointSize(1);
// nose
glColor3f(1, 1, 0);
glBegin(GL_TRIANGLES);
arr[0] = vertex({0*coefficient,11*coefficient}, pos, angle);
arr[1] = vertex({-3*coefficient,6*coefficient}, pos, angle);
arr[17] = vertex({3*coefficient,6*coefficient}, pos, angle);
glEnd();
// main body
glColor3f(1, 0, 1);
glBegin(GL_QUADS);
arr[1] = vertex({-3*coefficient,6*coefficient}, pos, angle);
arr[6] = vertex({-3*coefficient,-5*coefficient}, pos, angle);
arr[12] = vertex({3*coefficient,-5*coefficient}, pos, angle);
arr[17] = vertex({3*coefficient,6*coefficient}, pos, angle);
glEnd();
// wings
// left wing
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
arr[2] = vertex({-3*coefficient,2*coefficient}, pos, angle);
arr[3] = vertex({-11*coefficient,0*coefficient}, pos, angle);
arr[4] = vertex({-11*coefficient,-2*coefficient}, pos, angle);
arr[5] = vertex({-3*coefficient,-2*coefficient}, pos, angle);
glEnd();
// right wing
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
arr[16] = vertex({3*coefficient,2*coefficient}, pos, angle);
arr[15] = vertex({11*coefficient,0*coefficient}, pos, angle);
arr[14] = vertex({11*coefficient,-2*coefficient}, pos, angle);
arr[13] = vertex({3*coefficient,-2*coefficient}, pos, angle);
glEnd();
// back wings
glColor3f(0.5, 0.5, 1);
glBegin(GL_POLYGON);
arr[6] = vertex({-3*coefficient,-5*coefficient}, pos, angle);
arr[7] = vertex({-6*coefficient,-9*coefficient}, pos, angle);
arr[8] = vertex({-4*coefficient,-11*coefficient}, pos, angle);
arr[9] = vertex({0*coefficient,-7*coefficient}, pos, angle);
arr[10] = vertex({4*coefficient,-11*coefficient}, pos, angle);
arr[11] = vertex({6*coefficient,-9*coefficient}, pos, angle);
arr[12] = vertex({3*coefficient,-5*coefficient}, pos, angle);
glEnd();
//middle
*center = rotateTransfer({0,0}, pos, angle);
*size_arr = 18;
}
void drawCar(object_t car, point_t arr[], int *size_arr, point_t *center)
{
double angle = car.angle * D2R;
double coefficient = 12;
point_t pos = car.pos;
glPointSize(1);
glBegin(GL_QUADS);
// top body
glColor3f(0, 0.5, 1);
arr[0] = vertex({-6*coefficient, 6*coefficient}, pos, angle);
arr[1] = vertex({-8*coefficient, 2*coefficient}, pos, angle);
arr[12] = vertex({8*coefficient, 2*coefficient}, pos, angle);
arr[13] = vertex({6*coefficient, 6*coefficient}, pos, angle);
// main body
glColor3f(0.5, 0, 0);
arr[2] = vertex({-10*coefficient, 2*coefficient}, pos, angle);
arr[3] = vertex({-10*coefficient, -5*coefficient}, pos, angle);
arr[10] = vertex({10*coefficient, -5*coefficient}, pos, angle);
arr[11] = vertex({10*coefficient, 2*coefficient}, pos, angle);
// left window
glColor3f(0.8, 0.8, 1);
vertex({-5*coefficient, 5*coefficient}, pos, angle);
vertex({-6*coefficient, 3*coefficient}, pos, angle);
vertex({-3*coefficient, 3*coefficient}, pos, angle);
vertex({-2*coefficient, 5*coefficient}, pos, angle);
// right window
glColor3f(0.8, 0.8, 1);
vertex({2*coefficient, 5*coefficient}, pos, angle);
vertex({3*coefficient, 3*coefficient}, pos, angle);
vertex({6*coefficient, 3*coefficient}, pos, angle);
vertex({5*coefficient, 5*coefficient}, pos, angle);
glEnd();
drawCircleWire(vertex({-5*coefficient, -5*coefficient}, pos, angle), 3*coefficient, {0.5,0.5,0.5}, 5);
drawCircleWire(vertex({5*coefficient, -5*coefficient}, pos, angle), 3*coefficient, {0.5,0.5,0.5}, 5);
glLineWidth(1);
glBegin(GL_LINES);
glColor3f(1, 1, 1);
// left window line
vertex({-5*coefficient, 5*coefficient}, pos, angle);
vertex({-3*coefficient, 3*coefficient}, pos, angle);
// right window line
vertex({3*coefficient, 3*coefficient}, pos, angle);
vertex({5*coefficient, 5*coefficient}, pos, angle);
glEnd();
glLineWidth(5);
glColor3f(0.5, 0.5, 0.5);
glBegin(GL_LINES);
// left tire line
arr[4] = vertex({-8*coefficient, -5.3*coefficient}, pos, angle);
arr[6] = vertex({-2*coefficient, -5.3*coefficient}, pos, angle);
arr[5] = vertex({-5*coefficient, -8*coefficient}, pos, angle);
vertex({-5*coefficient, -2*coefficient}, pos, angle);
// right tire line
arr[7] = vertex({2*coefficient, -5.3*coefficient}, pos, angle);
arr[9] = vertex({8*coefficient, -5.3*coefficient}, pos, angle);
arr[8] = vertex({5*coefficient, -8*coefficient}, pos, angle);
vertex({5*coefficient, -2*coefficient}, pos, angle);
glEnd();
glColor3f(0.5, 0, 0);
glBegin(GL_TRIANGLES);
vertex({0*coefficient, 1*coefficient}, pos, angle);
vertex({-3*coefficient, -2*coefficient}, pos, angle);
vertex({3*coefficient, -2*coefficient}, pos, angle);
glEnd();
glColor3f(1, 1, 1);
point_t printp = rotateTransfer({-2*coefficient, -1.5*coefficient}, pos, angle);
vprint( printp.x, printp.y, GLUT_BITMAP_8_BY_13,"My Car");
// middle
*center = rotateTransfer({0*coefficient, 0*coefficient}, pos, angle);
*size_arr = 14;
}
void drawStar(object_t star, point_t arr[], int *size_arr, point_t *center)
{
double angle = star.angle * D2R;
double coefficient = 10;
point_t pos = star.pos;
glColor3d(1, 1, 0);
glBegin( GL_TRIANGLES );
// 1
arr[0] = vertex({0*coefficient, 8*coefficient}, pos, angle);
vertex({-4*coefficient, 0*coefficient}, pos, angle);
vertex({4*coefficient, 0*coefficient}, pos, angle);
arr[1] = rotateTransfer({2*coefficient, 4.5*coefficient}, pos, angle);
// 2
arr[2] = vertex({-6*coefficient, 6*coefficient}, pos, angle);
vertex({-3*coefficient, -3*coefficient}, pos, angle);
vertex({3*coefficient, 3*coefficient}, pos, angle);
arr[3] = rotateTransfer({-4.5*coefficient, 2*coefficient}, pos, angle);
// 3
arr[4] = vertex({-8*coefficient, 0*coefficient}, pos, angle);
vertex({0*coefficient, -4*coefficient}, pos, angle);
vertex({0*coefficient, 4*coefficient}, pos, angle);
arr[5] = rotateTransfer({-4.5*coefficient, -2*coefficient}, pos, angle);
// 4
arr[6] = vertex({-6*coefficient, -6*coefficient}, pos, angle);
vertex({3*coefficient, -3*coefficient}, pos, angle);
vertex({-3*coefficient, 3*coefficient}, pos, angle);
arr[7] = rotateTransfer({0*coefficient, 8*coefficient}, pos, angle);
// 5
arr[8] = vertex({0*coefficient, -8*coefficient}, pos, angle);
vertex({4*coefficient, 0*coefficient}, pos, angle);
vertex({-4*coefficient, 0*coefficient}, pos, angle);
arr[9] = rotateTransfer({-2*coefficient, -4.5*coefficient}, pos, angle);
// 6
arr[10] = vertex({6*coefficient, -6*coefficient}, pos, angle);
vertex({3*coefficient, 3*coefficient}, pos, angle);
vertex({-3*coefficient, -3*coefficient}, pos, angle);
arr[11] = rotateTransfer({4.5*coefficient, -2*coefficient}, pos, angle);
// 7
arr[12] = vertex({8*coefficient, 0*coefficient}, pos, angle);
vertex({0*coefficient, 4*coefficient}, pos, angle);
vertex({0*coefficient, -4*coefficient}, pos, angle);
arr[13] = rotateTransfer({4.5*coefficient, 2*coefficient}, pos, angle);
// 8
arr[14] = vertex({6*coefficient, 6*coefficient}, pos, angle);
vertex({-3*coefficient, 3*coefficient}, pos, angle);
vertex({3*coefficient, -3*coefficient}, pos, angle);
arr[15] = rotateTransfer({2*coefficient, 4.5*coefficient}, pos, angle);
glEnd();
//arr[-0] = vertex({-0*coefficient, -0*coefficient}, pos, angle);
*center = rotateTransfer({0*coefficient, 0*coefficient}, pos, angle);
*size_arr = 16;
}
void drawSatellite(object_t satellite, point_t arr[], int *size_arr, point_t *center)
{
double angle = satellite.angle * D2R;
double coefficient = 4;
point_t pos = satellite.pos;
//drawEllipse({0}, {10*coefficient+pos.x, 5*coefficient+pos.y}, {1, 0, 0});
glLineWidth(50);
glColor3f(1, 0, 1);
glBegin(GL_LINE_STRIP);
arr[2] = vertex({-6*coefficient, -3*coefficient}, pos, angle);
arr[1] = vertex({-6*coefficient, 10*coefficient}, pos, angle);
arr[0] = vertex({-10*coefficient, 10*coefficient}, pos, angle);
glEnd();
glColor3f(1, 0, 1);
glBegin(GL_LINE_STRIP);
arr[3] = vertex({6*coefficient, -3*coefficient}, pos, angle);
arr[4] = vertex({6*coefficient, 10*coefficient}, pos, angle);
arr[5] = vertex({10*coefficient, 10*coefficient}, pos, angle);
glEnd();
glLineWidth(1);
glColor3f(0, 1, 1);
glBegin(GL_QUADS);
arr[2] = vertex({-6*coefficient, -3*coefficient}, pos, angle);
arr[3] = vertex({6*coefficient, -3*coefficient}, pos, angle);
arr[6] = vertex({6*coefficient, 3*coefficient}, pos, angle);
arr[7] = vertex({-6*coefficient, 3*coefficient}, pos, angle);
glEnd();
// middle
*center = rotateTransfer({0*coefficient, 0*coefficient}, pos, angle);
*size_arr = 8;
}
void display()
{
glClearColor( 0, 0 , 0 , 0 );
glClear( GL_COLOR_BUFFER_BIT );
// BARRRIER
glLineWidth(5);
glColor3f(1, 1, 1);
glBegin(GL_LINE_LOOP);
glVertex2d(WINDOW_WIDTH/2-1, WINDOW_HEIGHT/2-1);
glVertex2d(-WINDOW_WIDTH/2+1, WINDOW_HEIGHT/2-1);
glVertex2d(-WINDOW_WIDTH/2, -WINDOW_HEIGHT/2);
glVertex2d(WINDOW_WIDTH/2, -WINDOW_HEIGHT/2);
glEnd();
glLineWidth(1);
glColor3f( 1, 1, 1 );
if(first_time)
vprint( -100, 0, GLUT_BITMAP_TIMES_ROMAN_24,"PRESS F1 TO START...");
else if(game_over)
vprint( -100, 0, GLUT_BITMAP_TIMES_ROMAN_24,"GAME OVER! YOU WIN!!");
drawTank(tank);
node_t *addr = bullet_list->next;
while(addr != NULL)
{
drawBullet(addr->data, addr->data.arr, &addr->data.size, &addr->data.center);
addr = addr->next;
}
node_t *motion_addr = motion_bullet_list->next;
while(motion_addr != NULL)
{
drawBullet(motion_addr->data, motion_addr->data.arr, &motion_addr->data.size, &motion_addr->data.center);
//glColor3f(1, 1, 1);
//for (int i = 0; i < motion_addr->data.size; i++)
//vprint( motion_addr->data.arr[i].x, motion_addr->data.arr[i].y, GLUT_BITMAP_8_BY_13,"%d", i);
//vprint( motion_addr->data.center.x, motion_addr->data.center.y, GLUT_BITMAP_8_BY_13,"center");
motion_addr = motion_addr->next;
}
double temp_x;
node_t *addr2 = object_list->next;
while(object_moves && addr2 != NULL)
{
if(addr2->data.type == plane)
drawPlane(addr2->data, addr2->data.arr, &addr2->data.size, &addr2->data.center);
else if(addr2->data.type == car)
{
centre = addr2->data.pos;
drawCar(addr2->data, addr2->data.arr, &addr2->data.size, &addr2->data.center);
}
else if(addr2->data.type == star)
drawStar(addr2->data, addr2->data.arr, &addr2->data.size, &addr2->data.center);
else if (addr2->data.type == satellite)
drawSatellite(addr2->data, addr2->data.arr, &addr2->data.size, &addr2->data.center);
addr2 = addr2->next;
}
// BACKGROUND OF DIRECTIVES
glColor3f( 0.5, 0.5, 0.5 );
glRectf(-WINDOW_WIDTH/2, WINDOW_HEIGHT/2-80, WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
// KEYBOARD AND MOUSE DIRECTIVES
glColor3f( 1, 1, 1 );
vprint( -WINDOW_WIDTH/2+15, WINDOW_HEIGHT/2-55, GLUT_BITMAP_HELVETICA_18,"<Spacebar,Left Click> Fire; <R,r> Motion_Bullet_Fire; <F1> Pause/Restart; <Up/Down,A,a/D,d Arrow Keys> Right/Left Move; <Right/Left Arrow Keys> Right/Left Turn");
// DYNAMIC MOUSE POSITION
glColor3f( 0, 1, 1 );
vprint( WINDOW_WIDTH/2-WINDOW_WIDTH/5, -WINDOW_HEIGHT/2+WINDOW_HEIGHT/14, GLUT_BITMAP_8_BY_13,"Mouse Pointer( x: %3.0f y: %3.0f )", p_onMove.x, p_onMove.y);
// TANK POSITION AND ANGLE
glColor3f( 1, 0, 1 );
vprint( WINDOW_WIDTH/2-WINDOW_WIDTH/6, +WINDOW_HEIGHT/2-WINDOW_HEIGHT/8, GLUT_BITMAP_8_BY_13,"Tank Pos( x: %3.0f y: %3.0f )", tank.pos.x, tank.pos.y);
glColor3f( 1, 1, 0 );
vprint( WINDOW_WIDTH/2-WINDOW_WIDTH/10, -WINDOW_HEIGHT/2+WINDOW_HEIGHT/10, GLUT_BITMAP_8_BY_13,"Tank angle: %.0f", tank.angle);
glColor3f( 1, 1, 1 );
vprint( WINDOW_WIDTH/2-WINDOW_WIDTH/4, -WINDOW_HEIGHT/2+WINDOW_HEIGHT/8, GLUT_BITMAP_8_BY_13,"motion_bullet velocity (+,-) to change %.0f", motion_velocity);
/*
glPointSize(10);
glColor3f(1, 1, 1);
glBegin(GL_POINTS);
glVertex2d(0, 0);
glEnd();
glPointSize(1);
//glColor3f(1, 1, 1);
//for (int i = 0; i < addr->data.size; i++)
//vprint( addr->data.arr[i].x, addr->data.arr[i].y, GLUT_BITMAP_8_BY_13,"%d", i);
//vprint( addr->data.center.x, addr->data.center.y, GLUT_BITMAP_8_BY_13,"center");
*/
glutSwapBuffers();
}
void move(point_t *pos, double *angle, double *speed)
{
pos->x += ( *speed * cos( (*angle + 90) * D2R ) );
pos->y += ( *speed * sin( (*angle + 90) * D2R ) );
if (*angle < 0)
*angle += 360;
if (*angle >= 360)
*angle -= 360;
}
point_t move_return(point_t pos, double *angle, double speed)
{
pos.x += ( speed * cos( (*angle + 90) * D2R ) );
pos.y += ( speed * sin( (*angle + 90) * D2R ) );
if (*angle < 0)
*angle += 360;
if (*angle >= 360)
*angle -= 360;
return pos;
}
void moveReverse(point_t *pos, double *angle, double *speed)
{
pos->x -= ( *speed * cos( (*angle + 90) * D2R ) );
pos->y -= ( *speed * sin( (*angle + 90) * D2R ) );
if (*angle < 0)
*angle += 360;
if (*angle >= 360)
*angle -= 360;
}
void move_x(point_t *pos, double *speed)
{
pos->x += *speed;
}
void move_y(point_t *pos, double *speed)
{
pos->y += *speed;
}
void motion_move(point_t *pos, double *angle, point_t *velocity)
{
pos->x += ( (*velocity).x * cos( (*angle + 90) * D2R ) );
pos->y += ( (*velocity).y * sin( (*angle + 90) * D2R ) );
if (*angle < 0)
*angle += 360;
if (*angle >= 360)
*angle -= 360;
}
void moveCircular( point_t orbit_center, point_t *pos, double radius, double *angle)
{
orbit_center.x += radius*cos(*angle*D2R);
orbit_center.y += radius*sin(*angle*D2R);
*pos = orbit_center;
(*angle)++;
}
void moveSinusoidal(point_t *pos, double *x)
{
(*pos).x = *x;
(*pos).y = f(*x);
if( (*pos).x <= +WINDOW_WIDTH/2 + 70 )
(*x)+= 5;
else
(*x) = -WINDOW_WIDTH/2 - 70;
}
void turn(double *angle, double turn)
{
*angle += turn;
if (*angle < -360)
*angle += 720;
if (*angle >= 720)
*angle -= 720;
}
// FOR TANK // boundaries could be 0 - 359
void turn_limited(double *angle, double turn)
{
//if(185-turn <= tank->angle && tank->angle <= 355-turn)
if(0 < (*angle) && (*angle) <= 90-turn || 270-turn <= (*angle) && (*angle) <= 360)
*angle += turn;
if(*angle == 0)
*angle = 360;
if (*angle < 0)
*angle += 360;
if (*angle >= 360)
*angle -= 360;
}
void onKeyDown(unsigned char key, int x, int y )
{
//CALLER FUNCTION: glutKeyboardFunc(onKeyDown);
// The x and y callback parameters indicate the mouse location in window relative coordinates when the key was pressed.
// key: integer value of ASCII characters like ESC, a,b,c..,A,B,..Z etc.
// exit when ESC is pressed.
if ( key == 27 )
exit(0);
if( key == 'R' | key == 'r' )
if(flow)
fire2 = true;
switch( key ){
case 'D': case 'd': up = true; break;
case 'A': case 'a': down = true; break;
case ' ':
if(flow)
fire = true;
break;
case '+':
{
motion_velocity++;
} break;
case '-':
{
motion_velocity--;
} break;
}
glutPostRedisplay(); // to refresh the window it calls display() function
}
void onKeyUp(unsigned char key, int x, int y )
{
//CALLER FUNCTION: glutKeyboardUpFunc(onKeyUp);
// The x and y callback parameters indicate the mouse location in window relative coordinates when the key was pressed.
// key: integer value of ASCII characters like ESC, a,b,c..,A,B,..Z etc.
// if exit() function executed, after a key pressed assumed released happened inevitably
// exit when ESC is pressed.
if ( key == 27 )
exit(0);
if( key == 'R' |- key == 'r' )
fire2 = false;
switch( key ){
case 'D': case 'd': up = false; break;
case 'A': case 'a': down = false; break;
case ' ': fire = false; break;
}
glutPostRedisplay(); // to refresh the window it calls display() function
}
void onSpecialKeyDown( int key, int x, int y )
{
//CALLER FUNCTION: glutSpecialFunc(onSpecialKeyDown);
// The x and y callback parameters indicate the mouse location in window relative coordinates when the key was pressed.
// key:
// Special Key like GLUT_KEY_F1, F2, F3,...
// Arrow Keys, GLUT_KEY_UP, GLUT_KEY_DOWN, GLUT_KEY_RIGHT, GLUT_KEY_RIGHT
switch (key) {
case GLUT_KEY_UP: up = true; break;
case GLUT_KEY_DOWN: down = true; break;
case GLUT_KEY_LEFT: left = true; break;
case GLUT_KEY_RIGHT: right = true; break;
case GLUT_KEY_F1: {
flow = !flow;
if(first_time)
{
first_time = false;
flow = true;
}
if( game_over )
{
game_over = false;
Init();
InitObjects();
flow = true;