racing.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*******************************************************************************************
  2. * I started this racing game experiment on the iphone and with appgamekit mobile during todays f1's forumula 1 race.
  3. *
  4. *
  5. * Trying to figure out how to do a racing game using lanes...
  6. *
  7. * I made a array of vector2d's, this in a circle as the racetrack.
  8. * there are 5 lanes. 5 bots that should be able to race against each other.
  9. *
  10. * work in progress or figure out yourself.
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include <math.h>
  15. #define MAXLANES 5
  16. #define MAXBOTS 5
  17. typedef struct thebot{
  18. Vector2 position;
  19. float speed;
  20. int pos;
  21. int lane;
  22. }thebot;
  23. static struct thebot bot[MAXBOTS];
  24. Vector2 track[5][360];
  25. void maketrack();
  26. void drawtrack();
  27. void updatebots();
  28. void drawbots();
  29. static float edistance(float x1,float y1,float x2,float y2);
  30. int main(void)
  31. {
  32. // Initialization
  33. //--------------------------------------------------------------------------------------
  34. const int screenWidth = 800;
  35. const int screenHeight = 450;
  36. InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
  37. maketrack();
  38. for(int i=0;i<MAXBOTS;i++){
  39. bot[i].position = track[i][0];
  40. bot[i].lane=i;
  41. bot[i].pos = 0;
  42. bot[i].speed = GetRandomValue(500,1000)/1000.0f;
  43. }
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. //track[0][0] = (Vector2){0,0};
  47. int countdown=300;
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. // TODO: Update your variables here
  54. //----------------------------------------------------------------------------------
  55. if(countdown>0)countdown--;
  56. if(countdown==0){
  57. updatebots();
  58. updatebots();
  59. }
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. drawtrack();
  65. drawbots();
  66. //DrawText(FormatText("%f",(float)GetRandomValue(0,10000)/10000*2-1), 190, 200, 20, LIGHTGRAY);
  67. EndDrawing();
  68. //----------------------------------------------------------------------------------
  69. }
  70. // De-Initialization
  71. //--------------------------------------------------------------------------------------
  72. CloseWindow(); // Close window and OpenGL context
  73. //--------------------------------------------------------------------------------------
  74. return 0;
  75. }
  76. void drawtrack(){
  77. for(int i=1;i<360;i++){
  78. for(int t=0;t<5;t++){
  79. DrawLineV(track[t][i-1],track[t][i],BLACK);
  80. }
  81. }
  82. }
  83. //
  84. // We have a array of [lanes][nodes] = vector2d
  85. // We create it by just making a circle in 360 steps. The cars wil go from node to node and loop this.
  86. //
  87. //
  88. void maketrack(){
  89. float x = 0.0f;
  90. float y = 0.0f;
  91. for(int i=0;i<360;i++){
  92. for(int t=0;t<5;t++){
  93. track[t][i] = (Vector2){x+x/100*t*10.0f,y+y/100*t*10.0f};
  94. track[t][i].x += 320;
  95. track[t][i].y += 100;
  96. track[t][i].y -= t*10;
  97. }
  98. x+=cos(i*DEG2RAD)*2;
  99. y+=sin(i*DEG2RAD)*2;
  100. }
  101. }
  102. //
  103. // Here the magic should happen(i think) Move towards the next node/.
  104. // If close to the target node then next node.
  105. //
  106. // Lane magic code should be added here.
  107. //
  108. void updatebots(){
  109. for(int i=0;i<MAXBOTS;i++){
  110. float l=edistance( bot[i].position.x,
  111. bot[i].position.y,
  112. track[bot[i].lane][bot[i].pos].x,
  113. track[bot[i].lane][bot[i].pos].y);
  114. if(l<5){
  115. bot[i].pos+=1;
  116. if(bot[i].pos>359)bot[i].pos=0;
  117. }
  118. float an = atan2(bot[i].position.y-track[bot[i].lane][bot[i].pos].y,bot[i].position.x-track[bot[i].lane][bot[i].pos].x);
  119. bot[i].position.x-=cos(an)*bot[i].speed;
  120. bot[i].position.y-=sin(an)*bot[i].speed;
  121. // Switch lane logic
  122. bool close=false;
  123. for(int j=0;j<MAXBOTS;j++){
  124. if(j==i)continue;
  125. float l=edistance( bot[i].position.x,
  126. bot[i].position.y,
  127. bot[j].position.x,
  128. bot[j].position.y);
  129. if(l<20)close=true;
  130. }
  131. if(close==false){
  132. if(bot[i].lane>0){
  133. bot[i].lane--;
  134. bot[i].pos+=2;
  135. }
  136. }
  137. // if overtake and only 2 close by
  138. int cnt=0;
  139. for(int j=0;j<MAXBOTS;j++){
  140. if(j==i)continue;
  141. float l=edistance( bot[i].position.x,
  142. bot[i].position.y,
  143. bot[j].position.x,
  144. bot[j].position.y);
  145. if(l<20 && bot[j].speed<bot[i].speed)cnt++;
  146. }
  147. if(cnt==1){
  148. if(bot[i].lane==0){
  149. bot[i].lane++;
  150. bot[i].pos+=2;
  151. }
  152. }
  153. }
  154. }
  155. void drawbots(){
  156. for(int i=0;i<MAXBOTS;i++){
  157. DrawCircleV(bot[i].position,5,RED);
  158. }
  159. }
  160. // Euclidean distance (more precise)
  161. float edistance(float x1,float y1,float x2,float y2){
  162. return sqrt( (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) );
  163. }