Collision_-_RectTileMAPCollide.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // POINTTILECOLLIDE function
  3. //
  4. // Here is a example of how to check collision with a point and a tile on a tilemap.
  5. //
  6. #include "raylib.h"
  7. #include <math.h>
  8. #define MAX_CARS 100
  9. #define MAX_WHISKERS 8
  10. int myMap[10][11] = { {1,1,1,1,1,1,1,1,1,1,1},
  11. {1,0,0,0,0,0,0,0,0,0,1},
  12. {1,0,1,1,1,1,1,1,1,0,1},
  13. {1,0,1,1,1,1,1,0,0,0,1},
  14. {1,0,1,0,0,0,1,0,1,1,1},
  15. {1,0,1,0,1,0,1,0,0,0,1},
  16. {1,0,1,0,1,0,1,1,1,0,1},
  17. {1,0,1,0,1,0,1,1,1,0,1},
  18. {1,0,0,0,1,0,0,0,0,0,1},
  19. {1,1,1,1,1,1,1,1,1,1,1}
  20. };
  21. int mapWidth = 11;
  22. int mapHeight = 10;
  23. float tileWidth;
  24. float tileHeight;
  25. //Unit collide with solid blocks true/false
  26. bool recttilecollide(int x, int y, int w, int h, int offsetx,int offsety);
  27. // Our rectsoverlap function. Returns true/false.
  28. static bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2);
  29. int main(void)
  30. {
  31. // Initialization
  32. //--------------------------------------------------------------------------------------
  33. const int screenWidth = 800;
  34. const int screenHeight = 600;
  35. tileWidth = ceil((float)(float)screenWidth/(float)mapWidth);
  36. tileHeight = ceil((float)screenHeight/(float)mapHeight);
  37. InitWindow(screenWidth, screenHeight, "raylib example.");
  38. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  39. //--------------------------------------------------------------------------------------
  40. Vector2 position;
  41. position.x = 100;
  42. position.y = 100;
  43. int time=0;
  44. Color collisionColor = GREEN;
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. time++;
  51. if(time>30){ // every time time is at 300
  52. time=0;//reset time
  53. position.x = GetRandomValue(32,screenWidth-32);
  54. position.y = GetRandomValue(32,screenHeight-32);
  55. }
  56. if(recttilecollide(position.x,position.y,tileWidth/2, tileHeight/2, 0,0)){
  57. collisionColor = RED;
  58. }else{
  59. collisionColor = GREEN;
  60. }
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. // Draw map
  67. for (int y = 0; y< mapHeight ; y++)
  68. {
  69. for (int x = 0; x< mapWidth ; x++)
  70. {
  71. if (myMap[y][x] == 1)
  72. {
  73. DrawRectangle(x*tileWidth,y*tileHeight,tileWidth,tileHeight,BLUE);
  74. }
  75. }
  76. }
  77. DrawRectangle(position.x,position.y,tileWidth/2,tileHeight/2,collisionColor);
  78. EndDrawing();
  79. //----------------------------------------------------------------------------------
  80. }
  81. // De-Initialization
  82. //--------------------------------------------------------------------------------------
  83. CloseWindow(); // Close window and OpenGL context
  84. //--------------------------------------------------------------------------------------
  85. return 0;
  86. }
  87. //Unit collide with solid blocks true/false
  88. bool recttilecollide(int x, int y,int w, int h, int offsetx,int offsety){
  89. int cx = (x+offsetx)/tileWidth;
  90. int cy = (y+offsety)/tileHeight;
  91. for(int y2=cy-1; y2<cy+2;y2++){//Note that the - and + are to be set differently with differently sized players
  92. for(int x2=cx-1; x2<cx+2;x2++){
  93. if(x2>=0 && x2<mapWidth && y2>=0 && y2<mapHeight){
  94. if(myMap[y2][x2] == 1){
  95. int x3 = (x2)*tileWidth;
  96. int y3 = (y2)*tileHeight;
  97. if(rectsoverlap(x+offsetx,y+offsety,w,h,x3,y3,tileWidth,tileHeight)){
  98. return true;
  99. }
  100. }
  101. }
  102. }}
  103. return false;
  104. }
  105. // Rectangles overlap
  106. bool rectsoverlap(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2){
  107. if(x1 >= (x2 + w2) || (x1 + w1) <= x2) return false;
  108. if(y1 >= (y2 + h2) || (y1 + h1) <= y2) return false;
  109. return true;
  110. }