/
main.cpp
820 lines (722 loc) · 24.3 KB
/
main.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
#include "easyglut.h"
#include "bodies.h"
#include <sstream>
#include <iostream>
#include <time.h>
#include "agent.h"
#ifndef DEGTORAD
#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876f
#endif
#include "Box2d/Box2D.h"
#include <string.h>
#include "Render.h"
#include "fstream"
char * readFile (char * fileName);
b2World myWorld (b2Vec2 (0, 0) );
DebugDraw draw;
b2Vec2 destination = b2Vec2 (0, 0);
float rayLength = 65; //long enough to hit the walls
Agent * agent;
enum
{
TIRE = 100,
LEFT_SIDE = 0,
RIGHT_SIDE = 1
};
// LKP = Left arrow Key Press
// RKP = Right arrow Key Press
// UKP = Up arrow Key Press
// DKP = Down arrow Key Press
// LMB = Left Mouse Button
// RMB = Right Mouse Button
// MMB = Middle Mouse Button
bool LKP, RKP, UKP, DKP, LMB, RMB, MMB;
bool fullscreen;
char map[30][50];
char fps[10];
char carCollisionMap[41][41] = {0};
bool emptyRayCast[3];
void NormalKeyInput (unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit (0);
case 'p':
break;
case 61:
if (rayLength < 100)
{ rayLength = rayLength + 1; }
break;
case '-':
if (rayLength > 1)
{ rayLength = rayLength - 1; }
break;
default:
break;
}
}
void ReleaseNormalKeyInput (unsigned char key, int x, int y)
{
printf (" input %d\n", key );
switch (key)
{
case 'w':
break;
case 'f':
break;
case 'a':
break;
case 'r':
for ( b2Body * b = myWorld.GetBodyList (); b; b = b->GetNext () )
{
myWorld.DestroyBody (b);
}
delete agent;
agent = new Agent (myWorld);
readFile ("map.txt");
break;
case '1':
break;
default:
break;
}
}
void SpecialKeyInput (int key, int x, int y)
{
switch (key)
{
case GLUT_KEY_LEFT:
LKP = 1;
break;
case GLUT_KEY_RIGHT:
RKP = 1;
break;
case GLUT_KEY_UP:
UKP = 1;
break;
case GLUT_KEY_DOWN:
DKP = 1;
break;
default:
break;
}
}
void ReleaseSpecialKeyInput (int key, int x, int y)
{
switch (key)
{
// pressing F1 key makes the prpgram fullscreen, press F1 again to get back in windowed mode
case GLUT_KEY_F1:
fullscreen = !fullscreen;
if (fullscreen)
{
glutFullScreen ();
}
else
{
glutLeaveFullScreen (glutGet (GLUT_INIT_WINDOW_X),
glutGet (GLUT_INIT_WINDOW_Y),
glutGet (GLUT_INIT_WINDOW_WIDTH),
glutGet (GLUT_INIT_WINDOW_HEIGHT) );
}
break;
case GLUT_KEY_F2:
break;
case GLUT_KEY_F3:
break;
case GLUT_KEY_RIGHT:
RKP = false;
break;
case GLUT_KEY_LEFT:
LKP = false;
break;
case GLUT_KEY_UP:
UKP = false;
break;
case GLUT_KEY_DOWN:
DKP = false;
break;
default:
break;
}
}
void ProcessMouse (int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
printf ("%d %d\n", x, y);
if (button == GLUT_LEFT_BUTTON)
{
//hides the cursor
if (glutGetModifiers () == GLUT_ACTIVE_CTRL)
{
createBody (myWorld, b2_staticBody, (x / 16) * 16 , ( (600 - y) / 10) * 10 , 10, 10, false);
}
if (glutGetModifiers () == GLUT_ACTIVE_ALT)
{
destination = b2Vec2 (x, 600 - y);
}
// glutSetCursor (GLUT_CURSOR_NONE);
LMB = true;
}
else if (button == GLUT_MIDDLE_BUTTON)
{
MMB = true;
}
else if (button == GLUT_RIGHT_BUTTON)
{
RMB = true;
}
}
else if ( state == GLUT_UP )
{
if ( button == GLUT_LEFT_BUTTON )
{
LMB = false;
}
else if ( button == GLUT_MIDDLE_BUTTON )
{
MMB = false;
if (glutGetModifiers () == GLUT_ACTIVE_CTRL)
{
b2Body * temp;
temp = createBody (myWorld, b2_dynamicBody, (x / 16) * 16 , ( (600 - y) / 10) * 10 , 10, 10, true);
temp->ApplyLinearImpulse (b2Vec2 (-50000000, 0), b2Vec2 () );
}
}
else if ( button == GLUT_RIGHT_BUTTON )
{
if (glutGetModifiers () == GLUT_ACTIVE_CTRL)
{
b2Body * temp;
temp = createBody (myWorld, b2_dynamicBody, (x / 16) * 16 , ( (600 - y) / 10) * 10 , 10, 10, true);
temp->ApplyLinearImpulse (b2Vec2 (0, 50000000), b2Vec2 () );
}
RMB = false;
}
}
}
void CursorActiveMotion (int x, int y)
{
// This makes the cursor (x,y) relative to the lower left corner of the glut
// window as in opengl
y = glutGet (GLUT_INIT_WINDOW_HEIGHT) - y;
}
void CursorPassiveMotion (int x, int y)
{
y = glutGet (GLUT_INIT_WINDOW_HEIGHT) - y;
}
void Update (int timer)
{
float32 timeStep = 1 /
60.0f; //the length of time passed to simulate (seconds)
int32 velocityIterations = 6; //how strongly to correct velocity
int32 positionIterations = 3; //how strongly to correct position
myWorld.Step ( timeStep, velocityIterations, positionIterations);
for ( b2Body * b = myWorld.GetBodyList (); b; b = b->GetNext () )
{
//do something with the body 'b'
if (b->GetUserData () == 0)
{ break; }
}
glutPostRedisplay ();
CalculateFps ();
glutTimerFunc (1000.0f / 60.0f , Update, 1);
}
void Clear (int count)
{
memset (carCollisionMap, 0, sizeof (carCollisionMap) );
agent->nearestAngle[0] = 1000.0f;
agent->nearestAngle[1] = 1000.0f;
agent->reverseMove = false;
if (agent->frontObstacle == true)
if (agent->body->GetLinearVelocity ().x < 0.3 && agent->body->GetLinearVelocity ().y < 0.3)
{
agent->reverseMove = true;
}
agent->frontObstacle = false ;
agent->foundNearestAngle = true;
glutTimerFunc (1000 , Clear, 1);
}
// Load your images here
void LoadImages ()
{
myWorld.SetDebugDraw (&draw);
draw.SetFlags ( DebugDraw::e_shapeBit | DebugDraw::e_aabbBit |
DebugDraw::e_jointBit | DebugDraw::e_centerOfMassBit | DebugDraw::e_pairBit);
glutTimerFunc (1000 / 100.0, Update, 1);
glutTimerFunc (2000 / 1000, Clear, 1);
}
// Your rendering functions all kinds of rendering functions needed to be called here
// custom opengl rendering functions should be called here
float converToDegree (float radian)
{
return (int) (fabs (radian + 1.57) / DEGTORAD) % 360;
}
void turnToDestination ()
{
// if (agent->frontObstacle == 0)
if (destination.x != 0)
{
if (agent->foundNearestAngle)
{
static float temp = glutGet (GLUT_ELAPSED_TIME);
if (glutGet (GLUT_ELAPSED_TIME) - temp > 1000)
{
float deltaX = destination.x - agent->body->GetPosition ().x;
float deltaY = destination.y - agent->body->GetPosition ().y;
float tempAngle = atan2 (deltaX, deltaY);
agent->angle = tempAngle - 1.57;
temp = glutGet (GLUT_ELAPSED_TIME);
agent->foundNearestAngle = false;
}
}
}
}
void turnToNearestAngle ()
{
// if there is any obstalce in front of the vehicle then, look for the nearest safe angle
// and change vehicle angle to that angle
if (agent->frontObstacle)
{
static float timerF = glutGet (GLUT_ELAPSED_TIME) ;
if (glutGet (GLUT_ELAPSED_TIME) - timerF > 500)
if (agent->nearestAngle[0] != 1000 || agent->nearestAngle[1] != 1000)
{
if (agent->nearestAngle[LEFT_SIDE] < agent->nearestAngle[RIGHT_SIDE])
{
printf ("Left side wins %f\n", agent->nearestAngle[LEFT_SIDE]);
agent->angle = agent->angle - .5;
}
else
{
printf ("Right side wins %f\n", agent->nearestAngle[RIGHT_SIDE]);
agent->angle = agent->angle + .5;
}
timerF = glutGet (GLUT_ELAPSED_TIME);
}
}
}
void Render ()
{
LoadFPS (60);
glClearColor (250/255.0, 245/255.0, 245/255.0, 1);
srand (time (NULL) );
//at global scope
static float currentRayAngle = 0;
currentRayAngle += 6;
//calculate points of ray
b2Vec2 p1 ( b2Vec2 (agent->body->GetPosition ().x,
agent->body->GetPosition ().y) ); //center of scene
b2Vec2 p2 = p1 + rayLength * b2Vec2 (sinf (DEGTORAD * (currentRayAngle ) ),
cosf ( DEGTORAD * (currentRayAngle ) ) );
b2Vec2 p3 ( b2Vec2 (agent->body->GetPosition ().x,
agent->body->GetPosition ().y) );
b2Vec2 p4 = p3 + rayLength * b2Vec2 (sinf (DEGTORAD * (currentRayAngle + 90) ),
cosf ( DEGTORAD * (currentRayAngle + 90) ) );
b2Vec2 p5 ( b2Vec2 (agent->body->GetPosition ().x,
agent->body->GetPosition ().y) ); //center of scene
b2Vec2 p6 = p5 + rayLength * b2Vec2 (sinf (DEGTORAD * (currentRayAngle + 180) ),
cosf ( DEGTORAD * (currentRayAngle + 180) ) );
b2Vec2 p7 ( b2Vec2 (agent->body->GetPosition ().x,
agent->body->GetPosition ().y) );
b2Vec2 p8 = p7 + rayLength * b2Vec2 (sinf (DEGTORAD * (currentRayAngle + 270 ) ),
cosf ( DEGTORAD * (currentRayAngle + 270 ) ) );
b2Vec2 p9 = b2Vec2 (agent->body->GetPosition ().x ,
agent->body->GetPosition ().y - 4 ) ;
b2Vec2 p10 = b2Vec2 (agent->body->GetPosition ().x ,
agent->body->GetPosition ().y - 4 ) + rayLength / 2.0 * b2Vec2 (sinf ( (
agent->angle + 1.57) ),
cosf ( ( agent->angle + 1.57 ) ) );
b2Vec2 p11 = b2Vec2 (agent->body->GetPosition ().x ,
agent->body->GetPosition ().y + 4 ) ;
b2Vec2 p12 = b2Vec2 (agent->body->GetPosition ().x,
agent->body->GetPosition ().y + 4 ) + rayLength / 2.0 * b2Vec2 (sinf ( (
agent->angle + 1.57) ),
cosf ( ( agent->angle + 1.57 ) ) );
b2RayCastInput input;
input.p1 = p1;
input.p2 = p2;
input.maxFraction = rayLength / 50.0;
b2RayCastInput input2;
input2.p1 = p3;
input2.p2 = p4;
input2.maxFraction = rayLength / 50.0;
b2RayCastInput input3;
input3.p1 = p5;
input3.p2 = p6;
input3.maxFraction = rayLength / 50.0 ;
b2RayCastInput input4;
input4.p1 = p7;
input4.p2 = p8;
input4.maxFraction = rayLength / 50.0;
b2RayCastInput input5;
input5.p1 = p9;
input5.p2 = p10;
input5.maxFraction = rayLength / 50.0;
b2RayCastInput input6;
input6.p1 = p11;
input6.p2 = p12;
input6.maxFraction = rayLength / 50.0;
//check every fixture of every body to find closest
float closestFraction = rayLength / 50.0; //start with end of line as p2
float closestFraction2 = rayLength / 50.0 ;
float closestFraction3 = rayLength / 50.0;
float closestFraction4 = rayLength / 50.0;
float closestFraction5 = rayLength / 50.0;
float closestFraction6 = rayLength / 50.0;
static int timer = 0;
timer++;
b2Vec2 intersectionNormal (0, 0);
bool isObstacle[5] = {0} ;
static int freeAngle[5] = {0};
freeAngle[1] = agent->angle;
static int count = 0 ;
/*
* Raycasting is done here, there are 6 ray here
*/
for (b2Body * b = myWorld.GetBodyList (); b; b = b->GetNext () )
{
if (100 == (int) b->GetUserData () )
{ break; }
for (b2Fixture * f = b->GetFixtureList (); f; f = f->GetNext () )
{
b2RayCastOutput output;
if ( f->RayCast ( &output, input, 0 ) )
{
if ( output.fraction < closestFraction )
{
isObstacle[1] = 1;
count = 0 ;
closestFraction = output.fraction;
intersectionNormal = output.normal;
b2Vec2 intersectionPoint = p1 + closestFraction * (p2 - p1);
b2Vec2 diff1 = agent->body->GetPosition () - intersectionPoint;
int x = diff1.x / (800 / 80.0) ;
int y = diff1.y / (600 / 60.0) ;
carCollisionMap[20 - y][20 - x] = 1;
}
}
b2RayCastOutput output2;
if (f->RayCast (&output2, input2, 0) )
{
if (output2.fraction < closestFraction)
{
isObstacle[2] = 1;
closestFraction2 = output2.fraction;
b2Vec2 intersectionPoint2 = p3 + closestFraction2 * (p4 - p3);
b2Vec2 diff2 = agent->body->GetPosition () - intersectionPoint2;
int x = diff2.x / (800 / 80.0) ;
int y = diff2.y / (600 / 60.0) ;
carCollisionMap[20 - y][20 - x] = 1;
}
}
b2RayCastOutput output3;
if (f->RayCast (&output3, input3, 0) )
{
if (output3.fraction < closestFraction3)
{
isObstacle[3] = 1;
closestFraction3 = output3.fraction;
b2Vec2 intersectionPoint3 = p5 + closestFraction3 * (p6 - p5);
b2Vec2 diff2 = agent->body->GetPosition () - intersectionPoint3;
int x = diff2.x / (800 / 80.0) ;
int y = diff2.y / (600 / 60.0) ;
carCollisionMap[20 - y][20 - x] = 1;
}
}
b2RayCastOutput output4;
if (f->RayCast (&output4, input4, 0) )
{
if (output4.fraction < closestFraction4)
{
isObstacle[4] = 1;
closestFraction4 = output4.fraction;
b2Vec2 intersectionPoint4 = p7 + closestFraction4 * (p8 - p7);
b2Vec2 diff2 = agent->body->GetPosition () - intersectionPoint4;
int x = diff2.x / (800 / 80.0) ;
int y = diff2.y / (600 / 60.0) ;
carCollisionMap[20 - y][20 - x] = 1;
}
}
b2RayCastOutput output5;
if (f->RayCast (&output5, input5, 0) )
{
if (output5.fraction < closestFraction5)
{
closestFraction5 = output5.fraction;
b2Vec2 intersectionPoint5 = p9 + closestFraction5 * (p10 - p9);
b2Vec2 diff2 = agent->body->GetPosition () - intersectionPoint5;
int x = diff2.x / (800 / 80.0) ;
int y = diff2.y / (600 / 60.0) ;
carCollisionMap[20 - y][20 - x] = 1;
agent->frontObstacle = 1;
}
}
b2RayCastOutput output6;
if (f->RayCast (&output6, input6, 0) )
{
if (output6.fraction < closestFraction6)
{
closestFraction6 = output6.fraction;
b2Vec2 intersectionPoint6 = p11 + closestFraction6 * (p12 - p11);
b2Vec2 diff2 = agent->body->GetPosition () - intersectionPoint6;
int x = diff2.x / (800 / 80.0) ;
int y = diff2.y / (600 / 60.0) ;
carCollisionMap[20 - y][20 - x] = 1;
agent->frontObstacle = 1;
}
}
}
}
static float timetem= glutGet(GLUT_ELAPSED_TIME);
if(glutGet(GLUT_ELAPSED_TIME)-timetem > 1000)
for (int i = 1 ; i <= 4; ++i)
{
if (isObstacle[i] == false)
{
float tempAngle;
tempAngle = (int) (currentRayAngle + 90 * (i - 1) ) % 360;
if (tempAngle >= converToDegree (agent->angle) && tempAngle <= (int) (converToDegree (agent->angle) + 180) % 360)
{
if (agent->nearestAngle[RIGHT_SIDE] > tempAngle - converToDegree (agent->angle) )
{
agent->nearestAngle[RIGHT_SIDE] = tempAngle - converToDegree (agent->angle) ;
}
else
{ isObstacle[i] = true; }
}
else
{
if (agent->nearestAngle[LEFT_SIDE] > (360 - tempAngle) - converToDegree (agent->angle) )
{ agent->nearestAngle[LEFT_SIDE] = fabs ( (360 - tempAngle) - converToDegree (agent->angle) ); }
else
{ isObstacle[i] = true; }
}
}
timetem= glutGet(GLUT_ELAPSED_TIME);
}
// if no obstacle is infront of behicle and destination is set, then head for the destination
// this is done by changing vehicle angle to face the destination
b2Vec2 intersectionPoint = p1 + closestFraction * (p2 - p1);
b2Vec2 intersectionPoint2 = p3 + closestFraction2 * (p4 - p3);
b2Vec2 intersectionPoint3 = p5 + closestFraction3 * (p6 - p5);
b2Vec2 intersectionPoint4 = p7 + closestFraction4 * (p8 - p7);
b2Vec2 intersectionPoint5 = p9 + closestFraction4 * (p10 - p9);
b2Vec2 intersectionPoint6 = p11 + closestFraction4 * (p12 - p11);
float dist = (agent->body->GetPosition().x - intersectionPoint5.x) * (agent->body->GetPosition().x - intersectionPoint5.x)
+ (agent->body->GetPosition().y - intersectionPoint5.y) * (agent->body->GetPosition().x - intersectionPoint5.y);
//draw a line
glColor3f (255, 0, 0); //white
glBegin (GL_LINES);
glVertex2f ( p1.x, p1.y );
glVertex2f ( intersectionPoint.x, intersectionPoint.y );
glEnd ();
SetRenderColor (0, 0, 0, 255);
if (isObstacle[1] == false)
{ SetRenderColor (255, 0, 255, 255); }
//draw a point at the intersection point
glPointSize (10);
glBegin (GL_POINTS);
glVertex2f ( intersectionPoint.x, intersectionPoint.y );
glEnd ();
SetRenderColor (255, 34, 25, 255);
glBegin (GL_POINTS);
glVertex2f ( destination.x, destination.y );
glEnd ();
glBegin (GL_LINES);
glVertex2f ( p3.x, p3.y );
glVertex2f ( intersectionPoint2.x, intersectionPoint2.y );
glEnd ();
glBegin (GL_LINES);
glVertex2f ( p5.x, p5.y );
glVertex2f ( intersectionPoint3.x, intersectionPoint3.y );
glEnd ();
glBegin (GL_LINES);
glVertex2f ( p7.x, p7.y );
glVertex2f ( intersectionPoint4.x, intersectionPoint4.y );
glEnd ();
glBegin (GL_LINES);
glVertex2f ( p9.x, p9.y );
glVertex2f ( intersectionPoint5.x, intersectionPoint5.y );
glEnd ();
glBegin (GL_POINTS);
glVertex2f ( intersectionPoint5.x, intersectionPoint5.y );
glEnd ();
glBegin (GL_LINES);
glVertex2f ( p11.x, p11.y );
glVertex2f ( intersectionPoint6.x, intersectionPoint6.y );
glEnd ();
glBegin (GL_POINTS);
glVertex2f ( intersectionPoint6.x, intersectionPoint6.y );
glEnd ();
SetRenderColor (0, 0, 0, 255);
if (isObstacle[2] == false)
{ SetRenderColor (255, 0, 255, 255); }
glPointSize (10);
glBegin (GL_POINTS);
glVertex2f ( intersectionPoint2.x, intersectionPoint2.y );
glEnd ();
SetRenderColor (0, 0, 0, 255);
if (isObstacle[3] == false)
{ SetRenderColor (255, 0, 255, 255); }
glBegin (GL_POINTS);
glVertex2f ( intersectionPoint3.x, intersectionPoint3.y );
glEnd ();
SetRenderColor (0, 0, 0, 255);
if (isObstacle[4] == false)
{ SetRenderColor (255, 0, 255, 255); }
glBegin (GL_POINTS);
glVertex2f ( intersectionPoint4.x, intersectionPoint4.y );
glEnd ();
// glBegin (GL_LINES);
// glVertex2f ( agent->body->GetPosition ().x, agent->body->GetPosition ().y );
// glVertex2f ( agent->body->GetPosition ().x + sin (agent->angle + 1.57) * 100,
// agent->body->GetPosition ().y + cos (agent->angle + 1.57) * 100);
// glEnd ();
static float posx = 0;
posx = posx + .1;
// we are drawing collision array here
for (int i = 0; i < 40; ++i)
for (int j = 0; j < 40; ++j)
{
if (carCollisionMap[i][j] == 1)
{
glPointSize (5);
glBegin (GL_POINTS);
glVertex2f (agent->body->GetPosition ().x - (20 - j) * 10 ,
agent->body->GetPosition ().y - (20 - i) * 10 );
glEnd ();
}
}
turnToDestination();
turnToNearestAngle ();
agent->isCollision = 0;
// checking collision here, not so important now
for (int i = 1; i < 15; ++i)
{
float x = 20 + (i * 2) * sinf (agent->angle + 1.57) ;
float y = 20 + (i * 2) * cosf (agent->angle + 1.57);
if (x < 40 && y < 40)
if (carCollisionMap[ (int) y][ (int) x] == 1)
{
// printf ("obstacle found\n");
agent->isCollision = 1;
static float timerT = glutGet (GLUT_ELAPSED_TIME);
bool safe = 1;
safe = rand () % 2;
if (!agent->frontObstacle)
if (agent->nearestAngle[LEFT_SIDE] != 1000 || agent->nearestAngle[RIGHT_SIDE] != 1000)
{
// if (glutGet (GLUT_ELAPSED_TIME) - timerT > 500)
{
if (agent->nearestAngle[LEFT_SIDE] < agent->nearestAngle[RIGHT_SIDE])
{
printf ("Left side wins %f\n", agent->nearestAngle[LEFT_SIDE]);
agent->angle = agent->angle - .2;
agent->foundNearestAngle = true;
// agent->nearestAngle[RIGHT_SIDE] = 1000;
}
else
{
printf ("Right side wins %f\n", agent->nearestAngle[RIGHT_SIDE]);
agent->foundNearestAngle = true;
// agent->nearestAngle[LEFT_SIDE] = 1000;
agent->angle = agent->angle + .2;
}
timerT = glutGet (GLUT_ELAPSED_TIME);
}
}
// if (glutGet (GLUT_ELAPSED_TIME) - timerT > 1000)
// {
//
// if (safe == 1)
// for (int j = 0; j < 10; ++j)
// {
// float x = 20 + ( (j ) * sinf (agent->angle + 1.57 + .3) ) ;
// float y = 20 + ( (j ) * cosf (agent->angle + 1.57 + .3) ) ;
//
// if (carCollisionMap[ (int) y][ (int) x] == 1)
// {
// safe = 0;
// break;
// }
// }
// if (safe == 1)
// { agent->angle = agent->angle + .3; }
// {
// memset (carCollisionMap, 0, sizeof (carCollisionMap) );
// }
// if (safe == 0)
// {
// safe = 1;
// for (int j = 0; j < 10; ++j)
// {
// float x = 20 + ( (j ) * sinf (agent->angle + 1.57 - .3) ) ;
// float y = 20 + ( (j ) * cosf (agent->angle + 1.57 - .3) ) ;
// if (carCollisionMap[ (int) y][ (int) x] == 1)
// {
// safe = 0;
// break;
// }
// }
// if (safe == 1)
// {
// agent->angle = agent->angle - .3;
// memset (carCollisionMap, 0, sizeof (carCollisionMap) );
// }
// }
// timerT = glutGet (GLUT_ELAPSED_TIME);
// }
}
}
agent->moveForward ();
SetRenderColor (0, 0, 0, 255);
RenderBitmapText (60, 550, GLUT_BITMAP_9_BY_15, "ctrl + left click to add obstacle, alt + left click to add a destination");
sprintf (fps, "%f", GetFps () );
SetRenderColor (255, 0, 255, 255);
RenderBitmapText (10, 590, GLUT_BITMAP_9_BY_15, fps);
myWorld.DrawDebugData ();
}
char * readFile (char * fileName)
{
std::string sLine = "";
std::ifstream infile;
int row = 0;
infile.open (fileName);
if (!infile)
{
std::cout << std::endl << "Failed to open file";
return NULL;
}
while (!infile.eof () )
{
getline (infile, sLine);
map[row][0] = sLine.length ();
for (int i = 0; i < map[row][0]; i++)
{ map[row][i + 1] = sLine[i]; }
++row;
std::cout << sLine << std::endl;
}
for (int j = 0; j < 30; j++)
for (int i = 1 ; i <= map[j][0]; ++i)
if (map[j][i] == '1')
{
createBody (myWorld, b2_staticBody, i * 800 / 40, (30 - j) * 600 / 30, 10, 10, false);
}
else if (map[j][i] == '2')
{
agent->body->SetTransform (b2Vec2 (i * 800 / 40, (30 - j) * 600 / 30), 0);
}
infile.close ();
return NULL;
}
int main ()
{
agent = new Agent (myWorld);
readFile ("map.txt");
SetGlutWindow (100, 100, 800, 600, "Sample");
return 0 ;
}