-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
executable file
·944 lines (822 loc) · 21.9 KB
/
player.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
#include <stdio.h>
#include <math.h>
#include "fssimplewindow.h"
#include "ysglfontdata.h"
#include "player.h"
#include "button.h"
#include "boundary.h"
#include "mathfunc.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include "glm.h"
#include <windows.h>
#include "stdafx.h"
// Player class.
// Keeps track of the position, velocity, acceleration, angle, and angular velocity.
// Also limits the maximum velocities that it can go
// Contains a button object, and takes in boundaries
// Many functions that check for interactions between fish and hooks
const int speedLength=100;//Arbitrary length used to scale the player speeds
const double times=0.015;// Times is the timestep in seconds. (leave it at 0.01 plz)
const double pie=3.14159;// pi
double Player::getT()
{
return t;
}
Player::Player()
{
x0 = 0;
z0 = 0;
t=270.010;
pt = -1;
dragc = 1;
goal = 2;
bounceID=-1;
switchtime = 500;
y = .5;
caught = 0;
Reset();
}
Player::~Player()
{
}
void Player::Erase()
{
Reset();
pt=-1;//Change playertype to unknown type
}
void Player::Reset()
{
// Reset to initial positions, angles, velocites etc.
// (initial starting position x0 is not changed here)
//printf("Stuffs reset\r");
z=z0;
x=x0;
y = .5;
goal = 2;//First goal is the third node
t=270.010; //0.01 necesary to keep trig from breaking
//printf("Reset %lf\n", t);
dt=0;
Vp=0;
Ap=0;
usedtobeinabound=0;
inabound=0;
boundid='q';
finish=0;
caught = 0;
camerano = 1;
}
void Player::SetPlayerType(int ptt)
{
double leaderspeed=speedLength*times/4;// Leaders take 4 seconds to move 800 pixels
// Save player type
pt=ptt;
dragc=1; // Drag coeff. (1=critical damping, >1: Over, <1: under
goal = 2;
switch( pt)
{
case 0://User
Vmax=speedLength*times/.8; // Takes 1 second to pass bounds at max speed
Vmin=-speedLength*times/2; // Takes 2 seconds to cross bounds going backwards
Apmax=Vmax*times/1; // Take 1s to reach max speed
Apmin=-Vmax*times/0.5; // 0.5 s to stop
dtmax=360*times/2.0; //1.2s to complete a spin
ddtmax=dtmax*times/0.1; //.1s to accelerate to max velocity
break;
case 1:
Vmax=speedLength*times/1; // Takes 1 second to pass bounds at max speed
Vmin=-speedLength*times/2; // Takes 2 seconds to cross bounds going backwards
Apmax=Vmax*times/1; // Take 1s to reach max speed
Apmin=-Vmax*times/0.5; // 0.5 s to stop
dtmax=360*times/2.0; //1.2s to complete a spin
ddtmax=dtmax*times/0.1; //.1s to accelerate to max velocity
break;
/*case 1://Racer
Vmax=speedLength*times/1; //Exactly the same as a User
Vmin=-speedLength*times/2;
Apmax=Vmax*times/1;
Apmin=-Vmax*times/0.5;
dtmax=360*times/1.2;
ddtmax=dtmax*times/0.1;
break;*/
case 2://Leader
Vmax=leaderspeed; // Initiated above
Vmin=-speedLength*times/1;
Apmax=Vmax*times/1;
Apmin=-Vmax*times/0.5;
dtmax=360*times/0.25;
ddtmax=dtmax*times/0.25;
break;
case 3://Follower
Vmax=leaderspeed*1.5; // Seems to work pretty well
Vmin=-speedLength*times/1;
Apmax=Vmax*times/1;
Apmin=-Vmax*times/0.5;
dtmax=360*times/1;
ddtmax=dtmax*times/.1;
break;
case 4://Hunter
Vmax=speedLength*times/2;
Vmin=-speedLength*times/1;
Apmax=Vmax*times/0.4;
Apmin=-Vmax*times/0.5;
dtmax=360*times/0.75;
ddtmax=dtmax*times/.1;
break;
case 5://Prey
Vmax=speedLength*times/2;
Vmin=-speedLength*times/1;
Apmax=Vmax*times/0.75;
Apmin=-Vmax*times/0.5;
dtmax=360*times/0.5;
ddtmax=dtmax*times/.1;
break;
case 6://Spaz
Vmax=speedLength*times/2;
Vmin=-speedLength*times/1;
Apmax=Vmax*times/1;
Apmin=-Vmax*times/0.5;
dtmax=360*times/1;
ddtmax=dtmax*times/.1;
break;
default://Shouldnt happen, if it does, print error
Vmax=0;
Vmin=0;
Apmax=0;
Apmin=0;
dtmax=0;
printf("Err: Unknown player type\n");
break;
}
}
void Player::Place(const Boundary &Path, const Boundary &BL, const Boundary &BR)
{
//(z>Path.z[Path.length-3])&&(pt==0||pt==1)
if(pt==1||pt==0)
{
do{
//Place the player, and see if it is between the two boundaries
x0=Path.x[3]*(double)(rand()%100)/100; // create initial x coord
z0=Path.z[3]*(double)(rand()%100)/100; // create initial z coordinate
}while(0==BetweenBounds(BL,BR,(int)x0,(int)z0)||!(z0<Path.z[2])||!(z0>Path.z[1]));
}
else
{
do{
//Place the player, and see if it is between the two boundaries
x0=Path.x[Path.length-1]*(double)(rand()%100)/100; // create initial x coord
z0=Path.z[Path.length-1]*(double)(rand()%100)/100; // create initial z coordinate
}while(0==BetweenBounds(BL,BR,(int)x0,(int)z0)||!(z0<Path.z[Path.length-2])||!(z0>Path.z[3]));
}
x=x0;
z=z0;
}
void Player::Go(Player *P, int numplayers, const Boundary &Path, const Boundary &BLL,const Boundary &BL,const Boundary &BR,const Boundary &BRR, const Button &B, Hook h[],int numHooks)
{
if(B.reset)
{
Reset();
}
if(caught)
{
Hook myHookobj = *myHook;
y = myHookobj.getY();
caught = myHookobj.caughtOrNot();
if(!caught)
{
y = .5;
z += myHookobj.getRadius()+2;
}
}
else
{
//Does everything for the player
//Works for every type of player. no specifications need to be given, just a lot of objects get passed in
Input(B);//Keyboard input for all players
//Player-Player interaction
if(!finish)//The player hasn't finished
{
Race(Path);//Follow the path
AI(P,numplayers);//Act
}
else
Finished(); //Act finished
Bounce(P,numplayers); //Check for bounces between each player
//Player-Env. Interaction
DetectBoundary(BLL,BL); //Check left boundary
DetectBoundary(BR,BRR); //Check right boundary
DetectEndCond(Path); //Check end line&beginning
if(usedtobeinabound==1||inabound==1)
MoveBound();
else//if not in a boundary
MoveDamp();
//Combine all
Move();
//Check to see that the Players have not exicted the outer boundary
Check(BLL, BL,Path,BR, BRR);
CheckFinished(Path);//Check to see if the player is passed the ifinish line
if(pt == 0)
{
CheckCollision(h, numHooks);
}
}
}
void Player::CheckCollision(Hook h[], int numHooks)
{
for(int i = 0; i < numHooks; i++)
{
Hook tempHook = h[i];
double dist = TrueDist(x,tempHook.getX(), z,tempHook.getZ());
//printf ("%lf truedist %lf radius\n", dist, tempHook.getRadius());
if(dist < tempHook.getRadius())
{
//printf("Caught\n");
caught = 1;
h[i].catchFish();
myHook = &(h[i]);
break;
}
}
}
void Player::Input(const Button &B)
{
// If Space Bar is pressed, Reset player
if(B.reset)
Reset();
if(pt==0)//If the player is controlled by a user
{
// Find Forward/Backward Accel
if(B.F&&!B.B)
{ // Hitting the gas
Ap=Apmax;
}
if(!B.F&&B.B)
{
if(Vp>=0) // If hitting brake
Ap=Apmin;
else // If Backing up
Ap=-Apmax;
}
if((!B.F&&!B.B)||(B.F&&B.B))
Ap=0;
// Find L/R turning
if(B.L&&!B.R)
ddt=-ddtmax;
if(!B.L&&B.R)
ddt=ddtmax;
if(!B.L&&!B.R)
ddt=0;
if(B.L&&B.R)
ddt=0;
}
}
void Player::Race(const Boundary &Path)
{
//Racer types follow the racepath
if(pt==1)//if a racer
{
double dist, alpha, dalpha, alpha2, dalpha2;
int goal2;
// Find the distance between the player and the next track node
dist=TrueDist(Path.x[goal],x,Path.z[goal],z);
// Find the difference between the current heading and the desired heading
alpha=TrueAng(Path.x[goal],x,Path.z[goal],z);
dalpha=FormAng(t-alpha,180);
//Find the next for added weight
goal2=goal+1;
if(goal2>=Path.length)
goal2=goal;
alpha2=TrueAng(Path.x[goal2],x,Path.z[goal2],z);
dalpha2=FormAng(t-alpha2,180);
//If large angle, turn
if(abs(dalpha)>5)
ddt=-(dalpha/abs(dalpha)-abs(dt)/dtmax)*ddtmax;
else //Prevent oscillations
ddt=0;
// A=kx+cX^. --> first term is difference, second is damping
Ap=Apmax;//*((50-abs(dalpha))/50*3/4+(50-abs(dalpha2))/50/4);
if(dist<50)//If close to the node
{
goal++; //Aim for the next node
if(goal>=Path.length)//If end is reached
{
goal=Path.length-1;
ddt=0;
Ap=0;
}
}
Ap=Ap*(double)(rand()%3+8)/10; //80% to 100% (adding variation)
ddt=ddt*(double)(rand()%4+7)/10; //70% to 100%
}
}
void Player::AI(Player *P, int numplayers)
{
int TID=ClosestPlayer(P,idno,numplayers);//Target ID number
int per=rand()%3+1; //randomly 1 2 or 3, used to step some things
double alpha, dalpha, dist;
switch(pt)//Different actions for different player types
{
case 2://Leader
Ap=Apmax*(double)(rand()%2+9)/10;//Accelerate 90-100%
if(per==1)// about once every three seconds
ddt=ddtmax*(double)(rand()%11-5)/5;//Turn maximum in a random direction
else
ddt=0;//No angular acceleration
break;
case 3://Follower
// Find the angle between the player and the nearest leader
alpha=TrueAng(P[TID].x,x,P[TID].z,z);
//Find the distance between the leader and the follower
dist = TrueDist(P[TID].x,x,P[TID].z,z);
// Find the difference between the current heading and the desired heading
dalpha=FormAng(t-alpha,180);
if(dist<40) //If close to the leader
ddt=-(dalpha/abs(dalpha))*ddtmax;//no angular damping
else
ddt=-(dalpha/abs(dalpha)-abs(dt)/dtmax)*ddtmax;//Add angular damping to prevent oscillatons
Ap=Apmax;//Maximize acceleration
break;
case 4://Hunter
//Find the angle between the player and the nearest prey
alpha=TrueAng(P[TID].x,x,P[TID].z,z);
// Find the distance between the player and the closest prey
dist = TrueDist(P[TID].x,x,P[TID].z,z);
// Find the difference between the current heading and the desired heading
dalpha=FormAng(t-alpha,180);
if(dist>100)//If far away, stop moving
{
ddt=0;
Ap=0;
}
else if(dist>50)//if between 50 and 100, chase the prey
{
ddt=-dalpha/abs(dalpha)*ddtmax;
Ap=Apmax;
}
else//if very close
{
dt=-dalpha/abs(dalpha)*ddtmax;
Ap=Apmax*(dist-25)/25;//Try to stay on top of it (not perfect)
}
break;
case 5://Prey
//Find the angle between the player and the nearest hunter
alpha=TrueAng(P[TID].x,x,P[TID].z,z);
// Find the distance between the player and the closest hunter
dist = TrueDist(P[TID].x,x,P[TID].z,z);
// Find the difference between the current heading and the desired heading
dalpha=FormAng(t-alpha+180,180);
if(dist>90)//Relaxing
{
Ap=Apmax*(double)(rand()%2+3)/10;
if(per==1)// about once every three seconds
ddt=ddtmax*(double)(rand()%11-5)/5;//Spin randomly
else
ddt=0;
}
else if (dist>30)//Running
{
Ap=Apmax*(double)(rand()%2+9)/10;
if(per>=1)// about once every three seconds
ddt=ddtmax*(double)(rand()%11-5)/5;//Evade
else
ddt=0;
}
else// Caught (option to change this to something else)
{
Ap=Apmax*(double)(rand()%2+9)/10;//Keep running
if(per==1)// about once every three seconds
ddt=ddtmax*(double)(rand()%11-5)/5;
else
ddt=0;
}
break;
case 6://Spaz
//Randomly move
Ap=(((double)(rand()%11))/20+0.5)*Apmax;
if(per>2)
{
ddt=((double)(rand()%11)/5-1)*ddtmax;
}
break;
default://Any non player based AI or Users
//printf("error#Derpaderp");
break;
}
}
void Player::Finished()
{
Ap=0;//Stop acceleration
ddt=0;//Stop turning
}
void Player::Bounce(Player *P, int numplayers)
{
//Bounces certain players apart
int racerdist=1;//each player has a radius of 5 at the moment
int leaderdist=3;//each player has a radius of 5 at the moment
int count , tempbouncecount=0;
double dt;
double temp1,temp2;
for(count=idno+1; count<numplayers;count++)
{
//For all following players
if(idno!=count)//Not the same player
{
if((pt==1||pt==0)&&(P[count].pt==1||P[count].pt==0))
{
if(TrueDist(P[count].x,x,P[count].z,z)<racerdist)//If collision detected
{
if(bounceID!=P[count].idno)
{
//Racer and user types must bounce and swap velocites (essentially) but also depends on the angle and velocity
bounceID=idno;
dt=FormAng(P[count].t-t,360);
temp1=-Vp*abs(sin(dt*pie/180))+P[count].Vp*cos(dt*pie/180);
temp2=-P[count].Vp*abs(sin(dt*pie/180))+Vp*cos(dt*pie/180);
Vp=temp1;
P[count].Vp=temp2;
tempbouncecount=1;
}
}
}
else if((pt==1||pt==0)&&(P[count].pt==2||P[count].pt==3))
{
if(TrueDist(P[count].x,x,P[count].z,z)<leaderdist)//If collision detected
{
//If a user or racer hits a follower or leader, slow down
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
MoveDamp();
tempbouncecount=1;
}
}
}
}
if(tempbouncecount==0)
bounceID=-1;
}
void Player::DetectBoundary(const Boundary &BL, const Boundary &BR)
{
//Check each segment of a boundary as a polygon to make sure that the player is not in it
int count; // Counting variable for the number of segments in the boundary
double x1,x2,x3,x4,z1,z2,z3,z4;//Four corners of the segment
double m1,m2,m3,m4;//slopes of each line segment
double line1,line2,line3,line4;
double currin=0;//Currently in the boundary flag
double tempt=0;//Temporary theta value
for(count=0;count<=BR.length-2;count++) // Check the location for every section in the boundary
{
x1=BL.x[count];
z1=BL.z[count];
x2=BR.x[count];
z2=BR.z[count];
x3=BR.x[count+1];
z3=BR.z[count+1];
x4=BL.x[count+1];
z4=BL.z[count+1];
//x=mz+b m=dx/dz b=x1-m*z1 x=m(z-z1)+x1
m1=((x1-x2)/(z1-z2));
m2=((x2-x3)/(z2-z3));
m3=((x3-x4)/(z3-z4));
m4=((x4-x1)/(z4-z1));
line1=m1*(z-z1)+x1;
line2=m2*(z-z2)+x2;
line3=m3*(z-z3)+x3;
line4=m4*(z-z4)+x4;
if( ((x>line3&&x<line1&&(m3>0&&m1>0))||
(x>line3&&x>line1&&(m3>0&&m1<0))||
(x<line3&&x<line1&&(m3<0&&m1>0))||
(x<line3&&x>line1&&(m3<0&&m1<0)))&&
((line2<line4&&x>line2&&x<line4)||
(line2>line4&&x<line2&&x>line4)))//If between line3 and line1 and line 2 and line 4.
{
currin=1;//currently in a boundary
tempt=2*atan(1/m2)*180/3.141592;//Calculate the bounce angle that it ould have to take
}
//printf("C:%d x: %lf z: %lf l1: %lf l2: %lf l3: %lf l4: %lf \n",count,x,z,line1,line2,line3,line4);//For debugging purposes
}
if(currin==1&&boundid==BL.side)
{
//If in the boundary that you were previously in
inabound=1;
tboundtemp =tempt;
}
else if(currin==1&&boundid!=BL.side)
{
inabound=1;
boundid=BL.side;//Save current boundary name
tboundtemp =tempt;
}
else if(currin==0&&boundid==BL.side)
{
inabound=0;//If you were previously in this boundary, and are no longer. Reset inabound
}
}
void Player::DetectEndCond(const Boundary &Path)
{
//Assumes that the end boundaries are flat, and are the 2nd to last element in path
double currin=0;
double tempt=0;
if(z>Path.z[Path.length-2]||z<Path.z[1])//If passed the end boundary
{
currin=1;
tempt=0;
}
if(currin==1&&boundid==Path.side)//If previously in the end boundary
{
inabound=1;
tboundtemp =tempt;
}
else if(currin==1&&boundid!=Path.side)
{
inabound=1;
boundid=Path.side;//Save the name of the boundary youre passing
tboundtemp =tempt;
}
else if(currin==0&&boundid==Path.side)
{
inabound=0;//If previously in the end boundary, reset it
}
}
void Player::MoveBound()//Move if a boundary has been crossed
{
if(inabound==1&&usedtobeinabound==0)//If first time in a boundary, save Vbound, tbound
{
if(pt==0)//If user, bounce back
{
if(abs(Vp)>abs(Vmax)/10)
Vbound=-Vp;
else if(abs(Vp)<abs(Vmax)/10)//Prevents the user from slowing to a crawl to get throught the barrier
Vbound=-Vp/abs(Vp)*abs(Vmax)/9;
Vp=Vbound;
Ap=0;
tbound=t;
}
if(pt!=0)//else, reflect
{
if(abs(Vp)>abs(Vmax)/10)
Vbound=Vp;
else if(abs(Vp)<abs(Vmax)/10)
Vbound=Vp/abs(Vp)*abs(Vmax)/9;
Vp=Vbound;
Ap=0;
tbound= -tboundtemp -t;
dt=0;
ddt=0;
t=tbound;
}
usedtobeinabound=1;//Remember that youve been through a boundary
}
else if(inabound==1&&usedtobeinabound==1)
{
//Every other time youre in the boundary
Ap=0;
Vp=Vbound;
t=tbound;
}
else if(inabound==0&&usedtobeinabound==1)//If you used to be in the boundary, and youre not anymore
usedtobeinabound=0;
}
void Player::MoveDamp()
{
// Motion takes the acceleration and dt and calculates a new position.
double Vdamp=(Vmax/Apmax)*(Vmax/Apmax)*times*times*times*dragc;
//Damping coefficient
// Slow down the player (drag/damping)
if(Vp>0)
{
Ap-=Apmax*Vp/Vmax*4/5+Apmax*1/5; //always remove apmax/5 (to help with hystoresis)
}
else if(Vp<0)
{
Ap+=abs(Vp)*Vmax/abs(Vmin)*Vdamp*4/5+Vmax*Vdamp*1/5;//Same as above
}
if((Vp+Ap<0&&Vp>0)||(Vp+Ap>0&&Vp<0))
{
//If the velocity would pass 0, stop the player
Vp=0;
Ap=0;
}
if(dt>0)//Angular damping
ddt-=dt*ddtmax/dtmax;
if(dt<0)
ddt-=dt*ddtmax/dtmax;
}
void Player::Move()//Calculate the new position
{
//Check Ap
if(Ap>Apmax)
Ap=Apmax;
else if (Ap<Apmin)
Ap=Apmin;
Vp+=Ap;//Calc Velocity
// Check Velocity
if(Vp>Vmax)
Vp=Vmax;
if(Vp<Vmin)
Vp=Vmin;
//Check ddt
if(ddt>ddtmax)
ddt=ddtmax;
else if (ddt<-ddtmax)
ddt=-ddtmax;
dt=dt+ddt;//Calculate dt
//Check dt
if(dt>dtmax)
dt=dtmax;
else if (dt<-dtmax)
dt=-dtmax;
t+=dt;//Calc new angle
// Keep the angle t between 0 and 360.
if(t>360)
t-=360;
if(t<0)
t+=360;
// Calculate new position
x+=Vp*cos(-t*3.141592/180);
z+=Vp*sin(-t*3.141592/180);
//Camera coordinates
double l=8.25;// Distance between player and camera
cx=x-l*cos(t*3.141592/180);
cz=z+l*sin(t*3.141592/180);
ctheta=t;
}
void Player::Check(const Boundary &BLL, const Boundary &BL, const Boundary &Path, const Boundary &BR,const Boundary &BRR)
{
//Check that the player is between any two of the boundaries
if(0==(BetweenBounds(BLL,BL,x,z)+BetweenBounds(BL,Path,x,z)+BetweenBounds(Path,BR,x,z)+BetweenBounds(BR,BRR,x,z)))
Reset();
}
void Player::CheckFinished(const Boundary &Path)
{
if((z>Path.z[Path.length-3])&&(pt==0||pt==1))//If racer or user, and the player has gone above the top point on the path
{
finish=1;//Race has finished
}
}
int Player::ClosestPlayer(Player *P, int pid,int tot)
{
//Find the closest player of interest
int id=-1;//PlayerID
int n;
double dist;
double olddist=8000000;
int type=-1;
//Different target types for different player types
switch( P[pid].pt)
{
case 3://Follower
type=2;
break;
case 4://Hunter
type=5;
break;
case 5://Prey
type=4;
break;
default://Everything else
break;
}
//Check every player
for(n=1;n<=tot;n++)
{
if(n!=pid)//Ignore the original player
{
if(P[n].pt==type)//If it is the correct type
{
dist= TrueDist(P[pid].x,P[n].x,P[pid].z,P[n].z);
if(dist<olddist)//check to see if it is closer than the last one
{
olddist=dist;//save it
id=n;
}
}
}
}
if(id==-1)//If not really looking
return -1;
else//Return the target ID number
return id;
}
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
void Player:: drawmodel_fish(void)
{
glTranslatef(x, y, -z);
glRotatef(180-t,0,1,0);
if (!pmodel1)
{
pmodel1 = glmReadOBJ("test3.obj"); //glm.cpp
if (!pmodel1) exit(0);
//glmUnitize(pmodel1); //glm.cpp
glmFacetNormals(pmodel1); //glm.cpp
glmVertexNormals(pmodel1, 90.0); //glm.cpp
}
glmDraw(pmodel1, GLM_SMOOTH| GLM_TEXTURE);//glm.cpp
}
void Player:: drawmodel_fish_mod()
{
glTranslatef(x, y, -z);
glRotatef(180-t,0,1,0);
if (!pmodel2)
{
pmodel2 = glmReadOBJ("fishLL.obj"); //glm.cpp
if (!pmodel2) exit(0);
// glmUnitize(pmodel1); //glm.cpp
glmFacetNormals(pmodel2); //glm.cpp
glmVertexNormals(pmodel2, 90.0); //glm.cpp
}
glmDraw(pmodel2, GLM_SMOOTH| GLM_TEXTURE);//glm.cpp
}
void Player:: drawmodel_fish_mod2()
{
glTranslatef(x, y, -z);
glRotatef(180-t,0,1,0);
if (!pmodel3)
{
//printf("Loaded\n");
pmodel3 = glmReadOBJ("fishRR.obj"); //glm.cpp
if (!pmodel3) exit(0);
// glmUnitize(pmodel1); //glm.cpp
glmFacetNormals(pmodel3); //glm.cpp
glmVertexNormals(pmodel3, 90.0); //glm.cpp
}
glmDraw(pmodel3, GLM_SMOOTH| GLM_TEXTURE);//glm.cpp
}
void Player::drawmodel_fish_AI()
{
double x1 = x-2;
double x2 = x+2;
double y1 = y;
double y2 = y+10;
double z1 = -(z-2);
double z2 = -(z+2);
glTranslatef(x, y, -z);
glRotatef(180-t,0,1,0);
if (!pmodel4)
{
//printf("Simple Loaded\n");
pmodel4 = glmReadOBJ("test3.obj"); //glm.cpp
if (!pmodel4) exit(0);
// glmUnitize(pmodel1); //glm.cpp
glmFacetNormals(pmodel4); //glm.cpp
glmVertexNormals(pmodel4, 90.0); //glm.cpp
}
glmDraw(pmodel4, GLM_SMOOTH| GLM_TEXTURE);//glm.cpp
}
void Player::drawfishes(int runtime)
{
if(pt == 0)
{
glColor3ub(35,234,23);
int runt = runtime%switchtime;
if(runt >=0 && runt < switchtime/4)
{
drawmodel_fish();
}
else if (runt >=switchtime/5 && runt < 2*switchtime/4)
{
drawmodel_fish_mod2();
}
else if(runt >=2*switchtime/5 && runt < 3*switchtime/4)
{
drawmodel_fish();
}
else if(runt >=3*switchtime/5 && runt < 4*switchtime/4-1)
{
drawmodel_fish_mod();
}
}
else
{
if(pt == 1)
{
glColor3ub(255,0,0);
}
else
glColor3ub(255,10,200);
drawmodel_fish_AI();
//drawmodel_fish();
}
glEnd();
}
void Player::Draw(int runtime)
{
glPushMatrix();
glColor3ub(0,100,255);
drawfishes(runtime);
glPopMatrix();
}