-
Notifications
You must be signed in to change notification settings - Fork 1
/
asteroids.c
822 lines (687 loc) · 21.9 KB
/
asteroids.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <GL/glut.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define RAD2DEG 180.0/M_PI
#define DEG2RAD M_PI/180.0
#define myTranslate2D(x,y) glTranslated(x, y, 0.0)
#define myScale2D(x,y) glScalef(x, y, 1.0)
#define myRotate2D(angle) glRotatef(RAD2DEG*angle, 0.0, 0.0, 1.0)
#define TIME_DELTA 33
#define SPAWN_ASTEROID_PROB 0.004
#define MAX_SHIP_VELOCITY 0.05
#define PHOTON_VELOCITY MAX_SHIP_VELOCITY * 2
#define PHOTON_LENGTH 2.5
#define MAX_PHOTONS 8
#define MAX_ASTEROIDS 8
#define MAX_VERTICES 16
/* -- type definitions ------------------------------------------------------ */
typedef struct Coords {
double x, y;
} Coords;
typedef struct {
int status; /* status: 0 = destroyed 1 = active, normal 2 = invincible */
double x, y, phi, dx, dy, radius, turnSpeed, acceleration;
Coords coords[3];
} Ship;
typedef struct {
int active;
double x1, y1, x2, y2, dx, dy, phi;
} Photon;
typedef struct {
int active, nVertices;
double x, y, phi, dx, dy, dphi, radius;
Coords coords[MAX_VERTICES];
} Asteroid;
/* -- function prototypes --------------------------------------------------- */
static void myDisplay(void);
static void myTimer(int value);
static void myPauseTimer(int value);
static void myMenuTimer(int value);
static void myKey(unsigned char key, int x, int y);
static void keyPress(int key, int x, int y);
static void keyRelease(int key, int x, int y);
static void myReshape(int w, int h);
static void init(void);
static void initAsteroid(Asteroid *a, double x, double y, double size);
static void drawShip(Ship *s);
static void drawPhoton(Photon *p);
static void drawAsteroid(Asteroid *a);
static void debug(void);
static void updateShip(void);
static void processUserInput(void);
static void updatePhotons(void);
static void spawnAsteroids(void);
static void advanceAsteroids(void);
static void destroyAndResetShip(void);
static double myRandom(double min, double max);
static double clamp(double value, double min, double max);
static void initPhoton(void);
static int isInBounds(double x, double y);
static void raycastForNewCoordinates(double x, double y, double dx, double dy, double * newCoords);
static void checkPhotonAsteroidCollision(void);
static void checkShipAsteroidCollision(void);
static void resetAsteroidShape(void);
/* -- global variables ------------------------------------------------------ */
static int up = 0, down = 0, left = 0, right = 0, firing = 0, circularAsteroids = 0, pause = 0, started = 0; // state of user input
static double xMax, yMax;
static int timer = 0, respawnTimer = 0;
static Ship ship;
static Photon photons[MAX_PHOTONS];
static Asteroid asteroids[MAX_ASTEROIDS];
/* -- main ------------------------------------------------------------------ */
int
main(int argc, char *argv[])
{
srand((unsigned int) time(NULL));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Asteroids");
glutDisplayFunc(myDisplay);
glutIgnoreKeyRepeat(1);
glutKeyboardFunc(myKey);
glutSpecialFunc(keyPress);
glutSpecialUpFunc(keyRelease);
glutReshapeFunc(myReshape);
glutTimerFunc(TIME_DELTA, myMenuTimer, 0);
glClearColor(0.0, 0.0, 0.0, 1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
init();
glutMainLoop();
return 0;
}
/* ================================================ GLUT Callback Functions ============================================== */
void
myDisplay()
{
/*
* display callback function
*/
int i;
glClear(GL_COLOR_BUFFER_BIT);
drawShip(&ship);
for (i=0; i<MAX_PHOTONS; i++){
if (photons[i].active){
drawPhoton(&photons[i]);
}
}
if(started){
for (i=0; i<MAX_ASTEROIDS; i++){
if (asteroids[i].active){
drawAsteroid(&asteroids[i]);
}
}
}
glutSwapBuffers();
}
void
myTimer(int value)
{
/*
* timer callback function
*/
debug();
updatePhotons();
processUserInput();
updateShip();
spawnAsteroids();
advanceAsteroids();
checkPhotonAsteroidCollision();
checkShipAsteroidCollision();
glutPostRedisplay();
if(pause){
glutTimerFunc(TIME_DELTA, myPauseTimer, value);
} else {
glutTimerFunc(TIME_DELTA, myTimer, value);
}
}
void
myPauseTimer(int value){
if(pause){
glutTimerFunc(TIME_DELTA, myPauseTimer, value);
} else {
glutTimerFunc(TIME_DELTA, myTimer, value);
}
}
void
myMenuTimer(int value){
if(started){
glutTimerFunc(TIME_DELTA, myTimer, value);
} else {
debug();
updatePhotons();
processUserInput();
updateShip();
glutPostRedisplay();
glutTimerFunc(TIME_DELTA, myMenuTimer, value);
}
}
void
myKey(unsigned char key, int x, int y) {
/*
* keyboard callback function
*/
if(started){
switch(key) {
case ' ':
if(ship.status && !pause){
// ship can only shoot if it is "alive"
initPhoton();
}
break;
case 'q':
exit(0); break;
case 'c':
resetAsteroidShape(); break;
case 'p':
pause = abs(pause - 1); break;
}
} else {
switch(key) {
case 's':
started = 1; break;
case ' ':
initPhoton(); break;
case 'q':
exit(0); break;
}
}
}
void
keyPress(int key, int x, int y) {
/*
* this function is called when a special key is pressed; we are
* interested in the cursor keys only
*/
if(ship.status){
switch (key) {
case 100:
left = 1; break;
case 101:
up = 1; break;
case 102:
right = 1; break;
case 103:
down = 1; break;
}
}
}
void
keyRelease(int key, int x, int y) {
/*
* this function is called when a special key is released; we are
* interested in the cursor keys only
*/
switch (key) {
case 100:
left = 0; break;
case 101:
up = 0; break;
case 102:
right = 0; break;
case 103:
down = 0; break;
}
}
void
myReshape(int w, int h) {
/*
* reshape callback function; the upper and lower boundaries of the
* window are at 100.0 and 0.0, respectively; the aspect ratio is
* determined by the aspect ratio of the viewport
*/
xMax = 100.0*w/h;
yMax = 100.0;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, xMax, 0.0, yMax, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
/* ============================================ Processing Functions ======================================= */
/**
* Print debug information to the console
*/
void
debug() {
timer += TIME_DELTA;
if (timer >= 3000) {
printf("ship coords: (%.3f, %.3f)\n", ship.x, ship.y);
printf("direction: %.3f\n", ship.phi);
printf("dx: %.4f\n", ship.dx);
printf("dy: %.4f\n", ship.dy);
printf("xMax: %.3f yMax: %.3f\n", xMax, yMax);
int i, numActiveAsteroids = 0;
for(i = 0; i < MAX_ASTEROIDS; i++){
if(asteroids[i].active){
numActiveAsteroids ++;
}
}
printf("numActiveAsteroids: %d\n", numActiveAsteroids);
printf("\n\n");
timer -= 3000;
}
}
/**
* Respond to user input by turning or accelerating the ship.
*/
void
processUserInput() {
if (right){
ship.phi -= ship.turnSpeed;
if (ship.phi < 0.0){
ship.phi += 360.0;
}
}
if (left){
ship.phi += ship.turnSpeed;
if (ship.phi > 360.0){
ship.phi -= 360.0;
}
}
if (up || down){
double newDx;
double newDy;
if (up){
// up velocity calculation
newDx = ship.dx - (ship.acceleration * sin((ship.phi - 90.0) * DEG2RAD) * TIME_DELTA);
newDy = ship.dy + (ship.acceleration * cos((ship.phi - 90.0) * DEG2RAD) * TIME_DELTA);
} else {
// down velocity calculation. Braking acceleration is 33% slower for extra challenge
newDx = ship.dx + ((ship.acceleration/1.5) * sin((ship.phi - 90.0) * DEG2RAD) * TIME_DELTA);
newDy = ship.dy - ((ship.acceleration/1.5) * cos((ship.phi - 90.0) * DEG2RAD) * TIME_DELTA);
}
double velocityMagnitude = sqrt(pow(newDx, 2) + pow(newDy, 2));
if (velocityMagnitude <= MAX_SHIP_VELOCITY){
ship.dx = newDx;
ship.dy = newDy;
} else {
// new velocity will exceed max ship velocity
// subtract the difference to make it the same magnitude as the max ship velocity
velocityMagnitude -= (velocityMagnitude - MAX_SHIP_VELOCITY);
double thetaRadians = atan(newDy/newDx);
if (newDx < 0){
ship.dx = velocityMagnitude * cos(thetaRadians - M_PI);
ship.dy = velocityMagnitude * sin(thetaRadians - M_PI);
} else {
ship.dx = velocityMagnitude * cos(thetaRadians);
ship.dy = velocityMagnitude * sin(thetaRadians);
}
}
}
}
/**
* Advances the ship. This entails incrementing the ship's position and resetting the ship
* within the bounds of the screen when it leaves.
*/
void
updateShip(){
if(ship.status == 0){
// ship has been destroyed and is waiting to respawn
respawnTimer += TIME_DELTA;
if(respawnTimer >= 3000){
respawnTimer = 0;
ship.status = 2;
}
} else if(ship.status == 2){
// ship is invincible
respawnTimer += TIME_DELTA;
if (respawnTimer >= 4000){
// ship has been respawned for a while so take away invincibility
respawnTimer = 0;
ship.status = 1;
}
}
// update x and y center coordinates
ship.x += ship.dx*TIME_DELTA;
ship.y += ship.dy*TIME_DELTA;
// check if ship left the screen
if (!isInBounds(ship.x, ship.y)){
// ship is out of the screen. Need to reset the ship on the opposite side of the screen
double newCoords[2];
raycastForNewCoordinates(ship.x, ship.y, ship.dx, ship.dy, newCoords);
ship.x = newCoords[0];
ship.y = newCoords[1];
}
// update the 3 vertices that make up the ship
float thetas[3];
thetas[0] = (0.0f + ship.phi) * DEG2RAD;
thetas[1] = (135.0f + ship.phi) * DEG2RAD;
thetas[2] = (225.0f + ship.phi) * DEG2RAD;
int i;
for(i = 0; i < 3; i++){
ship.coords[i].x = ship.x + (ship.radius * cos(thetas[i]));
ship.coords[i].y = ship.y + (ship.radius * sin(thetas[i]));
}
}
/**
* Iterate over all active photons and updates their position. When an active photon has left the screen,
* it is changed to not active.
*/
void
updatePhotons(){
int i;
for(i = 0; i < MAX_PHOTONS; i++){
if(photons[i].active){
Photon p = photons[i];
p.x1 += p.dx*TIME_DELTA;
p.y1 += p.dy*TIME_DELTA;
p.x2 += p.dx*TIME_DELTA;
p.y2 += p.dy*TIME_DELTA;
if(!isInBounds(p.x1, p.y1)){
p.active = 0;
}
photons[i] = p;
}
}
}
void
spawnAsteroids(){
// determine the number of active asteroids on the screen
int i, numActiveAsteroids = 0;
for (i = 0; i < MAX_ASTEROIDS; i++){
if (asteroids[i].active){
numActiveAsteroids++;
}
}
if (numActiveAsteroids == 0 || (numActiveAsteroids < (MAX_ASTEROIDS-3) && myRandom(0.0, 1.0) < SPAWN_ASTEROID_PROB)){
Asteroid newAsteroid;
for (i = 0; i < MAX_ASTEROIDS; i++){
if (!asteroids[i].active){
newAsteroid = asteroids[i];
break;
}
}
// cast double return value to an integer, giving possible values in the range [0-3]
int spawnSide = myRandom(0.0, 3.9999);
double spawnX, spawnY;
switch (spawnSide){
case 0:
// spawn on the right side of the screen
spawnX = xMax;
spawnY = myRandom(0.0, yMax);
break;
case 1:
// spawn on the bottom side of the screen
spawnX = myRandom(0.0, xMax);
spawnY = 0.0;
break;
case 2:
// spawn on the left side of the screen
spawnX = 0.0;
spawnY = myRandom(0.0, yMax);
break;
case 3:
// spawn on the top side of the screen
spawnX = myRandom(0.0, xMax);
spawnY = yMax;
break;
}
printf("Spawning an asteroid\nspawnSide: %i\nspawnX: %.3f spawnY: %.3f\n\n", spawnSide, spawnX, spawnY);
initAsteroid(&newAsteroid, spawnX, spawnY, 2.2);
asteroids[i] = newAsteroid;
}
}
void
advanceAsteroids(){
int i;
for (i = 0; i < MAX_ASTEROIDS; i++){
if(asteroids[i].active){
//printf("updating asteroid at index %i\noldX: %.3f oldY: %.3f\n", i, asteroids[i].x, asteroids[i].y);
Asteroid a = asteroids[i];
a.x += a.dx*TIME_DELTA;
a.y += a.dy*TIME_DELTA;
a.phi += a.dphi;
//printf("newX: %.3f newY: %.3f\n", a.x, a.y);
if (a.x > xMax){
a.x = 0.0;
} else if (a.x < 0.0){
a.x = xMax;
}
if (a.y > yMax){
a.y = 0.0;
} else if (a.y < 0.0){
a.y = yMax;
}
asteroids[i] = a;
}
}
}
void
destroyAndResetShip(){
left = 0;
right = 0;
up = 0;
down = 0;
ship.status = 0;
ship.x = 66.66;
ship.y = 50.0;
ship.dx = 0.000000;
ship.dy = 0.000000;
ship.phi = 90.00000;
}
/* ============================================= Drawing Functions ========================================= */
void
drawShip(Ship *s) {
int i;
glLoadIdentity();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_LINE_LOOP);
if(s->status == 1 || s->status == 2 && respawnTimer % (TIME_DELTA*12) < (TIME_DELTA*6)){
for (i = 0; i < 3; i++){
glVertex2f(s->coords[i].x, s->coords[i].y);
}
}
glEnd();
}
void
drawPhoton(Photon *p) {
glLoadIdentity();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_LINES);
glVertex2f(p->x1, p->y1);
glVertex2f(p->x2, p->y2);
glEnd();
}
void
drawAsteroid(Asteroid *a) {
int i;
glLoadIdentity();
// move to the asteroid's x and y
myTranslate2D(a->x, a->y);
myRotate2D(a->phi);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_LINE_LOOP);
for(i = 0; i < a->nVertices; i++){
//printf("drawing asteroid vertex x: %.3f y: %.3f\n", a->coords[i].x, a->coords[i].y);
glVertex2f(a->coords[i].x, a->coords[i].y);
}
glEnd();
}
/* ========================================== Initialization Functions ===================================== */
void
init()
{
/*
* set parameters including the numbers of asteroids and photons present,
* the maximum velocity of the ship, the velocity of the laser shots, the
* ship's coordinates and velocity, etc.
*/
/** initialize ship **/
ship.x = 66.66;
ship.y = 50.0;
ship.dx = 0.0;
ship.dy = 0.0;
ship.phi = 90.0;
ship.radius = 2.5;
ship.acceleration = 0.00002;
ship.turnSpeed = 3.4;
ship.status = 1;
}
void
initAsteroid(Asteroid *a, double x, double y, double size) {
/*
* generate an asteroid at the given position; velocity, rotational
* velocity, and shape are generated randomly; size serves as a scale
* parameter that allows generating asteroids of different sizes;
*/
double theta, r;
int i;
a->x = x;
a->y = y;
a->phi = 0.0;
a->dx = myRandom(-0.02, 0.02);
a->dy = myRandom(-0.02, 0.02);
a->dphi = myRandom(-0.1, 0.1);
a->nVertices = 6+rand()%(MAX_VERTICES-6);
if(circularAsteroids){
r = size * 2.5;
a->radius = r;
for (i=0; i<a->nVertices; i++) {
theta = 2.0*M_PI*i/a->nVertices;
a->coords[i].x = r*sin(theta);
a->coords[i].y = r*cos(theta);
}
} else {
a->radius = size * 2.0;
for (i=0; i<a->nVertices; i++) {
theta = 2.0*M_PI*i/a->nVertices;
r = size*myRandom(2.0, 3.0);
a->coords[i].x = r*sin(theta);
a->coords[i].y = r*cos(theta);
}
}
a->active = 1;
}
void
initPhoton(){
int i;
for(i = 0; i < MAX_PHOTONS; i++){
Photon p = photons[i];
if (p.active == 0){
p.active = 1;
p.phi = ship.phi;
p.x1 = ship.x + (ship.radius * cos(p.phi * DEG2RAD));
p.y1 = ship.y + (ship.radius * sin(p.phi * DEG2RAD));
p.x2 = p.x1 + (PHOTON_LENGTH * cos(p.phi * DEG2RAD));
p.y2 = p.y1 + (PHOTON_LENGTH * sin(p.phi * DEG2RAD));
p.dx = -PHOTON_VELOCITY * sin((p.phi - 90.0) * DEG2RAD);
p.dy = PHOTON_VELOCITY * cos((p.phi - 90.0) * DEG2RAD);
photons[i] = p;
//printf("firing a photon:\nv1: (%.2f, %.2f) v2: (%.2f, %.2f) dx: %.2f dy: %.2f phi: %.2f\n\n", p.x1, p.y1, p.x2, p.y2, p.dx, p.dy, p.phi);
break;
}
}
}
/* ============================================== Helper Functions ========================================= */
double
myRandom(double min, double max)
{
/* return a random number uniformly draw from [min,max] */
return min+(max-min)*(rand()%0x7fff)/32767.0;
}
/**
* clamps value between min and max
*/
double
clamp (double value, double min, double max){
value = value <= max ? value : max;
value = value >= min ? value : min;
return value;
}
/**
* checks to see if x and y are within the bounds of the screen
*/
int
isInBounds(double x, double y){
if(x < 0 || x > xMax || y < 0 || y > yMax){
return 0;
}
return 1;
}
void
raycastForNewCoordinates(double x, double y, double dx, double dy, double * newCoords){
// assume that x and y are outside of the bounds of the screen. They must be brought back into the screen
double reEnterX = clamp(x, 0.0, xMax);
double reEnterY = clamp(y, 0.0, yMax);
// perform a raycast using the opposite of the object's current velocity. When the ray goes out of the screen,
// this gives an approximation of where the object should re-enter
while (isInBounds(reEnterX, reEnterY)){
reEnterX -= dx*2;
reEnterY -= dy*2;
}
// clamp the re-entry values to be within the screen's bounds
newCoords[0] = clamp(reEnterX, 0.0, xMax);
newCoords[1] = clamp(reEnterY, 0.0, yMax);
}
void
checkPhotonAsteroidCollision(){
int photonIndex, asteroidIndex;
for(photonIndex = 0; photonIndex < MAX_PHOTONS; photonIndex ++){
if (photons[photonIndex].active){
// for each active photon...
for (asteroidIndex = 0; asteroidIndex < MAX_ASTEROIDS; asteroidIndex ++){
if (asteroids[asteroidIndex].active){
// ... and for each active asteroid, check if there was a collision
Photon p = photons[photonIndex];
Asteroid a = asteroids[asteroidIndex];
double xPow1 = pow((p.x2 - a.x), 2);
double yPow1 = pow((p.y2 - a.y), 2);
double xPow2 = pow((p.x1 - a.x), 2);
double yPow2 = pow((p.y1 - a.y), 2);
double r2 = pow(a.radius, 2);
if(xPow1 + yPow1 <= r2 || xPow2 + yPow2 <= r2){
// there was a collision between this photon and this asteroid
photons[photonIndex].active = 0;
asteroids[asteroidIndex].active = 0;
if(a.radius > 2.2){
// if the asteroid is big enough, create some child asteroids
int i, maxChildAsteroids = 3, childAsteroidCount = 0;
for(i = 0; i < MAX_ASTEROIDS; i++){
if (!asteroids[i].active && childAsteroidCount < maxChildAsteroids){
childAsteroidCount++;
initAsteroid(&asteroids[i], a.x, a.y, a.radius/4.0);
}
}
}
break;
}
}
}
}
}
}
void
checkShipAsteroidCollision(){
int asteroidIndex, shipVertexIndex;
for(asteroidIndex = 0; asteroidIndex < MAX_ASTEROIDS; asteroidIndex++){
if(asteroids[asteroidIndex].active && ship.status == 1){
for(shipVertexIndex = 0; shipVertexIndex < 3; shipVertexIndex++){
Asteroid a = asteroids[asteroidIndex];
double xPow = pow((ship.coords[shipVertexIndex].x - a.x), 2);
double yPow = pow((ship.coords[shipVertexIndex].y - a.y), 2);
double r2 = pow(a.radius, 2);
if(xPow + yPow <= r2){
// there was a collision between the ship and this asteroid
destroyAndResetShip();
break;
}
}
}
}
}
void
resetAsteroidShape(){
// change value from 0 -> 1 or from 1 -> 0 and clear asteroids
circularAsteroids = abs(circularAsteroids - 1);
int i;
for (i = 0; i < MAX_ASTEROIDS; i ++){
asteroids[i].active = 0;
}
}